我有我自己的VPS与CentOS 6和Nginx,我想启用caching。 要testing它,如果启用成功,我使用Google PageSpeed Insight。 我的问题是,我没有太多的经验,我必须启用caching,我可以设置多长时间的图像caching等等。 多数民众赞成在互联网上发现,并尝试到目前为止:
/etc/nginx/sites-available和/etc/nginx/sites-enabled因为它们不存在。 /etc/nginx/nginx.conf添加include /etc/nginx/sites-enabled/*; 在文件的末尾,但在最后} 创build文件/etc/nginx/sites-available/my-site.com.conf :
server { listen 80; server_name localhost; location / { root /usr/share/nginx/html; index index.html index.htm; } location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ { expires 15d; } location ~* \.(pdf)$ { expires 30d; }
}
链接conf文件: ln -s /etc/nginx/sites-available/my-site.com.conf /etc/nginx/sites-enabled/my-site.com.conf
service nginx restart 我用我的网站的WordPress。
因此,无论何时使用PageSpeed Insight或其他pagespeed工具testing我的页面时,都会说我不使用caching来保存header.png,javascript等。 但我没有得到一些错误,即使我检查与nginx -t的configuration文件nginx -t显示此:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
我忘记了什么吗?
这是我完整的nginxconfiguration: http : //pastebin.com/wxnzzePT
conf.d文件夹中的default.conf文件: http : //pastebin.com/KUH2tSrD
您需要将caching指令添加到您的default.conf文件,并删除您创build的这个新文件。
您的新文件仅在用户使用http://localhost访问站点时使用。 另外,与default.conf文件相比,新的文件configuration使用了不同的path。
此外, location块内的root指令是不好的做法。
所以,你的default.conf应该是这样的:
# # The default server # server { listen 80 default_server; server_name 213.165.xx.xx; #charset koi8-r; #access_log logs/host.access.log main; # Load configuration files for the default server block. include /etc/nginx/default.d/*.conf; root /var/www/wordpress; location / { index index.html index.htm index.php; try_files $uri $uri/ /index.php?q=$request_uri; } location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ { expires 15d; } location ~* \.(pdf)$ { expires 30d; } location /admin { auth_basic "Administrator Login"; auth_basic_user_file /var/www/admin/.htpasswd; } #!!! IMPORTANT !!! We need to hide the password file from prying eyes # This will deny access to any hidden file (beginning with a .period) location ~ /\. { deny all; } error_page 404 /404.html; location = /404.html { root /usr/share/nginx/html; } # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/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$ { root /var/www/wordpress; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} }