生产部署
部署代码
使用 yarn build
打包代码,将打包文件与package.json,上传到服务器(这个过程使用 ci/cd 完成)。
开发与生产环境的配置文件 .env
是分离的,随代码上传的 .env
属于开发环境。
注意:
.env
定义的配置变量可以使用process.env.XXX
进行访问。 在开发环境,更改.env
后,使用yarn env-to-ts-type
生成新类型。
nginx
命令
# 退出,此方式停止步骤是待nginx进程处理任务完毕进行停止。nginx -s quit
# 关闭,相当于先查出nginx进程id再使用kill命令强制杀掉进程nginx -s stop
# 重启,重新加载配置文件,先停止 nginx 再启动 nginx 即可将配置信息在 nginx 中生效nginx -s reload
配置文件
/etc/nginx/nginx.conf
user www-data;worker_processes auto;pid /run/nginx.pid;include /etc/nginx/modules-enabled/*.conf;
events { worker_connections 768; # multi_accept on;}
http { # Basic Settings ##
sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 2048; # server_tokens off;
server_names_hash_bucket_size 64; # server_name_in_redirect off;
include /etc/nginx/mime.types; default_type application/octet-stream;
## # SSL Settings ##
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE ssl_prefer_server_ciphers on;
## # Logging Settings ##
access_log /var/log/nginx/access.log; error_log /var/log/nginx/error.log;
## # Gzip Settings ## gzip on;
gzip_vary on;
# gzip_proxied any; gzip_comp_level 3; gzip_buffers 16 8k; gzip_min_length 1k; gzip_http_version 1.1; gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript image/svg+xml;
## # Virtual Host Configs ##
include /etc/nginx/conf.d/*.conf; include /etc/nginx/sites-enabled/*;}
/etc/nginx/conf.d/example.com
upstream api_server { hash $remote_addr consistent; server 127.0.0.1:3000; # 指向本地的 node 服务 # server 127.0.0.1:3093 weight=2; # server 127.0.0.1:3094; # server 127.0.0.1:3095; # server 10.10.11.1:80; # server 10.10.11.2:80; # server 10.10.11.3:80 backup;}server { # listen 443 ssl; # managed by Certbot # ssl_certificate /etc/letsencrypt/live/tailwindcss.tper.co/fullchain.pem; # managed by Certbot # ssl_certificate_key /etc/letsencrypt/live/tailwindcss.tper.co/privkey.pem; # managed by Certbot # include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot # ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot # 替换为你的域名 server_name example.com; location / { proxy_http_version 1.1; proxy_cache_bypass $http_upgrade; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection 'upgrade'; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://api_server/api$request_uri; } location /io { proxy_http_version 1.1; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $host; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; proxy_pass http://api_server$request_uri; } location /ui { proxy_http_version 1.1; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $host; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; proxy_pass http://api_server$request_uri; }}server { listen 80; server_name _; return 301 https://$host$request_uri;}
https 支持
使用 Certbot
pm2 启动
启动 app.js, 并指定 name
pm2 start app.js --name my-api
# 传递 node 参数pm2 start app.js --name my-api --node-args="--max-old-space-size=4096"
重启
注意:如果是文件有更新,重启命令有时候使用的还是旧的打包文件,如果发生这种情况,请使用
pm2 stop
和pm2 start
pm2 restart all
停止
pm2 stop all
删除配置
pm2 delete all
系统重启时,pm2 当前运行环境重启
pm2 startup
保当前存状态
pm2 save
检查是否运行正常
node 参数调优
--max-old-space-size
Node.js 中的内存限制,默认是 512 MB,如果超过该值,会触发 out of memory
错误,
--max-semi-space-size
触发 GC 的空间大小,默认 16 MB,为了避免频繁触发 GC,推荐使用 128 MB
--max-old-space-size
设置参考:
#increase to 1gbnode --max-old-space-size=1024 index.js
#increase to 2gbnode --max-old-space-size=2048 index.js
#increase to 3gbnode --max-old-space-size=3072 index.js
#increase to 4gbnode --max-old-space-size=4096 index.js
#increase to 5gbnode --max-old-space-size=5120 index.js
#increase to 6gbnode --max-old-space-size=6144 index.js
#increase to 7gbnode --max-old-space-size=7168 index.js
#increase to 8gbnode --max-old-space-size=8192 index.js