express - 将本地 nginx 服务器部署到公共(public) ubuntu 16.04

标签 express nginx reverse-proxy ubuntu-16.04 ufw

我正在尝试 部署我的本地 nginx 服务器 对公众。 nginx 服务器作为 运行反向代理 给我的节点 express 应用 也在运行本地端口 3000 .因此,我创建了一个从/etc/nginx/sites-available/express 到/etc/nginx/sites-enabled/express 的符号链接(symbolic link),所以我的配置文件称为 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)从公共(public)访问服务器时,我无法访问服务器。我到底错过了什么?

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

最佳答案

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

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

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

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

在这种情况下,公共(public) IP 将分配给路由器,所有其他设备将具有本地私有(private) ip,例如 192.168.x.x/24/10.x.x.x/8/172.16.0.0/20
您需要从路由器配置端口转发到服务器的私有(private) ip。根据路由器的供应商,此功能可能称为 virtual server左右,通常在 WAN 配置附近的某个地方找到。将其设置为将 TCP 端口 80 转发到服务器本地端口 80,对于 443 也是如此。

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

关于express - 将本地 nginx 服务器部署到公共(public) ubuntu 16.04,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39164039/

相关文章:

nginx - 更改 Nginx 自动索引的排序

ruby-on-rails - 为 Rails 配置 nginx.conf 显示文件目录而不是索引

azure - 使用 Azure 应用程序网关修改/截断基于路径的路由中的路径

go - goapp systemd nginx之间的上游超时

php - 为 nginx 和 php-fpm 容器将 docker-compose 转换为 Kubernetes

javascript - 从控制台输出实时解析进度 - NodeJS

node.js - Node.js-如何接收和解析multipart/formData作为具有字段和文件的对象数组?

javascript - 如何解析通过 ajax 请求发送的多部分/表单数据。?

java - NGINX反向代理背后的SpringBoot API REST

javascript - 如何使用JS、Node、Mongo、Express提交表单?