为 Node.js 个应用配置 Nginx 反向代理

Configuring Nginx Reverse Proxy for Node.js app

我有一个 Express 应用程序,服务器侦听 3000 端口。它托管在 VPS、Ubuntu 上。当我在浏览器中打开 {VPS server ip}:3000 时,一切正常。但现在我想访问 example.com 上的应用程序,那是我的域名。 我已经安装了 Nginx,启用了 UFW。我编辑了文件/etc/nginx/nginx.conf,现在它的内容是

user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

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 TLSv1.3; # 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_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/*;
    server {
        listen 80;
        server_name example.com[-->my real domain here<--];
    
        location / {
            proxy_set_header   X-Forwarded-For $remote_addr;
            proxy_set_header   Host $http_host;
            proxy_pass         http://[-->my real server ip here<--]:3000;
        }
    }
}


#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;
#   }
#}


nginx -t 表示语法没问题。那么systemctl restart nginx,就激活了。 当我在浏览器中打开 example.com 时,我的应用程序不可用。 我应该检查什么?我应该编辑或重新启动或激活什么?谢谢!

您需要在 nginx 文件夹 ( /etc/nginx/sites-enabled/ ) 的 sites-enabled 文件夹中创建一个文件。

并添加监听请求的代理配置 post 80(如果您有 SSL 证书,则为 443)并将其代理传递给 http://127.0.0.1:3000/

然后尝试运行 nginx -t重启nginx服务器。

希望对你有帮助!!

所以,我的经验可能会有用。 我的 nginx.conf 的 http 部分是

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 TLSv1.3; # 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_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/*;
    
}

显然,行

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

完成所有工作。 /sites-available 文件夹中的单个文件名称为 'myserver.config',其完整内容为

#The Nginx server instance
server{
        listen 80;
        server_name example.com;
        location / {
        proxy_pass http://myIP:3000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;       
    }
}

我删除了我在澄清过程中创建的 /sites-enabled 中的所有符号链接,并使用命令创建了一个新链接 sudo ln -s /etc/nginx/sites-available/myserver.config /etc/nginx/sites-enabled/

行得通。 这不是什么新东西,但它证实了现有的说明确实可行。