Nginx 下载 tomcat index.jsp 而不是提供它

Nginx downloads tomcat index.jsp instead of serving it

我尝试在 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 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 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;
}