简体   繁体   English

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

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

I am currently trying to run Nginx as a reverse proxy for a small Node application and serve up files for the core of a site.我目前正在尝试将 Nginx 作为小型 Node 应用程序的反向代理运行,并为站点的核心提供文件。

Eg例如

  • / Statically served files for root of website / 网站根目录的静态文件
  • /app/ Node app running on port 3000 with Nginx reverse proxy /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;
        }
}

This appeared to work at first glance, but what I have found over time is that I am being served 502 errors constantly on the Node app route.乍一看,这似乎有效,但随着时间的推移,我发现我在 Node 应用程序路由上不断收到 502 错误。 This applies to both the app itself, as well as static assets included in the app.这既适用于应用程序本身,也适用于应用程序中包含的静态资产。

I've tried using various different variations of the above config, but nothing I can find seems to fix the issue.我尝试使用上述配置的各种不同变体,但我找不到任何似乎可以解决问题的方法。 I had read of issues with SELinux, but this is currently not on the server in question.我读过有关 SELinux 的问题,但这目前不在相关服务器上。

Few additional bits of information;很少有额外的信息; Server: Ubuntu 18.04.3 Nginx: nginx/1.17.5服务器: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"

Has anyone encountered similar issues, or knows what it is that I've done wrong?有没有人遇到过类似的问题,或者知道我做错了什么?

Thanks in advance!提前致谢!

It's may be because of your node router.It's better to share nodes code too.这可能是因为你的节点路由器。最好也共享节点代码。 Anyway try put your main router and static route like app.use('/app', mainRouter);无论如何尝试把你的主路由器和静态路由像app.use('/app', mainRouter); and see it make any sense?看看它有什么意义?

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

相关问题 使用Nginx反向代理隐藏一个域上的Node应用程序端口号 - Hiding Node app port numbers on one domain with nginx reverse proxy 如何使用多个 Express 应用程序 + NGINX 作为反向代理服务器提供 static 文件(CSS,...) - How to serve static files (CSS, …) with multiples Express app + NGINX as reverse proxy server 使用Nginx作为节点反向代理来服务根静态文件 - Serve root static file with nginx as node reverse proxy 使用反向代理 Nginx 服务反应创建应用程序和 Nodejs 应用程序 - serve react create app and Nodejs app with reverse proxy Nginx 配置 Nginx 为 Angular 的 Static 文件提供服务,并为 express 提供反向代理 - Configure Nginx to serve Static files of Angular and also reverse proxy for express 如何在 CentOS 上使用 Nginx 反向代理到域子文件夹中的 Node.js 应用程序? - How to reverse proxy to a Node.js app in a domain subfolder with Nginx on CentOS? 用于节点服务器的 Nginx 反向代理为 css/js 文件返回 404 - Nginx Reverse Proxy for node server returning 404 for css/js files NGINX反向代理到运行Web应用程序的Docker容器 - NGINX reverse proxy to docker container running web app Nginx反向代理服务Node.js应用程序静态文件 - Nginx reverse proxy Serving Node.js app static file 为 Node.js 应用程序配置 Nginx 反向代理 - Configuring Nginx Reverse Proxy for Node.js app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM