Nginx Gzip配置语法

问题描述 投票:0回答:1

我想在nginx配置文件中启用gzip,但是当我添加代码时,出现语法错误。我想我将其添加到错误的位置,我正在尝试将其添加到注释行中的部分,但是出现语法错误,并且我的网站已关闭访问。我是第一次使用nginx,如果您有帮助,我会很高兴,谢谢。

upstream php {
       server unix:/tmp/php-cgi.socket;
        server 127.0.0.1:9000;
}

server {
server_name  www.example.com;
rewrite ^/(.*) http://example.com/$1 permanent;

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}

server {
server_name example.com;

access_log /var/www/example.com/logs/access.log;
error_log /var/www/example.com/logs/error.log;

# location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
 #              expires max;
  #             log_not_found off;
   #   }

    location = /favicon.ico {
                log_not_found off;
                access_log off;
        }

        location = /robots.txt {
                allow all;
                log_not_found off;
                access_log off;
        }

  location ~ \.php$ {
                #NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
                include fastcgi.conf;
        include fastcgi_params;
                fastcgi_intercept_errors on;
                fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
                #The following parameter can be also included in fastcgi_params file
                fastcgi_param  SCRIPT_FILENAME  /var/www/example.com/public$fastcgi_script_name;
        #fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
        }

location / {

root   /var/www/example.com/public/;
index  index.php;
try_files $uri $uri/ /index.php?$args;
}


  }


#location / {
                # This is cool because no php is touched for static content.
                # include the "?$args" part so non-default permalinks doesn't break when using query string
       #         try_files $uri $uri/ /index.php?$args;
      #  }
server {
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}



server {
    if ($host = example.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot



listen   80;
server_name example.com;
    return 404; # managed by Certbot


}server {
    if ($host = www.example.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot



listen   80;
server_name  www.example.com;
    return 404; # managed by Certbot

}
php nginx webserver
1个回答
0
投票

NGINX中有两个压缩模块:

  • gzip-用于压缩HTML响应
  • gzip_static-用于提供已压缩的文件变体

首先,将gzip on;添加到您的服务器上下文中。

如果NGINX是在gzip支持下编译的(默认情况下应构建),则它应该可以工作。

如果仍然遇到语法错误,请检查error.log并在此处更新为确切的错误。

P.S。我建议使用命令nginx -t测试NGINX的配置。

© www.soinside.com 2019 - 2024. All rights reserved.