我试图在Tomcat中使用Nginx vhost一个web应用程序,即我的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;
}

09-13 11:26