NGINX作为反向代理 - 转发不起作用

问题描述 投票:0回答:1

我有以下几点:

  • HTTPS访问NAS或类似的东西。
  • NGINX作为容器的反向代理
  • 容器与Tomcat作为appcontainer。

NAS将HTTPS请求作为HTTP转发到NGINX容器。然后NGINX容器将HTTP请求转发给我的appcontainer。

我可以访问我的appcontainer登录页面,但登录后,如下Nginx access.log进行POST

POST /foo/login.do HTTP/1.1" 302 0 "https://nas.dns.server/foo/login.do

在appcontainer tomcat中的localhost_access.log中显示

POST /foo/doLogin.do HTTP/1.0" 302

并请求作为HTTP的NAS

似乎忽略了X-Forwarded-Proto标头。

我的nginx.conf配置如下:

server {
    listen 80;

    server_name $hostname;
    access_log  /var/log/nginx/access.log;
    error_log   /var/log/nginx/error.log;
    error_log   /dev/stdout info;
    access_log  /dev/stdout;

    client_max_body_size 100M;

    proxy_connect_timeout 300;
    proxy_send_timeout 300;
    proxy_read_timeout 300;
    send_timeout 300;

    resolver 127.0.0.11 valid=30s;

    sendfile on;

    location /foo {
        proxy_set_header Origin "";
        set $appcontainer          http://appcontainer:8080;
        proxy_pass         $appcontainer;
        proxy_set_header    Host                $host;
        proxy_set_header    X-Real-IP           $remote_addr;
        proxy_set_header    X-Forwarded-For     $proxy_add_x_forwarded_for;
        proxy_set_header    X-Forwarded-Proto   $https;  #I’ve also tested with $scheme
    } 
}

谢谢

https nginx-reverse-proxy forwarding
1个回答
0
投票

在网络选项卡中查看Chrome的开发者工具可以看到login.do的调用有Request URL: https://entry.proxy.url/foo/doLogin.do,但在Response Headers我可以看到产生问题的Location: http://proxy.entry.url/foo/login.do必须是Location: https://proxy.entry.url/foo/login.do

我已经尝试在位置进行重定向proxy_redirect http://entry.proxy.url/ https://csprocure.ciport.be/;并且它有效。

所以位置设置为:

location /foo {
    proxy_set_header Origin "";
    set $appcontainer          http://appcontainer:8080;
    proxy_redirect     http://proxy.entry.url/ https://proxy.entry.url/;
    proxy_pass         $appcontainer;
    proxy_set_header    Host                $host;
    proxy_set_header    X-Real-IP           $remote_addr;
    proxy_set_header    X-Forwarded-For     $proxy_add_x_forwarded_for;
    proxy_set_header    X-Forwarded-Proto   $https;  #I’ve also tested with $scheme
} 
© www.soinside.com 2019 - 2024. All rights reserved.