Expire rulesets cause routing issue with CMS/Frameworks when not found
travisbotello opened this issue · comments
Hey everyone,
I moved my CMS/MVC Frameworks from Apache to Nginx using h5bp nginx configs. So far everything is working except for correct routing of missing static files that are declared in expires.conf ruleset.
If a static file declared in expires.conf ruleset is not found, the request is not being routed to index.php and therefore not handled by my cms/framework controller. This leads to massive problems which file managers/seo redirects. For example if some url is a virtual path that should be modified by my controller, redirected to the new location or just modifying the url nginx just returns error 404 and breaks.
In Apache environment this is not a problem at all. Of course I'd like to use the expires.conf ruleset for my static files as well, but not being able to handle my seo etc. stuff in my controller, activating these rules is not an option.
Any idea or setup for using the expires.conf alongside my described use case?
This is how I setup my vhost with php-fpm for Wolf CMS as an example. Same applies to Laravel or MODx. Expires.conf is included in basic.conf
server {
listen 80;
error_log /var/log/nginx/domain-error.log;
access_log /var/log/nginx/domain-access.log main;
# The host name to respond to
server_name domain.com;
# Path for static files
root /usr/share/nginx/www/public_html;
# Specify a charset
charset utf-8;
location / {
try_files $uri $uri/ /index.php?WOLFPAGE=$uri&$args;
}
index index.php index.html index.htm;
location ~ \.php$ {
try_files $uri =404;
fastcgi_intercept_errors on;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass php-fpm;
}
# Include the basic h5bp config set
include h5bp/basic.conf;
}
It might be a solution to apply the expire rules only if the file type in question is found. But I have absolutely no idea how to accomplish this.
Thanks in advance
Okay, found a solution. I included the try_files directive in the expires.conf rulesets like this:
# Media: images, icons, video, audio, HTC
location ~* \.(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc)$ {
try_files $uri $uri/ /index.php?WOLFPAGE=$uri&$args;
expires 1M;
access_log off;
add_header Cache-Control "public";
}
Any other idea because this will be hard to maintain since I have to create different try_files directive for each of my cms/frameworks depending on its needs.
Hi @travisbotello, I run into a similar issue with Kirby CMS, and this worked for me:
# Media: images, icons, video, audio, HTC
location ~* \.(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc)$ {
error_page 404 = /index.php; # pass requests for dynamically generated files to the application
add_header Cache-Control "max-age=2592000";
access_log off;
}
It took some searching, but in the end I found it close to ‘home’: the above, but also other solutions, can be found in the common problems section of the h5bp nginx documentation: https://github.com/h5bp/server-configs-nginx/blob/master/doc/common-problems.md#cannot-dynamically-serve--requests
Thanks @jolantis this might work in Kirby CMS, but I had some problems using the error_page 404
directive when my controller needs to rewrite the path. Using error_page 404
I ended up in seeing the home/index page of my cms/framework instead of processing the correct path.
Maybe my situation/use case is very specific but we should think about adding a comment to the Expires.conf
regarding potential issues.
Any idea or setup for using the expires.conf alongside my described use case?
I think this is all covered in https://github.com/h5bp/server-configs-nginx/blob/master/doc/common-problems.md
specifically https://github.com/h5bp/server-configs-nginx/blob/master/doc/common-problems.md#change-to-use-a-404-front-controller
we should think about adding a comment to the Expires.conf regarding potential issues.
I disagree since the problem you're describing applies to any location block - and there's already docs explaining how nginx acts and how to deal with it :)