【问题标题】:deploy local nginx server to public ubuntu 16.04将本地 nginx 服务器部署到公共 ubuntu 16.04
【发布时间】:2017-01-02 23:50:07
【问题描述】:

我正在尝试向公众部署我的本地 nginx 服务器。 nginx 服务器作为 反向代理 运行到我的 node express 应用程序,该应用程序也在 本地端口 3000 上运行。因此,我创建了一个从 /etc/nginx/sites-available/express 到 /etc/nginx/sites-enabled/express 的符号链接,因此我的配置文件称为 express,如下所示。

/etc/nginx/sites-enabled/express

upstream express_servers{
    server 127.0.0.1:3000;
}

server {

    listen 80;

        location / {
        proxy_pass http://express_servers;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
        }

}

我已经从启用站点的文件夹中删除了默认文件,并且我没有更改我的 nginx.conf 文件,看起来像这样

/etc/nginx/nginx.conf

user www-data;
worker_processes auto;
pid /run/nginx.pid;

events {
    worker_connections 768;
    # multi_accept on;
}

http {

    ##
    # Basic Settings
    ##

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    # server_tokens off;

    # server_names_hash_bucket_size 64;
    # server_name_in_redirect off;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    ##
    # SSL Settings
    ##

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
    ssl_prefer_server_ciphers on;

    ##
    # Logging Settings
    ##

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    ##
    # Gzip Settings
    ##

    gzip on;
    gzip_disable "msie6";

    # gzip_vary on;
    # gzip_proxied any;
    # gzip_comp_level 6;
    # gzip_buffers 16 8k;
    # gzip_http_version 1.1;
    # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

    ##
    # Virtual Host Configs
    ##

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}


#mail {
#   # See sample authentication script at:
#   # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
# 
#   # auth_http localhost/auth.php;
#   # pop3_capabilities "TOP" "USER";
#   # imap_capabilities "IMAP4rev1" "UIDPLUS";
# 
#   server {
#       listen     localhost:110;
#       protocol   pop3;
#       proxy      on;
#   }
# 
#   server {
#       listen     localhost:143;
#       protocol   imap;
#       proxy      on;
#   }
#}

我还使用 ufw(简单防火墙)更改了我的防火墙设置,以允许 http 访问(尤其是 nginx)。我的 ufw 状态如下所示:

Status: active
Logging: on (low)
Default: deny (incoming), allow (outgoing), disabled (routed)
New profiles: skip

    To                         Action      From
--                         ------      ----
80/tcp (Nginx HTTP)        ALLOW IN    Anywhere                  
80                         ALLOW IN    Anywhere                  
80/tcp (Nginx HTTP (v6))   ALLOW IN    Anywhere (v6)             
80 (v6)                    ALLOW IN    Anywhere (v6) 

当我使用 wrk 或 loadtest(npm) 运行负载测试时,一切似乎都运行良好。例如

wrk -t12 -c50 -d5s http://192.168.178.57/getCats/eng

所以在本地我可以访问 nginx 服务器,但是当我尝试使用我的手机(3G/4G)从公共访问服务器时,我无法访问服务器。我到底错过了什么?

编辑:我正在尝试通过 http://PUBLIC_IP_ADDR/getCats/eng 访问服务,而不是本地地址。

【问题讨论】:

  • 192.168.0.0/16 在私有局域网地址空间中。因此您无法通过地窖网络访问您的网站。更多信息:en.wikipedia.org/wiki/Private_network。换句话说:联系您的 ISP,购买我发布的 IP,或将您的网站托管在诸如 aws ec2 之类的托管服务中。
  • 我只是使用私有局域网地址来进行本地负载测试....实际上我正在使用我的公共 IP 地址。所以你的意思是我实际上需要购买许可证才能从我的本地机器上发布服务?????? AWS EC2 是我的 B 计划,但首先我想尝试从我的本地机器部署服务...
  • 您必须通过手机访问您的网站,方法是访问http://PUBLIC_IP_ADDRESS/getCats/eng:D
  • 我已经这样做了。我编辑了帖子,抱歉之前如此不准确......
  • 看看您的互联网接入设备/路由器。您的路由器可能没有打开端口,或者没有将静态 IP 转发到您的计算机,或者 NAT 已关闭。您的 ISP 可能也不允许传入流量。

标签: express nginx reverse-proxy ubuntu-16.04 ufw


【解决方案1】:

我认为你必须把

listen *:80

在您的文件 /etc/nginx/sites-enabled/express 中

nginx listen doc

我认为它没有像你现在拥有的那样监听来自你的 ISP 公共 IP 的请求。

【讨论】:

  • 本以为星号是多余的,试了下还是没有成功。
【解决方案2】:

您的 nginx 配置看起来非常好。

为了能够从外部访问您的服务器,您需要一个来自您的 ISP 的公共静态 IP。此外,ISP 不应阻止传入端口 80 和 443 的流量(以防您决定使用 https)。

那么你可能有这样的局域网:

ISP <---> Router <---> Server
             ^
             |
             ----> your other devices

在这种情况下,公共 IP 将分配给路由器,所有其他设备将具有本地私有 ip,例如 192.168.x.x/24/10.x.x.x/8/172.16.0.0/20

您需要配置从路由器到服务器私有 IP 的端口转发。根据路由器的供应商,此功能可能称为virtual server 左右,通常在 WAN 配置附近的某个地方找到。将其设置为将 TCP 端口 80 转发到服务器本地端口 80 和 443 相同。

另外,您可能需要将服务器配置为静态 ip,以便本地 ip 地址不会改变

【讨论】:

  • 您的解决方案乍一看似乎是正确的,但看起来我的路由器正在使用 DS-Lite 隧道,因此外部连接存在问题。我什至无法通过 ipv6 地址访问服务器:http://[IPV6]/getCats/eng。我更改了路由器上的端口设置以允许通过 http 访问。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-11
  • 2014-09-06
相关资源
最近更新 更多