zoukankan      html  css  js  c++  java
  • 在nginx上部署web项目及启用gzip压缩(以Vue路由history模式打包文件为例)

    系统环境 linux ubuntu 18.04,不同系统只是系统操作命令上的不同,nginx的目录结构和配置文件格式是一致的

    安装nginx

    sudo apt-get intall nginx

    Nginx的主配置文件是 nginx目录下的nginx.conf

    建议在conf.d文件夹下新建xx.conf进行配置,主配置文件会默认读取conf.d文件夹下的配置文件

    创建配置文件

    定位到虚拟主机配置文件目录

    cd /etc/nginx/conf.d

    使用vim编辑器创建配置文件并编辑

    vim xx.conf

    主要配置内容

    server {
            listen      80; # 端口
            server_name  localhost; # 域名
    
            # gzip压缩优化相关
            gzip on;
            gzip_vary on;
            gzip_min_length 1k;
            gzip_comp_level 9;
            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;
            gzip_disable "MSIE [1-6].";
    
            # 托管文件根目录
            root   "/etc/nginx/webPacker/dist";
    
            location / {
              expires    3d;  #表示缓存3天
              #expires    -1;  #表示永远过期,在js、css等静态文件在没有修改的情况下返回的是http 304,如果修改返回http 200
    
              #如果不想让代理或浏览器缓存,加no-cache参数
              #add_header Cache-Control no-cache;
    
                try_files $uri $uri/ /index.html;
            }

         #可选的代理服务,与vue.config.js中webpack的devServer的代理同理,可解决跨域问题
          #location /web { porxy_pass http://121.36.33.67:8888; }
    
    }

    如果没有Vim编辑器,安装Vim编辑器

    sudo apt-get install vim

    Vim编辑器命令

    i  进入插入模式,或者说编辑模式

    esc键退出插入模式

    dd 删除当前行

    shift + :   进入命令模式  

    命令模式下:

    wq 保存并退出

    q! 强制退出

    ubuntu中操作nginx的常用命令

    nginx -h     查看帮助详情

    nginx       启动nginx

    nginx -s stop        停止nginx

    nginx -s reopen    重启nginx

    nginx -s reload     重新加载配置文件

    nginx -t      测试配置是否有语法错误

    更多nginx 配置

    关于Nginx的详细配置这位大佬分享的很详细了

    http://www.ha97.com/5194.html

  • 相关阅读:
    【乱侃】How do they look them ?
    【softeware】Messy code,some bug of Youdao notebook in EN win7
    【随谈】designing the login page of our project
    【web】Ad in security code, making good use of resource
    SQL数据库内存设置篇
    关系数据库的查询优化策略
    利用SQL未公开的存储过程实现分页
    sql语句总结
    sql中使用cmd命令注销登录用户
    SQLServer 分页存储过程
  • 原文地址:https://www.cnblogs.com/web-xu/p/12597990.html
Copyright © 2011-2022 走看看