我已经在DigitalOcean上安装了Ubuntu 15.10 x64服务器来testing几个Java应用程序。 我安装了Nginx 1.9.3和Tomcat8,并使用Nginx作为反向代理,将所有请求转发到端口8080上的Tomcat。
这是事情变得危险的地方。 我在我的tomcat安装上运行了两个应用程序,/ webapp1&/ webapp2。 我想指出subdomain.domain.com到/ webapp1和subdomain2.domain.com到/ webapp2。 在Apache中,这是一个相当简单的使用mod_proxy的事情,但对于Nginx来说,这有点神秘。
到目前为止,这是我在我的网站启用/域文件尝试。
第一轮
server{ server_name subdomain1.domain.com; # ******************SSL configuration ************************ listen 443 ssl default_server; ssl_certificate /etc/nginx/conf/ssl/domain.crt; ssl_certificate_key /etc/nginx/conf/ssl/domain.key; #********************************************************************* #**********Proxy********************** location / { proxy_redirect off; proxy_set_header X-Forwarded-Host $host; proxy_set_header X-Forwarded-Server $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://subdomain1.domain.com:8080/webapp1/; }
这些请求在我访问https://subdomain1.domain.com时起作用,但是在我的应用程序中引用上下文path的任何链接(例如/ webapp1 /)导致包含两个上下文path(例如https://subdomain1.domain)的url。 com / webapp1 / webapp1 / 。 这引起了各种各样的参考文献
第二轮我发现了一个讨论类似问题的线程,修复方法是使用重写从URL中去除额外的上下文path。
server{ server_name subdomain1.domain.com; # ******************SSL configuration ************************ listen 443 ssl default_server; ssl_certificate /etc/nginx/conf/ssl/domain.crt; ssl_certificate_key /etc/nginx/conf/ssl/domain.key; #********************************************************************* #**********Proxy********************** location / { proxy_redirect off; proxy_set_header X-Forwarded-Host $host; proxy_set_header X-Forwarded-Server $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://subdomain1.domain.com:8080/webapp1/; rewrite ^/webapp1/(.*)$ /$1 last; }
这解决了我在url中的双上下文path的问题,现在可以工作,但我想知道是否有一个更优雅的解决scheme,使重写是不需要的。 我怀疑从服务器资源的angular度来看,重写是耗资巨大的,但是我不喜欢它。
预先感谢您的时间。
更新
我已经阅读了有关位置configuration。 这是另一个解决url中的双上下文path的configuration,避免了必须使用重写。 基本上,nginx使用两个位置configuration,所以当uri有一个像https://subomdain1.domain.com/webapp1/webapp1/这样的双上下文path时,它会在我的第二个位置块中select它,并将其转发到我的tomcat服务器但没有第一个上下文path。 我不知道这个或重写解决scheme是否更优雅。
server{ server_name subdomain1.domain.com; # ******************SSL configuration ************************ listen 443 ssl default_server; ssl_certificate /etc/nginx/conf/ssl/domain.crt; ssl_certificate_key /etc/nginx/conf/ssl/domain.key; #********************************************************************* #**********Proxy********************** location / { proxy_redirect off; proxy_set_header X-Forwarded-Host $host; proxy_set_header X-Forwarded-Server $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://subdomain1.domain.com:8080/webapp1/; #rewrite ^/webapp1/(.*)$ /$1 last; } location /webapp1/ { proxy_redirect off; proxy_set_header X-Forwarded-Host $host; proxy_set_header X-Forwarded-Server $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://subdomain1.domain.com:8080/webapp1/; } }