AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 438237
Accepted
Nyxynyx
Nyxynyx
Asked: 2012-10-15 10:00:22 +0800 CST2012-10-15 10:00:22 +0800 CST 2012-10-15 10:00:22 +0800 CST

关闭 Nginx 中某个位置的 gzip

  • 772

如何为特定位置及其所有子目录关闭 gzip?我的主站点位于,我想为和http://mydomain.com关闭 gzip 。在 中打开。http://mydomain.com/foohttp://mydomain.com/foo/bargzipnginx.conf

gzip我尝试如下所示关闭,但 Chrome 开发工具中的 Response Headers 显示Content-Encoding:gzip.

应该如何正确禁用 gzip/输出缓冲?

尝试:

server {
    listen   80;
    server_name www.mydomain.com mydomain.com;
    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;
    root /var/www/mydomain/public;

    index index.php index.html;

    location / {
        gzip on;
        try_files $uri $uri/ /index.php?$args ;
    }

    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_read_timeout 300;
    }

    location /foo/ {
        gzip off;
        try_files $uri $uri/ /index.php?$args ;
    }

}

更新

我关闭gzip并nginx.conf尝试使用命名位置。然而 gzip 现在总是关闭的,并且gzip on;在location /块中似乎没有重新打开 gzip。有任何想法吗?

server {
    listen   80;
    server_name www.mydomain.com mydomain.com;
    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;
    root /var/www/mydomain/public;

    index index.php index.html;

    location / {
        try_files $uri $uri/ @php;
    }

    location /foo/ {
        try_files $uri $uri/ @php_nogzip;
    }

    location @php {
        gzip on;
        try_files $uri $uri/ /index.php?$args ;
    }

    location @php_nogzip {
        gzip off;
        try_files $uri $uri/ /index.php?$args ;
    }

    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_read_timeout 300;
    }

}
php
  • 3 3 个回答
  • 23382 Views

3 个回答

  • Voted
  1. Best Answer
    poige
    2012-10-15T11:15:27+08:002012-10-15T11:15:27+08:00

    我假设您请求的 URL 正在try_files内部处理,location ^~ /foo/并且由于缺少这些文件,它们在内部被重定向到另一个location没有gzip off继承的处理程序。

    尝试在位置中使用“命名位置” /foo,然后@fooNoGzip使用gzip off内部和fascgi_pass内容定义该“命名”位置。

    • 2
  2. Danila Vershinin
    2019-08-13T10:46:09+08:002019-08-13T10:46:09+08:00

    只是为了一些死灵浪漫:OP 的更新的 nginx 配置不起作用,因为它最终由\.php$location 块处理。

    正确的解决方案是将其删除并将命名位置作为将请求转发到 FastCGI (PHP-FPM) 的位置:

    server {
        listen   80;
        server_name www.mydomain.com mydomain.com;
        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;
        root /var/www/mydomain/public;
    
        index index.php index.html;
    
        location / {
            try_files $uri $uri/ @php;
        }
    
        location /foo/ {
            try_files $uri $uri/ @php_nogzip;
        }
    
        location @php {
            gzip on;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_index index.php;
            include fastcgi_params;
            fastcgi_read_timeout 300;
        }
    
        location @php_nogzip {
            gzip off;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_index index.php;
            include fastcgi_params;
            fastcgi_read_timeout 300;
        }
    }
    
    • 1
  3. higuita
    2015-12-01T11:46:51+08:002015-12-01T11:46:51+08:00

    由于每个位置都是独立的,设置gzip offon@php_nogzip将不适用于location ~ \.php$,它仍然使用 nginx/server 默认值。这就是您看到 gzip 关闭的原因,因为这是默认设置。只有 中的 try_files 传送的文件@php_nogzip才会被压缩。

    我看到的唯一方法是使用地图。在 http 块中使用:

    map $uri   $gz {
       default  "on";
        ~/foo/  "off";
    }
    

    然后在服务器中:

       location ~ \.php$ {
            gzip $gz; 
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_index index.php;
            include fastcgi_params;
            fastcgi_read_timeout 300;
        }
    
    • -1

相关问题

  • 用户特定的 Php.ini 当 php 作为模块运行时?

  • 使 php mail() 函数在 ubuntu-server 上工作的步骤是什么?

  • Web 服务器和数据库服务器位于完全不同的位置

  • PHP 作为 CGI 还是 Apache 模块?

  • 通过 VPN 连接什么是远程服务器 IP?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve