nuxt 服务端部署

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/qq_14993375/article/details/97783032

1、打包,打包完把下面文件夹挪到服务器器上

// 打包
npm run build

在这里插入图片描述

// nuxt.config.js
module.exports = {
    mode: 'universal',
    /*
    ** Headers of the page
    */
    head: {
      title: process.env.npm_package_name || '商盟',
      meta: [
        { charset: 'utf-8' },
        { name: 'viewport', content: 'width=device-width, initial-scale=1' },
        { hid: 'description', name: 'description', content: process.env.npm_package_description || '' }
      ],
      link: [
        { rel: 'icon', type: 'image/x-icon', href: '/favicon.ico' }
      ]
    },
    /*
    ** Customize the progress-bar color
    */
    loading: { color: '#fff' },
    /*
    ** Global CSS
    */
    css: [
      'element-ui/lib/theme-chalk/index.css',
      '~/assets/styles/index.scss'
    ],
    /*
    ** Plugins to load before mounting the App
    */
    plugins: [
      '@/plugins/element-ui'
    ],
    /*
    ** Nuxt.js dev-modules
    */
    devModules: [
    ],
    /*
    ** Nuxt.js modules
    */
    modules: [
      '@nuxtjs/axios'
    ],
    axios: {
      proxy: true
    },
    proxy: {
      '/saas-pc': {
        target: 'http://localhost:8083',
        // pathRewrite: { '^/api': '' }
      }
    },
    /*
    ** Build configuration
    */
    build: {
      publicPath: '/saas/',   // 如果在这里指定了其他的,那么生产环境打包配置的无效果
      transpile: [/^element-ui/],
      /*
      ** You can extend webpack config here
      */
      extend(config, ctx) {
      }
    }
}

注意:publicPath,如果在这里配置了,那要跟打包前的文件名对应,否则js、图片访问不到,要么把这里配置的去掉

// package.json
{
  "name": "nuxt114",
  "version": "1.0.0",
  "description": "pc-project",
  "author": "ffc",
  "private": true,
  "scripts": {
    "build": "nuxt build && npm start",
    "start": "nuxt start"
  },
  "dependencies": {
    "cross-env": "^5.2.0",
    "koa": "^2.6.2",
    "element-ui": "^2.4.11",
    "nuxt": "^2.0.0"
  },
  "devDependencies": {
    "@nuxtjs/axios": "^5.5.4",  // 这些都必须要
    "@nuxtjs/proxy": "^1.3.3",
    "node-sass": "^4.12.0",
    "nodemon": "^1.18.9",
    "sass-loader": "^7.1.0"
  }
}

2、启动

npm run start

3、nignix配置

#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;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;
    gzip            on;
    gzip_min_length 1000;
    gzip_proxied    expired no-cache no-store private auth;
    gzip_types      text/plain application/x-javascript application/javascript text/css application/xml text/javascript 

application/x-httpd-php image/jpeg image/gif image/png;

   upstream backa {

       #ip_hash;

       server 127.0.0.1:8084;        

    }

   upstream backb {

       server 127.0.0.1:3000;   
       keepalive 64;   

    }

    server {
        listen       80;
        server_name  a.xxx.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        #location / {
        #    root   html;
        #     index  index.html index.htm;
        # }
  
	location /saas-pc/api {
	     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_buffering off; 
	     proxy_pass http://backa;     
	}

	location / {  
	     proxy_http_version 1.1;
	     proxy_set_header Upgrade $http_upgrade;
	     proxy_set_header Connection "upgrade";
	     proxy_set_header Host $host;
	     proxy_set_header X-Nginx-Proxy true;
	     proxy_cache_bypass $http_upgrade;
	     proxy_pass http://backb;     
	  
	}


        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

猜你喜欢

转载自blog.csdn.net/qq_14993375/article/details/97783032