Pet*_*ter 3 nginx cache wordpress
我有自己的带有 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;
}
Run Code Online (Sandbox Code Playgroud)
}
链接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 工具测试我的页面时,它都会说我不为 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
Run Code Online (Sandbox Code Playgroud)
我是不是忘记了什么?
这是我完整的 nginx 配置:http : //pastebin.com/wxnzzePT
在default.conf
从conf.d
文件夹:http://pastebin.com/KUH2tSrD
您需要将缓存指令添加到您的default.conf
文件中,并删除您创建的这个新文件。
您的新文件仅在用户使用http://localhost
. 此外,与您的default.conf
文件相比,您的新文件配置使用了不同的路径。
此外,块root
内的指令location
是不好的做法。
所以,你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;
#}
}
Run Code Online (Sandbox Code Playgroud)