部署nodejs应用

基础

  • git clone
  • npm i
  • pm2 start

修改nginx

  1. cat /etc/nginx/sites-enabled/default
  2. upstream backend_nodejs {
  3. server 127.0.0.1:3019 max_fails=0 fail_timeout=10s;
  4. #server 127.0.0.1:3001;
  5. keepalive 512;
  6. }
  7. server {
  8. listen 80 default_server;
  9. listen [::]:80 default_server ipv6only=on;
  10. #root /usr/share/nginx/html;
  11. root /home/sang/workspace/oschina/base2-wechat-jssdk/public;
  12. index index.html index.htm;
  13. # Make site accessible from http://localhost/
  14. server_name nodeonly.mengxiaoban.cn at35.com;
  15. client_max_body_size 16M;
  16. keepalive_timeout 10;
  17. location / {
  18. # First attempt to serve request as file, then
  19. # as directory, then fall back to displaying a 404.
  20. #try_files $uri $uri/ =404;
  21. # Uncomment to enable naxsi on this location
  22. # include /etc/nginx/naxsi.rules
  23. proxy_set_header X-Real-IP $remote_addr;
  24. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  25. proxy_set_header Host $http_host;
  26. proxy_set_header X-NginX-Proxy true;
  27. proxy_redirect off;
  28. proxy_next_upstream error timeout http_500 http_502 http_503 http_504;
  29. proxy_set_header Connection "";
  30. proxy_http_version 1.1;
  31. proxy_pass http://backend_nodejs;
  32. }
  33. }

注意

  • upstream backend_nodejs定义的代理转发的api地址
  • location /下面的proxy_pass,从upstream里取
  • root下面放的是静态资源,比如express下的public目录

然后重启nginx即可

  1. sudo nginx -s reload