配置HTTPS

      server {
        listen       443 ssl;
        server_name  xiaoying.org.cn;
        root         /usr/local/dist; #前端打包文件位置

       #域名证书文件名称
        ssl_certificate /etc/nginx/fullchain.cer;
       #域名证书私钥文件名称
        ssl_certificate_key /etc/nginx/xiaoying.org.cn.key;
  
        #请按照以下协议配置
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2; 
        #请按照以下套件配置,配置加密套件,写法遵循 openssl 标准。
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE; 
        ssl_prefer_server_ciphers on;
        ssl_session_cache shared:SSL:1m;
        ssl_session_timeout  10m;

        location ^~ /api/ {   #后端接口前缀
        proxy_set_header Host $host;
        proxy_set_header   X-Forwarded-Proto $scheme;
        proxy_set_header   X-Real-IP         $remote_addr;
        proxy_pass http://127.0.0.1:8080;  #后端接口地址
    }

        include /etc/nginx/default.d/*.conf;

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
  }
    

开启gzip压缩静态文件

配置 GZip 压缩

gzip 可以在 http, server, location 中和配置,这里配置到 http 下是全局配置,

      events {
    worker_connections 1024;
}

http{
   gzip on;   # 开启 gzip,Default: off
   gzip_min_length 1k; # gzip 压缩文件体积的最小值
   gzip_comp_level 6; # 压缩级别: 1-9,6是推荐的压缩级别,Default: 1
  
   # 压缩文件类型
   gzip_types application/atom+xml application/geo+json application/javascript application/x-javascript application/json application/ld+json application/manifest+json application/rdf+xml application/rss+xml application/xhtml+xml application/xml font/eot font/otf font/ttf image/svg+xml text/css text/javascript text/plain text/xml image/jpeg image/gif image/png;
  
  # 当客户端的 Accept-Encoding-capabilities 头发生变化时,告诉代理缓存 gzip 和常规版本的资源
   gzip_vary on; 
  
  /**服务器开启对静态文件( CSS, JS, HTML, SVG, ICS, and JSON)的压缩。但是,要使此部分与之相关,需要在 gzip_types 设置 MIME 类型,,仅仅设置 gzip_static 为 on 是不会自动压缩静态文件的。**/
   gzip_static on;
  
 /** 设置用于压缩响应的 number 和 size 的缓冲区。默认情况下,缓冲区大小等于一个内存页。根据平台的不同,它也可以是4K或8K。**/
   gzip_buffers 32 4k;
  
  # IE6 以下的浏览器禁用 gzip 压缩
   gzip_disable "MSIE [1-6]\.";
  
  server {
    ...
      location / {
      ...
    }
  }
}
    

适合 GZip 压缩的资源类型

实际上,gzip 主要用于对文本类型的资源进行压缩,例如常用见的文本资源:

  • HTML 文件:text/html(nginx 服务器默认就会压缩)、application/xhtml+xml
  • CSS 文件:text/css
  • JS 文件:application/x-javascript、application/javascript、text/javascript
  • JSON 文件(或者API请求结果):application/json、application/geo+json、application/ld+json application/manifest+json、application/x-web-app-manifest+json
  • XML 文件:application/xml、application/atom+xml、application/rdf+xml、application/rss+xml
  • SVG 文件:image/svg+xml;

除了常用的文本文件,gzip 也支持压缩以下 MIME 类型的文件:

  • application/vnd.ms-fontobject
  • application/wasm
  • font/eot
  • font/otf
  • font/ttf
  • image/bmp
  • text/cache-manifest
  • text/calendar
  • text/markdown
  • text/plain
  • text/vcard
  • text/vnd.rim.location.xloc
  • text/vtt
  • text/x-component
  • text/x-cross-domain-policy

GZip 对基于文本的内容的资源压缩效果最好,在压缩较大文件时往往可实现高达 70-90% 的压缩率,而如果对已经通过替代算法压缩过的资源(例如,大多数图片格式)运行 gzip,则效果甚微,甚至毫无效果。

加载中...

声明

作者: liyao

版权:本博客所有文章除特别声明外,均采用CCBY-NC-SA4.O许可协议。转载请注明!

最后更新于 2025-10-02 15:34 history