tomcat - Nginx 下载 tomcat index.jsp 而不是服务它

标签 tomcat nginx configuration

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

相关文章:

tomcat - 如何更改ROOT应用程序?

java - Spring.io 和 Tiles 3 - 在 Tomcat 服务器中部署 .war 文件时出现错误 404

ubuntu - NGINX Ubuntu Web Server 访问 CentOS 文件服务器 : forbidden

http - AngularJS - 未知提供者配置 $httpProvider

python-3.x - Python3 日志 yaml 配置

IntelliJ IDEA 中的 Tomcat 配置

java - 调试 Apache Tomcat

json - java.io.FileNotFoundException : WebContent\WEB-INF\classes\nmdp. json(系统找不到指定的路径)

docker - 0.0.0.0拒绝连接

nginx - k8s 反向代理使用自签名证书 nginx 保护上游