记一次nginx重定向端口问题

环境:
阿里云平台:

SLB:111.111.111.100:8001 映射>  ECS Nginx:10.10.10.100:80 反向代理> Tomcat:127.0.0.1:8080
域名解析: test.xxxx.com.cn > 111.111.111.100

ECS服务器配置:
Nginx:(80端口)
配置文件

server {
    listen       80;
    server_name  test.xxxx.com.cn;
    access_log  logs/test-access.log  main;
    error_log   logs/test-error.log;
    location / {
        proxy_pass      http://127.0.0.1:8080;
        #root   html;
        #index  index.html index.htm;
   	}
}

tomcat(8080端口)
配置文件:默认tomcat配置信息
应用路径:$tomcat_home/webapps/test-app
测试:
完成以上配置后,浏览器测试访问http://test.xxxx.com.cn:8001/test-app,无法访问,且浏览器url被重置为http://test.xxxx.com.cn/test-app/login

抓包跟踪检查,发现Request URL: http://test.xxxx.com.cn:8001/test-app,
但Response Headers中Location: http://test.xxxx.com.cn/test-app

处理:
修改nginx.conf配置文件

server {
    listen       80;
    server_name  test.xxxx.com.cn;
    access_log  logs/test-access.log  main;
    error_log   logs/test-error.log;
    location / {
        proxy_pass      http://127.0.0.1:8080;
        proxy_set_header Host $host:8001;  #这里修改为SLB监听的端口号
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header REMOTE-HOST $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        #root   html;
        #index  index.html index.htm;
    }
}

重启Nginx服务

# /home/nginx/sbin/nginx -s reload

再次访问http://test.xxxx.com.cn:8001/test-app,正常访问。
在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值