前端请求慢,采用nginx压缩js、css等配置

前端请求慢,采用nginx压缩js、css等配置

# 前端静态资源配置 压缩js
        location ~* \.js\.gz$ {
            add_header Content-Type application/javascript;
            gzip_static always;
            gunzip on;
            try_files $uri =404;
        }

        # 前端静态资源配置
        location ~* \.css\.gz$ {
            add_header Content-Type application/css;
            gzip_static always;
            gunzip on;
            try_files $uri =404;
        }

完整配置

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;
    client_max_body_size 200M;
    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         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  www.xxx.com xxx.com;

        location / {
            return 301 https://www.xxx.com$request_uri;
        }
    }
    server {
        listen       443 ssl http2;
        listen       [::]:443 ssl http2;
        server_name  www.xxx.com xxx.com;
        root         /usr/share/nginx/html;

        ssl_certificate "/etc/nginx/www.xxx.com.pem";
        ssl_certificate_key "/etc/nginx/www.xxx.com.key";
        ssl_session_cache shared:SSL:1m;
        ssl_session_timeout  10m;
        ssl_ciphers HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers on;

        # 开启gzip压缩
        gzip on;
        gzip_vary on;
        gzip_proxied any;
        gzip_comp_level 6;
        gzip_types 
            text/plain
            text/css
            text/xml
            text/javascript
            application/javascript
            application/x-javascript
            application/json
            application/xml
            application/xml+rss
            image/svg+xml
            image/jpeg
            image/png
            image/gif
            image/webp
            video/mp4
            video/webm;
        gzip_min_length 1024;
        gzip_buffers 16 8k;

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

        location / {
            proxy_set_header Host $host:$server_port;
            proxy_pass  http://localhost:8080;
        }

        # 前端静态资源配置 压缩js
        location ~* \.js\.gz$ {
            add_header Content-Type application/javascript;
            gzip_static on;
            gunzip on;
            try_files $uri =404;
        }

        # 前端静态资源配置
        location ~* \.css\.gz$ {
            add_header Content-Type text/css;
            gzip_static on;
            gunzip on;
            try_files $uri =404;
        }

        # 图片静态资源压缩配置
        location ~* \.(jpg|jpeg|png|gif|ico|webp)$ {
            expires 30d;
            add_header Cache-Control "public, no-transform";
            try_files $uri =404;
            
            # 启用图片压缩
            image_filter resize 1024 768;
            image_filter_jpeg_quality 85;
            image_filter_buffer 10M;
            image_filter_interlace on;
        }

        # 视频静态资源压缩配置
        location ~* \.(mp4|webm)$ {
            expires 30d;
            add_header Cache-Control "public, no-transform";
            try_files $uri =404;
            
            # 视频流媒体支持
            mp4;
            mp4_buffer_size 4M;
            mp4_max_buffer_size 10M;
        }

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

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }
}
posted @ 2025-07-03 14:25  明月心~  阅读(32)  评论(0)    收藏  举报