请教下大家: Frp 将内网 Nginx 80 端口代理到外网 18080 后,浏览器登录后错误, F12 发现返回了错误端口 80

2022-05-14 16:44:17 +08:00
 zunsgm

环境:

web: 192.168.1.10:8080 nginx: 192.168.1.100:80 frp(内网穿透): 100.123.10.10:18080 frp 使用 tcp 模式,将内网 nginx 端口 80 的 web 服务在开放到外网 100.123.10.10 的 18080 端口。

问题:

在网页登陆成功后,外部 url 的端口却变成称了 80 导致访问失败: 浏览器访问 http://100.123.10.10:18080 后可以正常显示登录界面,通过 f12 查看 network 发现 点击登录按钮后,nginx 返回的端口号为 80 ,也就是 http://100.123.10.10:80 。正常应该返回 http://100.123.10.10:18080 端口才对。在点击登录后手动加上 18080 端口才能正常跳转登录后的页面

Frp 内网穿透配置:

[ test ]
   type=tcp
   host=192.168.1.100
   local_port=80
   remote=18080

Nginx 配置:

http {
  include       mime.types;
  default_type  application/octet-stream;

  sendfile        on;
  keepalive_timeout  65;

  server {
    listen       80;
    server_name  localhost;

    location / {
        sendfile off;
        #proxy_redirect     default;
        proxy_pass http://127.0.0.1:8080;
        proxy_http_version 1.1;

        proxy_set_header   Connection        $connection_upgrade;
        proxy_set_header   Upgrade           $http_upgrade;

        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-Forwarded-Proto $scheme;
        proxy_max_temp_file_size 0;

        client_max_body_size       10m;
        client_body_buffer_size    128k;

        proxy_connect_timeout      90;
        proxy_send_timeout         90;
        proxy_read_timeout         90;
        proxy_buffering            off;
        proxy_request_buffering    off; # Required for HTTP CLI commands
        proxy_set_header Connection ""; # Clear for keepalive
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }}}

已尝试的方法:

之前以为是 proxy_set_header 的问题.更改为 ¥ host:$server_port 后 测试也不行。F12 后返回的端口还是不正确。

884 次点击
所在节点    问与答
6 条回复
stille
2022-05-14 17:29:36 +08:00
试试 frp 用 http 模式,在 frps 里设置 vhost_http_port = 18080
zunsgm
2022-05-14 20:35:15 +08:00
@stille #1 感谢。查看了下 frp 的文档,vhost 貌似也需要服务器同时配置。但这个 frp 是公用的。
我想尝试从 Nginx 上解决问题。最终达到内网用户可以直接通过 Nginx 的 80 端口进行访问;而外网可以通过 Frp 的 tcp 方式进行访问。
gam2046
2022-05-14 21:56:12 +08:00
将 nginx 代理后的应用 Listen port 变更为 18080 ,并且修改 nginx 内的 proxy_pass 即可。
zunsgm
2022-05-15 00:46:43 +08:00
@gam2046 #3 感谢,这是一个很棒的思路,我尝试下。
coolloves
2022-05-15 09:50:33 +08:00
proxy_set_header Host $host:$server_port;
改为
proxy_set_header Host $http_host;
zunsgm
2022-05-16 15:09:02 +08:00
@coolloves #5 Nice ! 已测试,改为$http_host 可以解决问题也很方便。感谢大佬指点!!

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/852818

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX