gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 14:21:46 37 4
gpt4 key购买 nike

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

例如

  • /网站根目录的静态服务文件
  • /app/使用 Nginx 反向代理在端口 3000 上运行的 Node 应用
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.3Nginx: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"

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

提前致谢!

最佳答案

这可能是因为你的 Node 路由器的原因。最好也共享 Node 代码。无论如何,尝试将主路由器和静态路由设置为 app.use('/app', mainRouter); 并查看它是否有意义?

关于node.js - 运行 nginx 来提供文件并充当同一域上 Node 应用程序的反向代理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60139257/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com