Nginx通配符代理,将子域传递给服务器(上游代理)

sir*_*bax 10 wildcard-subdomain wildcard nginx

我希望能够通过子域 .domain.com到.domain.com Apache服务器,与子域的信息了.

我想为域创建一个nginx缓存,就像通配符一样,但是将子域传递到目的地(也有apache witch wildcard).到目前为止,我通过proxy_set_header Host $ host传递信息; 但我想在apache服务器上有子域请求.

  upstream domain.com {
    server 172.1.1.1:80 weight=50 fail_timeout=30s;
  }

  server {
    server_name *.domain.com;

    location / {
      proxy_pass http://domain.com;
      #proxy_pass $request;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $host;
     }

    location ~* ^.+.    (jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js|swf)$ {
     proxy_pass http://topmanagergame.com;
     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
     proxy_cache my-cache;
     proxy_cache_valid  200 302  30m;
     proxy_cache_valid  404      1m;
    }

    access_log /var/log/nginx/domain.com.log main;
    error_log off;
 }
Run Code Online (Sandbox Code Playgroud)

你认为我可以在上游使用proxy_pass吗?

Nginx (*wildcard_domain.com) --(cache)--> Apache (*wildcard_domain.com)
Nginx (anything.domain.com) --(cache)--> Apache (anything.domain.com)
Run Code Online (Sandbox Code Playgroud)

dmy*_*ivv 10

upstream somestring {
    server domain2.com:80 weight=50 fail_timeout=30s;
}

server {
    listen  80;
    server_name *.domain.com;

    server_name ~^(?<subdomain>.+)\.domain\.com$;

    location / {
        proxy_pass http://somestring;
        proxy_set_header   Host             $subdomain.domain2.com;
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 如果这个答案有一些解释,那就太好了。仅用代码作为答案虽然可能是准确的,但并没有提供任何有关其工作原理的信息。知道为什么某件事有效比知道什么有效更有价值。 (4认同)

Ran*_*ust 7

所以我试图找到这个问题的答案并不断找到这篇文章。但我认为 dmytivv 的回答已经过时了。在我们的场景中,我们同时拥有通配符域(例如 *.mydomain.com)和自定义域(例如fullycustomdomain.com)。但是你可以通过使用proxy_set_header Host $host;来解决这两个问题 并在收听结束时默认

upstream qaweb {
    # Servers in the web farm
    server ip-notreal-name.ec2.internal:80;
}


server {
        listen 443 ssl default;

        ssl_certificate     certs/mydomain.com.crt;
        ssl_certificate_key certs/mydomain.com.key;

        # Support for wildcard domains
        server_name admin.mydomain.com *.mydomain.com "";

        location / {
                # Turn off access logging so we don't fill the hardrive
                access_log      off;
                proxy_pass http://qaweb;
                proxy_set_header Host $host;
                # So that the correct IP shows up in the log once libapache2-mod-rpaf is installed
                proxy_set_header X-Real-IP  $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
}
Run Code Online (Sandbox Code Playgroud)

请注意,我们还将其用作 TLS 终止代理。

您还可以在此处找到有关如何使用 proxy_pass 的更多示例https://www.liaohuqiu.net/posts/nginx-proxy-pass/