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

时间:2012-10-18 08:45:57

标签: nginx wildcard wildcard-subdomain

我希望能够将子域名 .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;
 }

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

Nginx (*wildcard_domain.com) --(cache)--> Apache (*wildcard_domain.com)
Nginx (anything.domain.com) --(cache)--> Apache (anything.domain.com)

2 个答案:

答案 0 :(得分: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;
    }
}

答案 1 :(得分:2)

所以我试图找到这个问题的答案并继续找到这篇文章。但我认为dmytrivv的答案已经过时了。在我们的场景中,我们有两个通配符域(例如* .mydomain.com)和自定义域(例如,fullcustomdomain.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;
        }
}

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

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