【问题标题】:Nginx Gzip configuration syntaxNginx Gzip 配置语法
【发布时间】:2020-08-04 22:06:24
【问题描述】:

我想在 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】:

    NGINX 中有两个压缩模块:

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

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

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

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

    附:我建议使用命令nginx -t 测试NGINX 的配置。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-25
      • 1970-01-01
      • 2020-10-25
      • 2016-09-24
      • 1970-01-01
      • 2017-02-11
      • 2017-02-08
      • 2016-06-27
      相关资源
      最近更新 更多