Express(Node.js)+Nginx环境下应用无法在3000端口运行求助
问题描述
我正按照教程搭建一个测试应用,相关代码如下:
app.js
var app = require('express')(); var http = require('http').Server(app); app.get('/', function(req, res) { res.sendfile('index.html'); }); http.listen(3000, function() { console.log('listening on *:3000'); });
index.html
<!DOCTYPE html> <html> <head> <title>Hello world</title> </head> <body>Hello world</body> </html>
package.json
{ "name": "html", "version": "1.0.0", "description": "My project", "main": "app.js", "dependencies": { "express": "^4.18.2", "socket.io": "^4.6.2" }, "devDependencies": {}, "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "author": "Me", "license": "ISC" }
使用nodemon app.js启动应用后,服务端运行正常,但在浏览器中访问server_ip:3000时,显示“连接尝试失败”。此前在搭载Apache的服务器上运行类似应用一切正常,怀疑问题出在Nginx上。我的操作系统为CentOS 7,以下是我的/etc/nginx/nginx.conf配置:
# 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; listen [::]:80; server_name 你的服务器IP; root /var/www/html; # Load configuration files for the default server block. include /etc/nginx/default.d/*.conf; location / { root /var/www/html; index index.php index.html index.htm; } error_page 404 /404.html; location = /404.html { root /var/www/html; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /var/www/html; } location ~ .php$ { root /var/www/html; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } } # Settings for a TLS enabled server. # # server { # listen 443 ssl http2; # listen [::]:443 ssl http2; # server_name _; # root /var/www/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 HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # # # Load configuration files for the default server block. # include /etc/nginx/default.d/*.conf; # # error_page 404 /404.html; # location = /40x.html { # } # # error_page 500 502 503 504 /50x.html; # location = /50x.html { # } # } }
恳请告知如何解决该问题,使应用正常运行。
解决方案
一、开放3000端口(直接访问用)
如果要直接通过server_ip:3000访问应用,CentOS7默认的firewalld防火墙大概率拦截了该端口的外部请求,执行以下命令开放:
# 临时开放端口,重启服务器后失效 firewall-cmd --add-port=3000/tcp # 永久开放端口,需重启防火墙生效 firewall-cmd --add-port=3000/tcp --permanent firewall-cmd --reload
完成后再次尝试访问server_ip:3000,若仍无法连接,继续配置Nginx反向代理。
二、配置Nginx反向代理(推荐通过80端口访问)
直接暴露应用端口存在安全风险,更推荐用Nginx做反向代理,通过默认的80端口访问。修改Nginx配置文件/etc/nginx/nginx.conf中的server块:
server { listen 80; listen [::]:80; server_name 你的服务器IP; include /etc/nginx/default.d/*.conf; # 核心反向代理配置,将请求转发到本地3000端口的Express应用 location / { proxy_pass http://127.0.0.1:3000; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } # 保留原有错误页配置(如果需要PHP服务,也保留对应的location规则) error_page 404 /404.html; location = /404.html { root /var/www/html; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /var/www/html; } location ~ .php$ { root /var/www/html; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }
修改完成后,先验证配置合法性:
nginx -t
如果输出nginx: configuration file /etc/nginx/nginx.conf test is successful,则重启Nginx生效:
systemctl restart nginx
现在直接访问server_ip(无需加3000端口)即可看到你的Express应用。
三、额外检查项
- 确认Express应用监听地址:代码中
http.listen(3000, ...)默认监听所有网卡(0.0.0.0),若不确定可改为http.listen(3000, '0.0.0.0', function() {...})明确指定。 - 检查应用进程状态:执行
ps aux | grep node,确认nodemon或node进程正常运行。
内容的提问来源于stack exchange,提问作者Donna Lizard
相关产品推荐
相关产品推荐

