zunsgm
V2EX  ›  问与答

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

  •  
  •   zunsgm · May 14, 2022 · 1473 views
    This topic created in 1488 days ago, the information mentioned may be changed or developed.

    环境:

    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 后返回的端口还是不正确。

    6 replies    2022-05-16 15:09:02 +08:00
    stille
        1
    stille  
       May 14, 2022
    试试 frp 用 http 模式,在 frps 里设置 vhost_http_port = 18080
    zunsgm
        2
    zunsgm  
    OP
       May 14, 2022
    @stille #1 感谢。查看了下 frp 的文档,vhost 貌似也需要服务器同时配置。但这个 frp 是公用的。
    我想尝试从 Nginx 上解决问题。最终达到内网用户可以直接通过 Nginx 的 80 端口进行访问;而外网可以通过 Frp 的 tcp 方式进行访问。
    ntdll
        3
    ntdll  
       May 14, 2022
    将 nginx 代理后的应用 Listen port 变更为 18080 ,并且修改 nginx 内的 proxy_pass 即可。
    zunsgm
        4
    zunsgm  
    OP
       May 15, 2022
    @gam2046 #3 感谢,这是一个很棒的思路,我尝试下。
    sparkssssssss
        5
    sparkssssssss  
       May 15, 2022
    proxy_set_header Host $host:$server_port;
    改为
    proxy_set_header Host $http_host;
    zunsgm
        6
    zunsgm  
    OP
       May 16, 2022
    @coolloves #5 Nice ! 已测试,改为$http_host 可以解决问题也很方便。感谢大佬指点!!
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3093 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 46ms · UTC 14:19 · PVG 22:19 · LAX 07:19 · JFK 10:19
    ♥ Do have faith in what you're doing.