V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
NGINX
NGINX Trac
3rd Party Modules
Security Advisories
CHANGES
OpenResty
ngx_lua
Tengine
在线学习资源
NGINX 开发从入门到精通
NGINX Modules
ngx_echo
Raul7
V2EX  ›  NGINX

请教个 nginx 端口转发的问题

  •  1
     
  •   Raul7 · 2020-04-28 14:03:28 +08:00 · 2850 次点击
    这是一个创建于 1431 天前的主题,其中的信息可能已经有所发展或是发生改变。

    机器 A 在异地,我本地无法直接连接,机器 A 上部署了个服务,64297 端口了 nginx 代理,开启了 ssl 、auth 等。相关配置如下:

    server {
    
        #########################
        ### Basic server settings
        #########################
        listen 64297 ssl http2;
        index tpotweb.html;
        ssl_protocols TLSv1.3;
        server_name example.com;
        error_page 300 301 302 400 401 402 403 404 500 501 502 503 504 /error.html;
    
    
        ##############################################
        ### Remove version number add different header
        ##############################################
        server_tokens off;
        more_set_headers 'Server: apache';
    
    
        ##############################################
        ### SSL settings and Cipher Suites
        ##############################################
        ssl_certificate /etc/nginx/cert/nginx.crt;
        ssl_certificate_key /etc/nginx/cert/nginx.key;
        
        ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH:!DHE:!SHA:!SHA256';
        ssl_ecdh_curve secp384r1;
        ssl_dhparam /etc/nginx/ssl/dhparam4096.pem;
    
        ssl_prefer_server_ciphers on;
        ssl_session_cache shared:SSL:10m;
    
    
        ####################################
        ### OWASP recommendations / settings
        ####################################
    
        ### Size Limits & Buffer Overflows 
        ### the size may be configured based on the needs. 
        client_body_buffer_size  128k;
        client_header_buffer_size 1k;
        client_max_body_size 256k;
        large_client_header_buffers 2 1k;
    
        ### Mitigate Slow HHTP DoS Attack
        ### Timeouts definition ##
        client_body_timeout   10;
        client_header_timeout 10;
        keepalive_timeout     5 5;
        send_timeout          10;
    
        ### X-Frame-Options is to prevent from clickJacking attack
        add_header X-Frame-Options SAMEORIGIN;
    
        ### disable content-type sniffing on some browsers.
        add_header X-Content-Type-Options nosniff;
    
        ### This header enables the Cross-site scripting (XSS) filter
        add_header X-XSS-Protection "1; mode=block";
    
        ### This will enforce HTTP browsing into HTTPS and avoid ssl stripping attack
        add_header Strict-Transport-Security "max-age=31536000; includeSubdomains;";
    
    
        ##################################
        ### Restrict access and basic auth
        ##################################
    
        # satisfy all;
        satisfy any;
    
        allow 127.0.0.1;
        allow ::1;
        deny  all;
    
        auth_basic           "closed site";
        auth_basic_user_file /etc/nginx/nginxpasswd;
    
    
        #################
        ### Proxied sites
        #################
    
        ### Kibana
        location /kibana/ {
            proxy_pass http://127.0.0.1:64296;
            rewrite /kibana/(.*)$ /$1 break;
        }
    
        ### ES 
        location /es/ {
            proxy_pass http://127.0.0.1:64298/;
            rewrite /es/(.*)$ /$1 break;
        }
    
        ### head standalone 
        location /myhead/ {
            proxy_pass http://127.0.0.1:64302/;
            rewrite /myhead/(.*)$ /$1 break;
        }
    
        ### CyberChef
        location /cyberchef {
            proxy_pass http://127.0.0.1:64299;
            rewrite ^/cyberchef(.*)$ /$1 break;
        }
    
        ### spiderfoot
        location /spiderfoot {
            proxy_pass http://127.0.0.1:64303;
        }
    
            location /static {
                proxy_pass http://127.0.0.1:64303/spiderfoot/static;
            }
    
            location /scanviz {
                proxy_pass http://127.0.0.1:64303/spiderfoot/scanviz;
            }
            
            location /scandelete {
                proxy_pass http://127.0.0.1:64303/spiderfoot/scandelete;
            }
    
    }
    

    我这边有台可以连接的 B 机器,可以通这个 A 机器。 我想访问 A 机器的 64297 端口,故想通过 B 机器做中转,将 A 机器的 64297 端口代理到 B 机器上,B 机器 nginx 配置如下:

    
        server {
            listen 64297;
          
             
            location / {
                # x.x.x.x 为 A 机器 ip
                proxy_pass https://x.x.x.x:64297/;
            }
    
        }
    
    

    但是访问 B 机器的 64297 端口,一直报 502 。

    请教下大家,该如何解决?

    7 条回复    2020-04-28 22:14:06 +08:00
    jmyz0455
        1
    jmyz0455  
       2020-04-28 14:36:50 +08:00
    光是说 502 范围挺广的,只能够叫你测试清楚 B 是不是真的能连接 A 。
    如果有日志会好判断一些,建议你 A B 的配置都记录一下 log,看看是哪边的问题。
    MonoLogueChi
        2
    MonoLogueChi  
       2020-04-28 15:05:21 +08:00 via Android
    你 A 机器上开了 ssl,从 B 机器反代访问 A 机器的时候,就要设置 host 头了
    silencexxx
        3
    silencexxx  
       2020-04-28 15:37:41 +08:00
    为啥非用 NGINX 做中转流量? iptables 实现端口映射不好吗?
    PriestTomb
        4
    PriestTomb  
       2020-04-28 17:22:46 +08:00
    想起前不久 V 站看到的帖子,试试 B 机器 Nginx 的 proxy_pass 里 ip:port 后面的斜杠去掉。。
    qwerthhusn
        5
    qwerthhusn  
       2020-04-28 17:29:37 +08:00
    proxy_pass https://x.x.x.x:64297/;
    不知道跟信任证书有没有关系,ip 的话肯定是不信的
    cnleon
        6
    cnleon  
       2020-04-28 21:07:51 +08:00
    用 stream 吧,你这个 https 就过不了。
    uxff
        7
    uxff  
       2020-04-28 22:14:06 +08:00
    转给 A,A 是 ssl 必须设置 host 头
    proxy_set_header Host $http_host;
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5167 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 09:30 · PVG 17:30 · LAX 02:30 · JFK 05:30
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.