gpt4 book ai didi

node.js - Nginx + Node JS + Express JS 产生 502 错误

转载 作者:太空宇宙 更新时间:2023-11-04 01:29:48 26 4
gpt4 key购买 nike

我现在已经尝试了两天来让我的服务器运行......我想我已经有大约 30 个不同的 nginx 配置并尝试了不同的端口和一切。我现在很沮丧,因为没有一个人在做设置 nginx + Node 服务器的教程时提到过错误,一切似乎都很简单,但显然事实并非如此......

我让 nginx 服务器以其标准配置运行:

# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
worker_connections 1024;
}

http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';

access_log /var/log/nginx/access.log main;

sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 4096;

include /etc/nginx/mime.types;
default_type application/octet-stream;

# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;

server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/html;

# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;

location / {
}

error_page 404 /404.html;
location = /40x.html {
}

error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}

# Settings for a TLS enabled server.
#
# server {
# listen 443 ssl http2 default_server;
# listen [::]:443 ssl http2 default_server;
# server_name _;
# root /usr/share/nginx/html;
#
# ssl_certificate "/etc/pki/nginx/server.crt";
# ssl_certificate_key "/etc/pki/nginx/private/server.key";
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 10m;
# ssl_ciphers PROFILE=SYSTEM;
# ssl_prefer_server_ciphers on;
#
# # Load configuration files for the default server block.
# include /etc/nginx/default.d/*.conf;
#
# location / {
# }
#
# error_page 404 /404.html;
# location = /40x.html {
# }
#
# error_page 500 502 503 504 /50x.html;
# location = /50x.html {
# }
# }

}

哪个服务器成功为我提供了默认测试页面...

这是我通过 pm2 运行的 server.js:

var express = require('express'),
path = require('path'),
app = express();

//set the port
app.set('port', 3000);

//get node files i need
app.use('/fa', express.static(__dirname + '/node_modules/@fortawesome/fontawesome-free/'));

//tell express that we want to use the www folder
//for our static assets
app.use(express.static(path.join(__dirname, 'www')));

// Listen for requests
var server = app.listen(app.get('port'), function () {
console.log('The server is running on http://localhost:' + app.get('port'));
});

所以我测试的最后一个配置是原始的,只是将位置 block 替换为:

location / {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}

但我也测试了非常简单的配置,例如:

events {
worker_connections 1024;
}

http {
server {
listen 80;

server_name _;

location / {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
}

我只是不知道出了什么问题......

通过 curl http://localhost:8080我收到了我的 html,但没有收到我给定的服务器 IP...

netstat 给了我这个:

netstat -tulpn | grep :80
tcp 0 0 0.0.0.0:80 0.0.0.0:*
LISTEN 14841/nginx: master
tcp6 0 0 :::80 :::*
LISTEN 14841/nginx: master

netstat -tulpn | grep :3000
tcp 0 0 127.0.0.1:3000 0.0.0.0:*
LISTEN 14375/node /home/no

顺便说一句:我一如既往地通过 systemctl 运行 fedora 30 和 nginx,并通过 pm2 start server.js 作为非 root 用户运行 pm2。我也使用 UFW 并禁用 firwalld,但是 ufw 中允许 WWW...

我真的很感谢你们给我的所有帮助和建议。

我根本不知道该怎么办:(...

最佳答案

好吧,我现在明白了。这是 SELinux 规则错误。拒绝我通过 http 连接到我的服务器。

setsebool httpd_can_network_connect on -P

永久修复了它。这篇文章回答了我的问题:

(13: Permission denied) while connecting to upstream:[nginx]

您好,乔什。

如果您愿意,可以查看该页面,并警告其内容超过 18 岁:D我知道 IO 设备存在 Canvas 错误,但在那里不起作用。由于 Assets 很重,我不建议您使用有限的数据计划(大约 70Mb 的 3D Assets )。如果它有效或无效,请与我联系(这只是一个更大的实验:))。IP为:140.82.47.236

关于node.js - Nginx + Node JS + Express JS 产生 502 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56479873/

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