创建ecosystem.config.js
module.exports = {
apps: [{
name: 'wangyu',
script: './server.js',
instances: 4,
max_restarts: 7,
env: {
NODE_ENV: 'production',
NODE_PORT: 8080,
REACT_APP_ENV: 'test',
},
watch: false,
merge_logs: true,
exec_mode: 'cluster',
max_memory_restart: '600M',
instance_var: 'NODE_APP_INSTANCE',
}]
}
创建server.js
https://nextjs.org/docs/advanced-features/custom-server
const { parse } = require('url');
const next = require('next');
const express = require('express');
const server = express();
const NODE_PORT = process.env.NODE_PORT;
const dev = process.env.NODE_ENV !== 'production';
const app = next({ dev });
app.prepare().then(() => {
server.get('*', (req, res) => {
const parsedUrl = parse(req.url, true);
const { pathname, query } = parsedUrl;
// 主页
if (pathname === '/wangyu/index') {
return app.render(req, res, '/', query);
}
// ... 其它页面
app.render(req, res, '/404', query);
})
server.listen(NODE_PORT, () => console.log('Example app listening on port ' + NODE_PORT));
})
修改npm script
"server_start": "pm2 start ecosystem.config.js",
"server_reload": "pm2 reload ecosystem.config.js",
"server_stop": "pm2 stop ecosystem.config.js"