![如何让 nginx 对所有文件进行 gzip 压缩并添加过期标头](https://linux22.com/image/583685/%E5%A6%82%E4%BD%95%E8%AE%A9%20nginx%20%E5%AF%B9%E6%89%80%E6%9C%89%E6%96%87%E4%BB%B6%E8%BF%9B%E8%A1%8C%20gzip%20%E5%8E%8B%E7%BC%A9%E5%B9%B6%E6%B7%BB%E5%8A%A0%E8%BF%87%E6%9C%9F%E6%A0%87%E5%A4%B4.png)
最近开始使用 VPS 系统并安装它们,诸如此类等等。因此,我有几个正常运行的网站,其中一个网站我正在尝试真正优化其速度。使用 Yahoo 的 ySlow 作为指南,我仍然无法通过 gzip 和标头过期部分。我的 nginx.conf 位于 /etc/nginx/nginx.conf,以下是其详细信息:
请帮忙!我不知道为什么它不工作。
#######################################################################
#
# This is the main Nginx configuration file.
#
# More information about the configuration options is available on
# * the English wiki - http://wiki.codemongers.com/Main
# * the Russian documentation - http://sysoev.ru/nginx/
#
#######################################################################
#----------------------------------------------------------------------
# Main Module - directives that cover basic functionality
#
# http://wiki.codemongers.com/NginxMainModule
#
#----------------------------------------------------------------------
user www www;
worker_processes 2;
error_log /var/log/nginx/error.log;
#error_log /var/log/nginx/error.log notice;
#error_log /var/log/nginx/error.log info;
pid /var/run/nginx.pid;
#----------------------------------------------------------------------
# Events Module
#
# http://wiki.codemongers.com/NginxEventsModule
#
#----------------------------------------------------------------------
events {
worker_connections 1024;
}
#----------------------------------------------------------------------
# HTTP Core Module
#
# http://wiki.codemongers.com/NginxHttpCoreModule
#
#----------------------------------------------------------------------
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
client_max_body_size 10M;
client_body_buffer_size 128k;
log_format main ' - [] '
'"" "" '
'"" ""';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
keepalive_timeout 20;
tcp_nodelay on;
# Load config files from the /etc/nginx/conf.d directory
include /etc/nginx/conf.d/*.conf;
#
# The default server
#
server {
listen 443;
server_name 127.0.0.2;
ssl on;
ssl_certificate /etc/nginx/sslconf/server.crt;
ssl_certificate_key /etc/nginx/sslconf/server.key;
location /phpmyadmin {
root /usr/html;
index index.php;
gzip on;
gzip_http_version 1.1;
gzip_vary on;
gzip_comp_level 6;
gzip_proxied any;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript text/x-js;
gzip_buffers 16 8k;
location ~* \.(jpg|png|gif|jpeg|css|js)$ {
expires 1h;
}
}
location ~ \.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/html$fastcgi_script_name;
fastcgi_param HTTPS on;
}
}
server {
listen 80;
server_name 127.0.0.2;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root /usr/html;
index index.html index.htm index.php;
}
location = /phpmyadmin/ {
rewrite ^ https://127.0.0.2$uri redirect;
}
error_page 404 /404.html;
location = /404.html {
root /usr/html;
}
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/html$fastcgi_script_name;
}
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
deny all;
}
}
# Load virtual host configuration files.
include /etc/nginx/sites-enabled/*;
}
答案1
从http://wiki.nginx.org/HttpGzipModule
gzip on;
gzip_min_length 1000;
gzip_proxied expired no-cache no-store private auth;
gzip_types text/plain application/xml;
gzip_disable "MSIE [1-6]\.";
从http://wiki.nginx.org/HttpHeadersModule
expires 24h;
expires modified +24h;
expires @15h30m;
expires 0;
expires -1;
expires epoch;
add_header Cache-Control private;
看起来其中一些选项存在于您的 phpmyadmin 区域内,但不在您的主网站配置中。