我尝试在 Tomcat 中使用 Nginx vhost webapp,我的 vhost 配置文件:
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
server_name *.a.com;
access_log /var/log/access.log;
error_log /var/log/error.log;
root /opt/javaee/shared/shared1/apache-tomcat-8.0.30/webapps/testapp1;
index index.html index.jsp;
location / {
rewrite ^ /testapp1$1 last;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://localhost:8080;
proxy_redirect off;
}
}
当我在浏览器中请求 a.com
时,它会继续下载 index.jsp 而不是提供页面。当我请求 localhost:8080/testapp1
时,一切正常。请有任何见解。
最佳答案
rewrite ^/testapp1$1 last;
在我看来完全错误。一切都在无限循环中重写为 /testapp1
。我很惊讶它能提供任何服务。
如果您希望将 /
(并且仅 /
)映射到内部路径 /testapp1
,请使用:
location = / { rewrite ^ /testapp1 last; }
location / {
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://localhost:8080;
proxy_redirect off;
}
如果您希望在将所有内容发送到上游之前以 /testapp1
为前缀,请使用:
location / {
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://localhost:8080/testapp1/;
proxy_redirect off;
}
关于tomcat - Nginx 下载 tomcat index.jsp 而不是服务它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34581220/