Nginx

使用 docker 和 bitnami/nginx 啟用 gzip

  • May 24, 2020

我用 docker 和bitnami/nginx鏡像部署了一個網站:https ://www.10studio.tech/demo 。部署後,我意識到像這樣的文件analyzejs.js沒有被壓縮:

在此處輸入圖像描述

這裡是docker-compose.yml

version: "3"
services:
 docusaurus:
   image: bitnami/nginx:1.16
   restart: always
   volumes:
   - ./build:/app
   - ./certs:/certs:ro
   - ./my_server_block.conf:/opt/bitnami/nginx/conf/server_blocks/my_server_block.conf:ro
   ports:
   - "3001:3001"
   - "3002:3002"

這裡是my_server_block.conf

server {
 listen  3002;
 absolute_redirect off;
 root  /app;

 location = / {
   rewrite ^(.*)$ https://$http_host/docs/introduction redirect;
 }

 location / {
   try_files $uri $uri/ =404;
 }
}

server {
 listen  3001 ssl;

 ssl_certificate      /certs/server.crt;
 ssl_certificate_key  /certs/server.key;

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

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

 location / {
   proxy_pass http://localhost:3002;
   proxy_redirect off;
   proxy_set_header Host $host:$server_port;
   proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
   proxy_set_header X-Forwarded-Ssl on;
 }
}

這是/opt/bitnami/nginx/conf/nginx.conf,其中 gzip 似乎已啟用:

I have no name!@8317023de7ec:/app$ cat /opt/bitnami/nginx/conf/nginx.conf
# Based on https://www.nginx.com/resources/wiki/start/topics/examples/full/#nginx-conf
# user              www www;  ## Default: nobody

worker_processes  auto;
error_log         "/opt/bitnami/nginx/logs/error.log";
pid               "/opt/bitnami/nginx/tmp/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    "/opt/bitnami/nginx/logs/access.log";
   add_header    X-Frame-Options SAMEORIGIN;

   client_body_temp_path  "/opt/bitnami/nginx/tmp/client_body" 1 2;
   proxy_temp_path        "/opt/bitnami/nginx/tmp/proxy" 1 2;
   fastcgi_temp_path      "/opt/bitnami/nginx/tmp/fastcgi" 1 2;
   scgi_temp_path         "/opt/bitnami/nginx/tmp/scgi" 1 2;
   uwsgi_temp_path        "/opt/bitnami/nginx/tmp/uwsgi" 1 2;

   sendfile           on;
   tcp_nopush         on;
   tcp_nodelay        off;
   gzip               on;
   gzip_http_version  1.0;
   gzip_comp_level    2;
   gzip_proxied       any;
   gzip_types         text/plain text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript;
   keepalive_timeout  65;
   ssl_protocols      TLSv1 TLSv1.1 TLSv1.2;

   include  "/opt/bitnami/nginx/conf/server_blocks/*.conf";

   # HTTP Server
   server {
       # port to listen on. Can also be set to an IP:PORT
       listen  8080;

       location /status {
           stub_status on;
           access_log   off;
           allow 127.0.0.1;
           deny all;
       }
   }
}

有誰知道這裡出了什麼問題以及如何啟用 gzip?

您的 .js 文件發送為,application/javascript但該字元串未列在gzip_types. 添加它或更正與文件一起發送的內容類型。

引用自:https://serverfault.com/questions/1018420