How can Nginx add the Subdomain as parameter when a proxy_pass to Tomcat is executed

做~自己de王妃 提交于 2019-12-05 20:41:51

The answer is little bit simpler than that. Just get the substring with the subdomain and use it as a parameter for 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;              
  }                                                              
} 

I considered the request with or without args. I think it solves yout problem.

Alfredo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!