nginx 反向代理无法在 http 上工作 [英] nginx reverse proxy not working on http

查看:66
本文介绍了nginx 反向代理无法在 http 上工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过来自外部 IP 的 nginx 反向代理访问位于 http://localhost:4000 的网站地址(不在我的网络上)

I wanted to access my website available at http://localhost:4000, via nginx reverse proxy from external IP address(not on my network)

我在家用路由器上启用了 TCP 端口转发,并将端口 80 转发到端口 80.

I have enabled TCP port forwarding on my home router and forwarding port 80 to port 80.

当我尝试以 http://my.public.iphttp://my.public.ip:80,页面无法加载.(我确实尝试在路由器上转发实际端口 4000 和 http://my.public.ip:4000, 网站加载正常)

When I try to access my website as http://my.public.ip OR http://my.public.ip:80, page doesn't load. (I did try forwarding actual port 4000 on router and http://my.public.ip:4000, site loaded fine)

可能缺少什么.

以下是我的 ngnix 配置:

Below is my ngnix conf:

#user  nobody;
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    send_timeout 1800;
    sendfile        on;
    keepalive_timeout  6500;
    server {
        listen  80;
        location / {
          proxy_pass          http://localhost:4000;
          proxy_set_header    Host             $host;
          proxy_set_header    X-Real-IP        $remote_addr;
          proxy_set_header    X-Forwarded-For  $proxy_add_x_forwarded_for;
          proxy_set_header    X-Client-Verify  SUCCESS;
          proxy_set_header    X-Client-DN      $ssl_client_s_dn;
          proxy_set_header    X-SSL-Subject    $ssl_client_s_dn;
          proxy_set_header    X-SSL-Issuer     $ssl_client_i_dn;
          proxy_read_timeout 1800;
          proxy_connect_timeout 1800;
        }
    }
    # HTTPS server

    server {
        listen       443;
        server_name  localhost;

        ssl                  on;
        ssl_certificate      server.crt;
        ssl_certificate_key  server.key;
        ssl_session_timeout  5m;

        ssl_protocols  SSLv2 SSLv3 TLSv1;
        ssl_ciphers  ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
        ssl_prefer_server_ciphers   on;

        location / {
          proxy_pass          http://localhost:3000;
          proxy_set_header    Host             $host;
          proxy_set_header    X-Real-IP        $remote_addr;
          proxy_set_header    X-Forwarded-For  $proxy_add_x_forwarded_for;
          proxy_set_header    X-Client-Verify  SUCCESS;
          proxy_set_header    X-Client-DN      $ssl_client_s_dn;
          proxy_set_header    X-SSL-Subject    $ssl_client_s_dn;
          proxy_set_header    X-SSL-Issuer     $ssl_client_i_dn;
          proxy_read_timeout 1800;
          proxy_connect_timeout 1800;
        }
    }
}

推荐答案

如果你想接受IP地址而不是域名,你可以像这样定义默认的server_name:

If you want to accept with IP address instead of domain name, you can define default server_name like this:

...
http {
    ...
    server {
        listen  80;
        server_name _ ;
        location / {
            ...
        }
    }
    ....
}

这篇关于nginx 反向代理无法在 http 上工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆