atsushimemet / vook_web_v3

railsベースのvook_webの構築
3 stars 0 forks source link

機能開発: テキストの圧縮 #489

Closed garammasala29 closed 3 months ago

garammasala29 commented 3 months ago

Issue

テキスト圧縮の有効化 7,788 KiB 削減可能
テキストベースのリソースは圧縮(gzip、deflate、または brotli)して配信し、ネットワークの全体的な通信量を最小限に抑えてください。テキスト圧縮の詳細FCPLCP
URL
転送サイズ
減らせるデータ量
vook.tokyo 自社
9,407.1 KiB 7,787.5 KiB
/assets/application-58d1687….js(vook.tokyo)
8,815.0 KiB
7,268.0 KiB
/assets/application-267226c….css(vook.tokyo)
531.2 KiB
469.5 KiB
https://vook.tokyo
50.2 KiB
41.3 KiB
/api/knowledges(vook.tokyo)
10.7 KiB
8.7 KiB

何が問題なのか?/なぜ開発する必要があるのか (WHY)

具体的にどのようにIssueを解決していくのか

完了条件

追加論点

garammasala29 commented 3 months ago
  1. 必要なパッケージのインストール
    sudo yum groupinstall 'Development Tools'
    sudo yum install pcre pcre-devel zlib zlib-devel openssl openssl-devel cmake git
  2. Brotliライブラリのインストール
    
    # 必要な場合は古いディレクトリを削除
    rm -rf ~/brotli

Brotliのソースコードをダウンロード

git clone https://github.com/google/brotli.git cd brotli

Brotliをビルド

mkdir out && cd out cmake -DCMAKE_BUILD_TYPE=Release .. make

ライブラリをインストール

sudo make install

3. Nginxソースコードのダウンロードと展開

必要に応じて古いNginxディレクトリを削除

rm -rf ~/nginx-1.24.0 rm -rf ~/nginx-1.24.0.tar.gz

Nginxのソースコードを再ダウンロード

cd ~ wget http://nginx.org/download/nginx-1.24.0.tar.gz tar -zxvf nginx-1.24.0.tar.gz cd nginx-1.24.0

4. Brotliモジュールのダウンロード

git clone https://github.com/google/ngx_brotli.git cd ngx_brotli git submodule update --init cd ..

5. NginxをBrotliモジュール付きで再コンパイル

./configure --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/lib/nginx/tmp/scgi --pid-path=/run/nginx.pid --lock-path=/run/lock/subsys/nginx --user=nginx --group=nginx --with-compat --with-debug --with-file-aio --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_degradation_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module=dynamic --with-http_mp4_module --with-http_perl_module=dynamic --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-http_xslt_module=dynamic --with-mail=dynamic --with-mail_ssl_module --with-openssl-opt=enable-ktls --with-pcre --with-pcre-jit --with-stream=dynamic --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-threads --add-module=./ngx_brotli make sudo make install

6. Nginxのバージョン確認
再コンパイルされたNginxが正しくインストールされていることを確認

nginx -V

garammasala29 commented 3 months ago

/etc/nginx/nginx.conf

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    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  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    keepalive_timeout   65;
    types_hash_max_size 4096;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80;
        listen       [::]:80;
        server_name  _;
        root         /usr/share/nginx/html;

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

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

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

    gzip on;
    gzip_http_version 1.0;
    gzip_proxied any;
    gzip_min_length 256;
        gzip_disable "MSIE [1-6]\.";
        gzip_vary on;
        gzip_comp_level 6;
        gzip_buffers 16 8k;
        gzip_static on;
    gzip_types text/plain text/xml text/css
               text/comma-separated-values
               text/javascript application/x-javascript
               application/atom+xml
               application/json application/javascript
               application/font-woff application/font-ttf
               image/gif image/png image/jpeg
               application/octet-stream;
    brotli on;
    brotli_comp_level 6;  # 圧縮レベルを設定 (1-11, デフォルトは4)
    brotli_static on;  # 静的Brotliファイルを提供
    brotli_types text/plain text/xml text/css
                 text/comma-separated-values
                 text/javascript application/x-javascript
                 application/atom+xml
                 application/json application/javascript
                 application/font-woff application/font-ttf
                 image/gif image/png image/jpeg
                 application/octet-stream;
    }

# Settings for a TLS enabled server.
#
#    server {
#        listen       443 ssl http2;
#        listen       [::]:443 ssl http2;
#        server_name  _;
#        root         /usr/share/nginx/html;
#
#        ssl_certificate "/etc/pki/nginx/server.crt";
#        ssl_certificate_key "/etc/pki/nginx/private/server.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;
#
#        error_page 404 /404.html;
#        location = /404.html {
#        }
#
#        error_page 500 502 503 504 /50x.html;
#        location = /50x.html {
#        }
#    }

}

/etc/nginx/conf.d/vook_web_v3.conf

upstream puma {
  server unix:/var/www/vook_web_v3/tmp/sockets/puma.sock;
}

server {
  server_name  vook_web_v3.com;

  keepalive_timeout 0;

  access_log  /var/log/nginx/vook_web_v3.access.log  main;
  client_max_body_size 4G;

  root /var/www/vook_web_v3/public;
  location ~ .*\.(swf|SWF|ico|ICO|jar|txt|gz|js) {
    root /var/www/vook_web_v3/public;
    expires 15m;
    break;
  }
  location ~ ^\/fonts\/* {
    root /var/www/vook_web_v3/public;
    expires 15m;
    break;
  }
  location ~ ^\/assets\/* {
    root /var/www/vook_web_v3/public;
    break;
  }
  location ~ ^\/favicon\/* {
    root /var/www/vook_web_v3/public;
    break;
  }
  location = /manifest.json {
    root /var/www/vook_web_v3/public;
    break;
  }
  location / {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;

    #auth_basic "Restricted";
    #auth_basic_user_file /etc/nginx/.htpasswd;
    #if ($http_x_forwarded_proto = "http") {
     #rewrite ^(.*) https://$server_name$1
      #break;
    #}
    proxy_pass http://puma;
  }

  # Brotli圧縮とGzip圧縮の設定を追加
  gzip on;
  gzip_http_version 1.0;
  gzip_proxied any;
  gzip_min_length 256;
  gzip_disable "MSIE [1-6]\.";
  gzip_vary on;
  gzip_comp_level 6;
  gzip_buffers 16 8k;
  gzip_static on;
  gzip_types text/plain text/xml text/css
             text/comma-separated-values
             text/javascript application/x-javascript
             application/atom+xml
             application/json application/javascript
             application/font-woff application-font-ttf
             image/gif image/png image/jpeg
             application/octet-stream;

  brotli on;
  brotli_comp_level 6;
  brotli_static on;
  brotli_types text/plain text/xml text/css
               text/comma-separated-values
               text/javascript application/x-javascript
               application/atom+xml
               application/json application/javascript
               application/font-woff application-font-ttf
               image/gif image/png image/jpeg
               application/octet-stream;
}