[英]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例如
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.