nginx反向代理,首页重定向自动添加端口导致访问异常

问题:使用nginx反向代理,首页重定向到前端静态资源目录时自动添加端口 8001导致无法访问。

nginx版本1.16.1,配置内容如下:

server
{
    listen 80;
    server_name pifa.test;
    index index.php index.html;
    location / {
        proxy_set_header Host $host;
        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;
        proxy_set_header X-Forwarded-Proto  $scheme;
        proxy_pass http://192.168.251.8:8001; 
    }
}

被代理配置:

server
{
    listen 8001;
    server_name 192.168.251.8;
    index index.php index.html;
    root /www/wwwroot/kpi/public;

    location ~ [^/]\.php(/|$)
    {
        try_files $uri =404;
        fastcgi_pass  unix:/tmp/php-cgi-73.sock;
        fastcgi_index index.php;
        include fastcgi.conf;
        include pathinfo.conf;
    }
}

跳转的前端目录:
nginx反向代理,首页重定向自动添加端口导致访问异常

1、访问首页 pifa.test 打印内容正常
代码如下:

public function wellcome()
{
    dd(redirect('/app/kpi/')->content());
    return redirect('/app/kpi/');
}

打印内容如下:
nginx反向代理,首页重定向自动携带被代理端口导致访问异常

2、移除打印代码后,访问首页 pifa.test 就会自动加上端口 8001 变成 pifa.test:8001/app/kpi 导致无法访问,页面截图如下:
nginx反向代理,首页重定向自动添加端口导致访问异常

手动移除端口后访问 pifa.test/app/kpi 是正常的,页面截图如下:
nginx反向代理,首页重定向自动添加端口导致访问异常

有大神能帮忙看一下吗?

《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
最佳答案

查阅相关资料,最终在被代理的 nginx 配置中添加 absolute_redirect off 成功。

server
{
    listen 8001;
    server_name _;
    absolute_redirect off;
    index index.php index.html;
    root /www/wwwroot/kpi/public;
    ...
}

资料网址:Nginx中absolute_redirect、port_in_redirect配置简述

2年前 评论
讨论数量: 2

多次测试发现是 Nginx反向代理的目录访问问题,pifa.test/app/kpi/ 正常访问,pifa.test/app/kpi 访问失败。虽然可以通过再配置 location /app/kpi/ 进行代理,但是还有没有其它方法呢?

2年前 评论

查阅相关资料,最终在被代理的 nginx 配置中添加 absolute_redirect off 成功。

server
{
    listen 8001;
    server_name _;
    absolute_redirect off;
    index index.php index.html;
    root /www/wwwroot/kpi/public;
    ...
}

资料网址:Nginx中absolute_redirect、port_in_redirect配置简述

2年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!