解决nginx https代理tomcat redirect问题

    xiaoxiao2022-06-29  46

    问题描述

    http服务器:nginx,10.10.10.95,版本:1.10.1,请求使用协议为https,端口为18080。

    服务服务器:tomcat,10.10.10.92,使用协议为http,端口为8080。

    问题:当在业务服务器使用sendRedirect时,tomcat响应302给nginx,nginx再响应给浏览器,默认情况下,nginx响应给浏览器的location会将schema换为http,端口为:80。

    这就导致浏览器redirect后,访问不到正确资源。

    过程举例:

    浏览器输入:https://10.10.10.95:18080/redirect_test.doredirect_test.do中执行sendRedirect("/welcome.do")浏览器接收到的redirect响应(302)中location为http://10.10.10.95/welcome.do浏览器跳到http://10.10.10.95/welcome.do,发现访问不了

    解决方案

    修改nginx配置,关键配置如下:

        server{         listen 18080;         server_name ecsc;         ssl on;         ssl_certificate  /root/ssl/test.crt;         ssl_certificate_key  /root/ssl/test_nopass.key;         error_page 497  https://$host:8080;         access_log  /var/log/nginx/access.log  main;         proxy_redirect http:// $scheme://;         port_in_redirect on;         location ~/druid{                 return 404;         }         location /{                 proxy_connect_timeout 300;                 proxy_send_timeout 300;                   proxy_read_timeout 300;                 expires 10d;                 proxy_pass  http://console.eayun.com ;                 proxy_set_header Host $host:$server_port;                 proxy_set_header  X-Real-IP        $remote_addr;                   proxy_set_header  X-Forwarded-For  $proxy_add_x_forwarded_for;                 proxy_set_header X-NginX-Proxy true;         }

    说明:

    使用proxy_redirect将location中的协议转换为请求nginx的协议。

    使用port_in_redirect on指示nginx使用请求的端口,而不是使用默认端口。

    proxy_set_header Host $host:$server_port;很关键,我也不明白为啥,不设置,端口为变为80。

    转载请注明原文地址: https://ju.6miu.com/read-1125516.html

    最新回复(0)