首页 > 系统相关 >打包优化,nginx部署到本地nginx服务器,模拟验证上线

打包优化,nginx部署到本地nginx服务器,模拟验证上线

时间:2022-12-11 17:44:27浏览次数:38  
标签:index log cdn server nginx html error 服务器 打包

// npm run build 自动设置process.env.NODE_ENV => 'production'
// npm run serve 则会设置为devlopment

const isProduction = process.env.NODE_ENV === 'production';
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const CompressionWebpackPlugin = require('compression-webpack-plugin')


const plugins = [];
let externals = {};

const cdn = {
    dev: {
        css: [], js: []
    },
    build: {
        css: [],
        js: [
            'https://cdn.bootcdn.net/ajax/libs/axios/0.27.2/axios.min.js',
            'https://cdn.bootcss.com/vue/2.6.11/vue.min.js',
            'https://cdn.bootcss.com/vue-router/3.5.4/vue-router.min.js',
        ]
    }
}


if (isProduction) {
    // 压缩 混淆 取出注释 console插件
    plugins.push(new UglifyJsPlugin({
        uglifyOptions: {
            output: {
                comments: false, // 去除注释
            },
            warnings: false, // 去除黄色警告,
            compress: {
                drop_console: true,
                drop_debugger: false, // 特定情况需要利用debugger防止调试
                pure_funcs: ['console.log'], // 移除console.log 避免console.error
            }
        }
    }));

    plugins.push(new CompressionWebpackPlugin({
        test: /\.(js|json|css)$/i,  // 图片一般不Gzip压缩 webpack-image-loader
        threshold: 10240, // 只有在10kb以上才压缩
    }))

    // 附加全局引包的引用关系
    externals = {
        // from后的 : 使用全局暴露的对象名,具体看包暴露哪个
        vue: 'Vue',
        'vue-router': 'VueRouter',
        axios: 'axios'
    }

}


module.exports = {
    productionSourceMap: !isProduction,
    configureWebpack: {
        devServer: {
            proxy: {
                '/api': {
                    target: 'http://localhost://api地址',
                    changeOrigin: true
                }
            }
        },
        plugins,
        externals
    },
    
    chainWebpack: config => {
        // html-webpack-plugin
        config.plugin('html').tap(args => {
            if (isProduction) {
                args[0].cdn = cdn.build
            } else {
                args[0].cdn = cdn.dev;
            }
            return args;
        });
    }
}

```Nginx代理操作简单,端口就是80
#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压缩
   #是否启动gzip压缩,on代表启动,off代表开启
    gzip on;
    # 优先.gz文件
    gzip_static on;
    #需要压缩的常见静态资源
    gzip_types text/plain application/javascript   application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
    
    #由于nginx的压缩发生在浏览器端而微软的ie6很坑爹,会导致压缩后图片看不见所以该选
    #项是禁止ie6发生压缩
    gzip_disable "MSIE [1-6]\.";
    
    #如果文件大于1k就启动压缩
    gzip_min_length 1k;
    
    #以16k为单位,按照原始数据的大小以4倍的方式申请内存空间,一般此项不要修改
    gzip_buffers 4 16k;
    
    #压缩的等级,数字选择范围是1-9,数字越小压缩的速度越快,消耗cpu就越大
    gzip_comp_level 2;
  
    


    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }
        # 代理
        location /api/ {
            proxy_pass http://localhost:/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;

        }



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

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

标签:index,log,cdn,server,nginx,html,error,服务器,打包
From: https://www.cnblogs.com/jycom/p/16974002.html

相关文章