【问题标题】:Nginx wildcard proxy, pass subdomain to the server (upstream proxy)Nginx通配符代理,将子域传给服务器(上游代理)
【发布时间】:2012-10-08 16:05:25
【问题描述】:

我希望能够将 subdomain.domain.com 传递到 .domain.com apache 服务器,同时包含 subdomain 信息。

我想为域创建一个 nginx 缓存,就像通配符一样,但将 子域 传递到目的地(也有 apache 巫婆通配符)。到目前为止,我通过 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)

【问题讨论】:

  • 你有没有想过这个问题?

标签: nginx wildcard wildcard-subdomain


【解决方案1】:
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;
    }
}

【讨论】:

  • 如果对此答案有一些解释会很好。仅将代码作为答案,虽然可能是准确的,但并没有提供任何关于它为什么起作用的信息。知道某事为什么有效比知道什么有效更有价值。
【解决方案2】:

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

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 终止代理。

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

【讨论】:

    猜你喜欢
    • 2020-05-05
    • 2017-07-25
    • 2013-08-29
    • 2020-04-19
    • 2016-12-26
    • 2023-03-31
    • 2011-06-20
    • 1970-01-01
    相关资源
    最近更新 更多