【问题标题】:How can Nginx add the Subdomain as parameter when a proxy_pass to Tomcat is executed执行 Tomcat 的 proxy_pass 时,Nginx 如何添加子域作为参数
【发布时间】:2014-05-11 09:08:02
【问题描述】:

我正在努力实现的目标
Web 应用程序应该能够支持多个子域,而无需在每次使用新子域时对 nginx 或 tomcat 进行任何更改。 (我已经对 DNS 进行了必要的更改以支持通配符子域)

Nginx 监听 80 端口。它在 8080 端口对 tomcat 执行 proxy_pass。 nginx应该能够支持多个子域。

我目前的设置是基于这个答案。但它没有传递参数
Nginx proxy_pass : Is it possible to add a static parameter to the URL?

所有可能的子域
dynamic_subdomain_1.localhost
dynamic_subdomain_2.localhost

nginx 设置

server {
    listen 80 default_server;

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

    location / {
        set $args ?$args&site=$subdomain;
        proxy_pass http://127.0.0.1:8080;
    }
}

Nginx 在调用 Tomcat 时应将子域作为参数附加。

每个子域对 Tomcat 的调用应如下所示

http://127.0.0.1:8080?site=dynamic_subdomain_1
http://127.0.0.1:8080?site=dynamic_subdomain_2

我已经尝试了上面的设置,但是查询参数总是显示为空。

我应该在 nginx 中进行哪些更改才能实现这一点?

【问题讨论】:

    标签: java tomcat nginx dns wildcard-subdomain


    【解决方案1】:

    答案比这简单一点。只需获取带有子域的子字符串并将其用作proxy_pass的参数即可:

    server {                                                         
      # this matches every subdomain of domain.
      server_name .domain;                                           
    
      location / {                                                   
        set $new_request_uri "";                                     
        set $subdomain "";
    
        if ($host ~* "^(.+)\.domain$") {                             
          set $subdomain $1;                                         
          # lets assume there are args...
          set $new_request_uri "$request_uri&subdomain=$subdomain";  
        }                                                            
        # if there are no args add a question mark and the subdomain argument
        if ($args = '') {                                            
          set $new_request_uri "$request_uri?subdomain=$subdomain";  
        }                                                            
    
        proxy_pass http://127.0.0.1:8080$new_request_uri;              
      }                                                              
    } 
    

    我考虑了带或不带参数的请求。我认为它可以解决您的问题。

    阿尔弗雷多

    【讨论】:

      猜你喜欢
      • 2015-10-25
      • 2017-11-13
      • 2011-01-27
      • 1970-01-01
      • 2020-10-20
      • 2013-01-08
      • 2011-07-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多