一个 Node.js 部署的常见错误,但我就是搜不到解决方案,搞不定,求高人指点

2017-12-12 01:45:24 +08:00
 ericgui
## 我把 log 在 google 上搜了一下,很多人描述这个问题,我也尝试了 2 天了,都搞不定,特来求助

## 环境:
- Node.js v8.9.1
- ExpressJS
- Ubuntu 16.04 LTS
- pm2

## 错误
- 描述 1:如果短时间内多次刷新首页,出现 502 Bad Gateway 错误
- 描述 2:代码里,用户注册或登陆成功后会自动跳转到首页,在本机 OK,但部署到服务器之后,跳转出现 502 Bad Gateway

## 配置:
### Nginx 配置文件:

1 upstream helloworld {
2 server <server-ip-address>:3000 max_fails=0 fail_timeout=10s weight=1;
3 ip_hash;
4 keepalive 512;
5 }
6
7 server {
8 listen 80;
9 server_name helloworld.com www.helloworld.com;
10
11 #charset koi8-r;
12 access_log /app/archives/logs/nginx/helloworld .host.access.log main;
13 error_log /app/archives/logs/nginx/helloworld .host.error.log debug;
14
15 root /app/helloworld ;
16
17 location / {
18 proxy_pass http://helloworld;
19 proxy_set_header X-Real-IP $remote_addr;
20 proxy_set_header X-Forwarded-Proto $scheme;
21 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
22 proxy_set_header Host $host;
23 proxy_set_header X-NginX-Proxy true;
24 proxy_http_version 1.1;
25 proxy_set_header Upgrade $http_upgrade;
26 proxy_set_header Connection 'upgrade';
27 proxy_set_header Host $host;
28 proxy_cache_bypass $http_upgrade;
29
30 }
31 }

## error log
2017/12/11 09:22:23 [error] 29952#29952: *81 connect() failed (111: Connection refused) while connecting to upstream, client: 169.228.200.160, server: helloworld.com, request: "GET / HTTP/1.1", upstream: "http://<server-ip-address>:3000/", host: "www.helloworld.com", referrer: "http://www.helloworld.com/user/signin"
5316 次点击
所在节点    NGINX
22 条回复
ericgui
2017-12-13 01:41:41 +08:00
@tlday 是的,我把 pm2 一停,就用 npm start,一切都 OK 了。
ericgui
2017-12-13 01:46:02 +08:00
@tlday 但只用 npm start 也很原始,而且 log 都输出到 console 来了。最好还是用 pm2 的吧?否则怎么自动重启呢?

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/413940

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX