我有自己的CentOS 6和Nginx VPS,我想启用缓存.要测试它,如果成功启用,我会使用Google PageSpeed Insight.
我的问题是,我没有太多经验,我必须启用缓存,我可以设置图像缓存等多长时间.
多数民众赞成在互联网上发现并尝试到目前为止:
>创建目录:/ etc / Nginx / sites-available和/ etc / Nginx / sites-enabled因为它们不知道以某种方式存在.
>链接创建的目录:/etc/Nginx/Nginx.conf并添加include / etc / Nginx / sites-enabled / *;在文件的末尾,但在最后}之前
>创建文件/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
>服务Nginx重启
我使用我的网站wordpress.
因此,每当我使用PageSpeed Insight或其他pagespeed工具测试我的页面时,它表示我不使用缓存来获取header.png,javascripts等.但我没有得到一些错误,即使我用Nginx -t检查配置文件,这显示了这一点:
Nginx: the configuration file /etc/Nginx/Nginx.conf Syntax is ok
Nginx: configuration file /etc/Nginx/Nginx.conf test is successful
我忘记了什么吗?
这是我完整的Nginx配置:http://pastebin.com/wxnzzePT
conf.d文件夹中的default.conf:http://pastebin.com/KUH2tSrD
您的新文件仅在用户使用http:// localhost访问该站点时使用.此外,与default.conf文件相比,新文件配置使用不同的路径.
此外,位置块内的根指令是不好的做法.
所以,你的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;
#}
}