简体   繁体   中英

nginx WordPress config with caching

Am on a RHEL server and having a beast of a time getting my nginx config to serve a WP site with caching. I'd like to handle caching at the nginx layer instead of futzing around with WP plugins, as I think it will be more reliable (with potentially using the nginx cache purge plugin just to help WP handle purging). Have yet to find a combination of config settings that actually return HITs on the cache. Config below - hopefully y'all see something that I'm missing (I've stripped out all of the caching config as none of it has worked - this is basically a straight pull from the nginx site for standard WP setups):

add_header Fastcgi-Cache $upstream_cache_status;

# Upstream to abstract backend connection(s) for php
upstream php {
server unix:/tmp/php-cgi.socket;
server 127.0.0.1:9000;
}

server {
listen   80;
listen   [::]:80;
listen   443 ssl;

## Your website name goes here.
server_name intranet-test.*;
root /var/www/html/intranet;
index index.php index.html index.htm;

ssl_certificate        /etc/httpd/conf.d/certificates/intranet-test.cer;
ssl_certificate_key  /etc/httpd/conf.d/keys/intranet-test.key;

if ($scheme = http) {
return 301 https://$server_name$request_uri;
}

location = /favicon.ico {
log_not_found off;
access_log off;
}

location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}

location / {
try_files $uri $uri/ /index.php?$args;
}

location ~ \.php$ {
include fastcgi.conf;
fastcgi_intercept_errors on;
fastcgi_pass php;
}

location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires max;
log_not_found off;
}

}

What needs to be added to that to set up nginx's cache?

Change

location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires max;
log_not_found off;
}

To

location ~* \.(?:ico|jpg|jpeg|png|gif|svg|js|css|swf)$ {
    add_header Cache-Control "public";
    add_header X-Frame-Options "SAMEORIGIN";
    expires 1y;
    access_log off;
}

location ~* \.(?:ttf|eot|woff|otf|woff2)$ {
    add_header Access-Control-Allow-Origin "*";
    add_header Cache-Control "public";
    add_header X-Frame-Options "SAMEORIGIN";
    expires 1y;
    access_log off;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM