nginx部署nuxt

追风筝的猿

1.nuxt项目package.json的scripts字段配置

以下是mac os的配置
"dev": "nuxt",
"build": "nuxt build",
"start:dev": "NODE_ENV=development nuxt start --port 3001",
"start": "nuxt start --port 3000",
如果是windows os则安装cross-env模块
"dev": "nuxt",
"build": "nuxt build",
"start:dev": "cross-env NODE_ENV=development nuxt start --port 3001",
"start": "nuxt start --port 3000",

2.nginx配置

路径:/etc/nginx/nginx.conf

主要参考以下server字段

server_name要写自己的域名

#user  nobody;
worker_processes  1;
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
#pid  logs/nginx.pid;
events {
  worker_connections  1024;
}
http {
  include       mime.types;
  default_type  application/octet-stream;
  sendfile        on;
  keepalive_timeout  65;
  #gzip  on;
    server {
        listen       80;
        server_name  xxxx.com www.xxxx.com;
        location / {
                proxy_redirect off;
                proxy_set_header Host               $host;
                proxy_set_header X-Real-IP          $remote_addr;
                proxy_set_header X-Forwarded-For    $proxy_add_x_forwarded_for;
                proxy_set_header X-Forwarded-Proto  $scheme;
                proxy_read_timeout          1m;
                proxy_connect_timeout       1m;
                proxy_pass                          http://127.0.0.1:3000; # 监听node 3000端口即正式环境
        }
    }
  server {
      listen       80;
      server_name  test.xxxx.com www.test.xxxx.com;
      location / {
          proxy_redirect off;
          proxy_set_header Host               $host;
          proxy_set_header X-Real-IP          $remote_addr;
          proxy_set_header X-Forwarded-For    $proxy_add_x_forwarded_for;
          proxy_set_header X-Forwarded-Proto  $scheme;
          proxy_read_timeout          1m;
          proxy_connect_timeout       1m;
          proxy_pass                          http://127.0.0.1:3001; # 监听node 3001端口即测试环境
      }
      error_page   500 502 503 504  /50x.html;
      location = /50x.html {
          root   html;
      }
  }
}

配置好后重启nginx(下面有教程)

3.上传相关文件到服务器

1.打包

 npm run build

2.上传
上传文件

4.pm2开启node服务

进入上传目录,然后再执行以下命令

npm i
npm i pm2 -g
pm2 start npm --name "prod" -- run start
pm2 start npm --name "test" -- run start:dev

5.面板监听

进入https://keymetrics.pm2.io/
按照指示在服务器执行

pm2 link xxx xxx 

面板

6.其他有用的命令

查看nginx开放的端口

sudo netstat -anp | grep nginx

强制关闭nginx

pkill -9 nginx

重启nginx

sudo nginx -s reload

删除文件夹/文件

rm -rf

get到的童鞋点个赞吧 -_-/

阅读 2.2k

一个前端开发者,曾就职猪八戒网、马上消费金融

14 声望
0 粉丝
0 条评论

一个前端开发者,曾就职猪八戒网、马上消费金融

14 声望
0 粉丝
文章目录
宣传栏