使用express写了后端api(监听的是3000端口),在主机上使用pm2启动并watch服务(监听3000端口),使用Nginx做服务器,并做端口转发,页面和接口都没问题。可是,随着时间的推移,过了几个小时,页面就会报以下错: 我以为是服务挂了,可是: ![5%GF93U%N13K)ROD(3XNWB.png 看了下,3000端口,处于监听状态,然后pm2也是好着。但是,我重启下pm2,就又恢复正常了,也不会报错了,过那么几个小时,又是504错误。。。大神们,help me。
这里贴上我的Nginx关键的配置,不知道,是Nginx配置问题,还是pm2的问题,还是我的代码自身的问题。
那你直接通过ip加端口访问看看是不是正常,正常就可以先排除nginx
通过ip加端口访问正常的哦 我附上pm2 log: PM2 | 2017-05-11 18:17:09: Application log path : /root/.pm2/logs PM2 | 2017-05-11 18:17:09: Process dump file : /root/.pm2/dump.pm2 PM2 | 2017-05-11 18:17:09: Concurrent actions : 2 PM2 | 2017-05-11 18:17:09: SIGTERM timeout : 1600 PM2 | 2017-05-11 18:17:09: =============================================================================== PM2 | 2017-05-11 18:17:09: [Watch] Start watching 0 PM2 | 2017-05-11 18:17:09: Starting execution sequence in -fork mode- for app name:index id:0 PM2 | 2017-05-11 18:17:09: App name:index id:0 online PM2 | 2017-05-11 22:08:16: [Watch] Stop watching 0 PM2 | 2017-05-11 22:08:16: Stopping app:index id:0 PM2 | 2017-05-11 22:08:16: App [index] with id [0] and pid [2399], exited with code [0] via signal [SIGINT] PM2 | 2017-05-11 22:08:16: pid=2399 msg=process killed PM2 | 2017-05-11 22:08:32: [Watch] Start watching 0 PM2 | 2017-05-11 22:08:32: Starting execution sequence in -fork mode- for app name:index id:0 PM2 | 2017-05-11 22:08:32: App name:index id:0 online
/root/.pm2/logs/index-error-0.log last 15 lines: 0|index | at Object.Module._extensions…js (module.js:580:10) 0|index | at Module.load (module.js:488:32) 0|index | at tryModuleLoad (module.js:447:12) 0|index | at Function.Module._load (module.js:439:3) 0|index | Error: Cannot find module 'express’ 0|index | at Function.Module._resolveFilename (module.js:470:15) 0|index | at Function.Module._load (module.js:418:25) 0|index | at Module.require (module.js:498:17) 0|index | at require (internal/module.js:20:19) 0|index | at Object.<anonymous> (/root/soft_index/tomcat/apache-tomcat-8.5.14/server/api/userApi.js:2:15) 0|index | at Module._compile (module.js:571:32) 0|index | at Object.Module._extensions…js (module.js:580:10) 0|index | at Module.load (module.js:488:32) 0|index | at tryModuleLoad (module.js:447:12) 0|index | at Function.Module._load (module.js:439:3)
/root/.pm2/logs/index-out-0.log last 15 lines: 0|index | errno: ‘ETIMEDOUT’, 0|index | syscall: ‘read’, 0|index | fatal: true } 0|index | { message_list: ‘’, author: ‘zygg’, date: ‘2017-05-12 09:04:45’ } 0|index | { Error: Cannot enqueue Query after fatal error. 0|index | at Protocol._validateEnqueue (/root/soft_index/tomcat/apache-tomcat-8.5.14/server/node_modules/mysql/lib/protocol/Protocol.js:199:16) 0|index | at Protocol._enqueue (/root/soft_index/tomcat/apache-tomcat-8.5.14/server/node_modules/mysql/lib/protocol/Protocol.js:135:13) 0|index | at Connection.query (/root/soft_index/tomcat/apache-tomcat-8.5.14/server/node_modules/mysql/lib/Connection.js:208:25) 0|index | at router.post (/root/soft_index/tomcat/apache-tomcat-8.5.14/server/api/userApi.js:73:10) 0|index | at Layer.handle [as handle_request] (/root/soft_index/tomcat/apache-tomcat-8.5.14/server/node_modules/express/lib/router/layer.js:95:5) 0|index | at next (/root/soft_index/tomcat/apache-tomcat-8.5.14/server/node_modules/express/lib/router/route.js:137:13) 0|index | at Route.dispatch (/root/soft_index/tomcat/apache-tomcat-8.5.14/server/node_modules/express/lib/router/route.js:112:3) 0|index | at Layer.handle [as handle_request] (/root/soft_index/tomcat/apache-tomcat-8.5.14/server/node_modules/express/lib/router/layer.js:95:5) 0|index | at /root/soft_index/tomcat/apache-tomcat-8.5.14/server/node_modules/express/lib/router/index.js:281:22 0|index | at Function.process_params (/root/soft_index/tomcat/apache-tomcat-8.5.14/server/node_modules/express/lib/router/index.js:335:12) code: ‘PROTOCOL_ENQUEUE_AFTER_FATAL_ERROR’, fatal: false }
应该是mysql断开连接了,能看下你的代码里面关于mysql的连接是怎么写的吗
@jiangli373 我感觉链接应该没问题,如果链接有问题,一开始就会报错,它这是开始好着,过一段时间就504了,我想应该是异常没处理好。
@alloyteamzy 能把你建立mysql 连接的代码贴下吗
MySQL中有一个名叫wait_timeout的变量,表示操作超时时间,当连接超过一定时间没有活动后,会自动关闭该连接,这个值默认为28800(即8小时)
@jiangli373 var models = require(’…/db’); var express = require(‘express’); var router = express.Router(); var mysql = require(‘mysql’); var $sql = require(’…/sqlMap’); // var getName = require(’…/…/src/js/getLoginName’); var login_name = ‘zygg’; console.log(‘get’ + login_name); // 连接数据库 var conn = mysql.createConnection(models.mysql);
conn.connect(); var jsonWrite = function(res, ret) { if (typeof ret === ‘undefined’) { res.json({ code: ‘1’, msg: ‘操作失败’ }); } else { res.json(ret); } };
你参考下这个帖子https://cnodejs.org/topic/516b77e86d382773064266df的方式修改下试试呢,应该就是mysql断开的问题
@jiangli373 好的
解决了,是按照 jiangli373 提供的方法:https://cnodejs.org/topic/516b77e86d382773064266df 解决的。找到问题原因所在了,因为mysql连接断开,我这里没有做异常处理,导致错误的代码继续在运行,导致node没有结束进程,pm2就不会自动重启。所以,pm2这些都好着,就是连不上数据库,会报504错误。
我也有个关于pm2的问题,直接node server.js 没问题,用pm2启动就会报500错误,这是为什么啊?
@hifarer pm2 -i 1 如果不出,就是代码写得有问题
@alloyteamzy 新版的mysql 库 没这个问题了把,我这里用的一直好好的啊···