> ## Content Index
> Fetch the complete content index at: https://lucent.blog/llms.txt
> Use this file to discover other available public pages before exploring further.

# Nginx开启Gzip兼容多层反代
- URL: https://lucent.blog/nginxkai-qi-gzipjian-rong-duo-ceng-fan-dai/
- Published: 2021-12-22T04:10:06.000Z
- Updated: 2026-08-21T00:49:57.000Z
- Author: Lucent
- Tags: Nginx

使用docker部署前端时，如果使用了静态资源压缩，则需要在nginx的配置中开启gzip：

```shell
server {
    listen 80;
    server_name localhost;

    gzip on;
    gzip_static on;     # 需要http_gzip_static_module 模块
    gzip_min_length 1k;
    gzip_comp_level 4;
    gzip_proxied any;
    gzip_types text/plain text/xml text/css;
    gzip_vary on;
    gzip_disable "MSIE [1-6]\.(?!.*SV1)";

    # 前端打包好的dist目录文件
    root /data/;

    location ~* ^/(code|auth|admin|monitor|gen|job|tx|act|mp|pay) {
       proxy_pass http://aurora-gateway:9999;
       proxy_connect_timeout 15s;
       proxy_send_timeout 15s;
       proxy_read_timeout 15s;
       proxy_set_header X-Real-IP $remote_addr;
       proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
       proxy_set_header X-Forwarded-Proto http;
    }
}

```

此时通过ip直接访问或者将域名直接解析到该服务器的ip是可以正常访问的，但是，生成环境中往往需要用一台服务器作为统一的出口，所以需要在该服务器或者其他服务器上再架设一台nginx进行反代。  
**这时，如果将域名解析到统一出口服务器，使用域名访问项目时就会报静态资源404,如下图所示：**  

![image.png](https://img-1251540275.cos.ap-shanghai.myqcloud.com/blog/image_1640146065015.png)

  
解决方法是nginx配置中加上:

```shell
gzip_http_version   1.0;

```

如下：  

![image.png](https://img-1251540275.cos.ap-shanghai.myqcloud.com/blog/image_1640146180478.png)

  
这时就可以通过域名正常访问了。