Shiv Shiv - 1 year ago 174
PHP Question

Nginx Caching 404

I am using WordPress with Nginx but whenever I try to enable caching of static files they turn to 404 not found.

This is my


server {
listen 80;
server_name _;

# SSL configuration
listen 443 ssl default_server;
ssl_certificate /etc/letsencrypt/live/;
ssl_certificate_key /etc/letsencrypt/live/;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_dhparam /etc/nginx/ssl/dhparams.pem;
ssl_session_timeout 30m;
ssl_session_cache shared:SSL:10m;
ssl_buffer_size 8k;
add_header Strict-Transport-Security max-age=31536000;

location / {
root /home/shivam/sites/;
index index.php index.html index.htm;
try_files $uri $uri/ /index.php?$args;

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
#location ~ \.php$ {
# proxy_pass;

# pass the PHP scripts to FastCGI server listening on
location ~ \.php$ {
root /home/shivam/sites/;
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;

gzip on;
gzip_vary on;
gzip_proxied any;
gzip_comp_level 9;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_types text/plain text/css application/json applicationx-javascript text/xml application/xml application/xml+rss text/javascript;

include wordpress/wordpress.conf;

And this is the WordPress.conf file:

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

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

# Deny all attempts to access hidden files such as .htaccess, .htpasswd, .DS_Store (Mac).
# Keep logging the requests to parse later (or to pass to firewall utilities such as fail2ban)
location ~ /\. {
deny all;

# Deny access to any files with a .php extension in the uploads directory
# Works in sub-directory installs and also in multisite network
# Keep logging the requests to parse later (or to pass to firewall utilities such as fail2ban)
location ~* /(?:uploads|files)/.*\.php$ {
deny all;

# Add trailing slash to */wp-admin requests.
rewrite /wp-admin$ $scheme://$host$uri/ permanent;

With that, it works fine. But if I add

# Media: images, icons, video, audio, HTC
location ~* \.(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc)$ {
expires 1M;
access_log off;
add_header Cache-Control "public";

# CSS and Javascript
location ~* \.(?:css|js)$ {
expires 1y;
access_log off;
add_header Cache-Control "public";

To the end of wordpress.conf and restart nginx and reload my site all the static files come up with 404.

So I have to comment that caching out and it works fine.

Any ideas?

Single 404

This means that if I include wp-rocket.conf (I use it) it also gives a 404 because wp-rocket.conf has caching stuff in it.

So basically, why can't i cache static files?

Answer Source

It's because you've defined your root in the root location block. So the other location blocks don't know about it. This is bad practice (see here). These location blocks don't know where to look for the files.

You should have the root /home/shivam/sites/; above your location / { block and then add the try_files $uri $uri/ /index.php?$args to each of your static blocks.

Another way to do it is to keep your config, but add alias /path/to/static/files/; to each block.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download