繁体   English   中英

运行 nginx 来提供文件并充当同一域上 Node 应用程序的反向代理

[英]Running nginx to serve files and act as a reverse proxy for Node app on same domain

我目前正在尝试将 Nginx 作为小型 Node 应用程序的反向代理运行,并为站点的核心提供文件。

例如

  • / 网站根目录的静态文件
  • /app/ 使用 Nginx 反向代理在端口 3000 上运行的节点应用程序
server {
        listen 80;
        listen [::]:80;
        server_name  example.com www.example.com;

        root /var/www/example.com/html;
        index index.html index.htm;

        # Set path for access_logs
        access_log /var/log/nginx/access.example.com.log combined;

        # Set path for error logs
        error_log /var/log/nginx/error.example.com.log notice;

        # If set to on, Nginx will issue log messages for every operation
        # performed by the rewrite engine at the notice error level
        # Default value off
        rewrite_log on;

        # Settings for main website
        location / {
                try_files $uri $uri/ =404;
        }

        # Settings for Node app service
        location /app/ {
                # Header settings for application behind proxy
                proxy_set_header Host $host;

                # proxy_set_header X-NginX-Proxy true;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "upgrade";

                # Proxy pass settings
                proxy_pass http://127.0.0.1:3000/;

                # Proxy redirect settings
                proxy_redirect off;

                # HTTP version settings
                proxy_http_version 1.1;

                # Response buffering from proxied server default 1024m
                proxy_max_temp_file_size 0;

                # Proxy cache bypass define conditions under the response will not be taken from cache
                proxy_cache_bypass $http_upgrade;
        }
}

乍一看,这似乎有效,但随着时间的推移,我发现我在 Node 应用程序路由上不断收到 502 错误。 这既适用于应用程序本身,也适用于应用程序中包含的静态资产。

我尝试使用上述配置的各种不同变体,但我找不到任何似乎可以解决问题的方法。 我读过有关 SELinux 的问题,但这目前不在相关服务器上。

很少有额外的信息; 服务器:Ubuntu 18.04.3 Nginx:nginx/1.17.5

2020/02/09 18:18:07 [error] 8611#8611: *44 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: x, server: example.com, request: "GET /app/assets/images/image.png HTTP/1.1", upstream: "http://127.0.0.1:3000/assets/images/image.png", host: "example.com", referrer: "http://example.com/overlay/"
2020/02/09 18:18:08 [error] 8611#8611: *46 connect() failed (111: Connection refused) while connecting to upstream, client: x, server: example.com, request: "GET /app/ HTTP/1.1", upstream: "http://127.0.0.1:3000/", host: "example.com"

有没有人遇到过类似的问题,或者知道我做错了什么?

提前致谢!

这可能是因为你的节点路由器。最好也共享节点代码。 无论如何尝试把你的主路由器和静态路由像app.use('/app', mainRouter); 看看它有什么意义?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM