nginx 配置 浏览器缓存 和 HTTPS

883 阅读1分钟

Nginx 配置浏览器缓存

  • 强缓存
    • <- cache-control: max-age=600 ---- no-store 不缓存 no-cache 不使用强缓存
    • <- expires: Mon, 14 Sep 2020 09:02:20 GMT
  • 协商缓存
    • <- last-modified: Fri, 07 Aug 2020 02:35:59 GMT
    • -> if-modified-since: Fri, 07 Aug 2020 02:35:59 GMT
    • <- etag: W/“5f2cbe0f-2382"
    • -> if-none-match: W/"5f2cbe0f-2382"

Nginx 配置

  • gzip 和 etag
http {
  # 开启gzip
  gzip on;
  # 启用gzip压缩的最小文件;小于设置值的文件将不会被压缩
  gzip_min_length 1k;
  # gzip 压缩级别 1-9
  gzip_comp_level 5;
  # 进行压缩的文件类型。
  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;
  # 是否在http header中添加Vary: Accept-Encoding,建议开启
  gzip_vary on;
  
  etag on;
}
  • 强缓存配置
server {
  location ~* \.(html)$ {
    access_log off;
    add_header  Cache-Control  max-age=no-cache;
  }

  location ~* \.(css|js|png|jpg|jpeg|gif|gz|svg|mp4|ogg|ogv|webm|htc|xml|woff)$ {
    access_log off;
    add_header    Cache-Control  max-age=360000;
  }
}

HTTPS 配置

ssl_certificate "/etc/pki/nginx/server.crt";
ssl_certificate_key "/etc/pki/nginx/private/server.key";
return 301 https://www.nllcoder.com$request_uri;
server {
        listen       443 ssl http2 default_server;
        listen       [::]:443 ssl http2 default_server;
        server_name  www.nllcoder.com;
        root         /usr/share/nginx/html;

        ssl_certificate "/etc/pki/nginx/www.nllcoder.com_bundle.crt";
        ssl_certificate_key "/etc/pki/nginx/private/www.nllcoder.com.key";
        ssl_session_cache shared:SSL:1m;
        ssl_session_timeout  10m;
        ssl_ciphers PROFILE=SYSTEM;
        ssl_prefer_server_ciphers on;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
        }

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

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