vue项目nginx配置问题

先对项目生产环境build进行设置:

# config index.js 
build: {
    env: require('./prod.env'),
    port: 4343,
    // Template for index.html
    index: path.resolve(__dirname, '../dist/index.html'),

    // Paths
    assetsRoot: path.resolve(__dirname, '../dist'),
    assetsSubDirectory: 'static',
    assetsPublicPath: '/',

    /**
     * Source Maps
     */

    productionSourceMap: false,
    // https://webpack.js.org/configuration/devtool/#production
    devtool: '#source-map',

    // Gzip off by default as many popular static hosts such as
    // Surge or Netlify already gzip all static assets for you.
    // Before setting to `true`, make sure to:
    // npm install --save-dev compression-webpack-plugin
    productionGzip: false,
    productionGzipExtensions: ['js', 'css'],

    // Run the build command with an extra argument to
    // View the bundle analyzer report after build finishes:
    // `npm run build --report`
    // Set to `true` or `false` to always turn it on or off
    bundleAnalyzerReport: process.env.npm_config_report
  }
}

nginx代理到node的配置:

server {
        // listen 4343
        server_name www.ygnili.com;

        location / {
                proxy_pass http://127.0.0.1:4343/; // 端口号和config目录下index.js的build:{port:4343}一致
                #proxy_cache proxycache;
                #proxy_cache_key $host:$server_port$request_uri;
                #proxy_cache_valid 200 304 1h;
                proxy_pass_header Set-Cookie;
                proxy_set_header Host $host;
                proxy_set_header X-Forwarded-For $remote_addr;
                #add_header X-Cache '$upstream_cache_status from $host';
        }
}

有server_name的时候不能设置listen端口,同理相反。

(操作只有把源码请求的地址改了,重新设置express跑的端口,重新打包上传服务器,启动express,nginx代理过去)

express服务多个项目的时候,需要用到pm2去部署node,启动express,把nginx转发过来,就ok了。

你可能感兴趣的:(vue项目nginx配置问题)