Welcome! Log In Create A New Profile

Advanced

try_files and static content with query string (token/version)

Posted by compiler2k 
try_files and static content with query string (token/version)
May 13, 2014 05:28AM
Hi all.

I'm adding an nginx in front of an Apache webserver. The idea is to serve static content with nginx and dynamic content with Apache. I've moved Apache to port 8080 and created the following nginx configuration:

# ... = snip of extensions for readability purposes
location ~* \.(jpg|jpeg|gif|png|ico|css|...|js|htm|html)$ {
try_files $request_uri @apache;
}

location @apache {
include /etc/nginx/proxy.conf;
proxy_pass http://127.0.0.1:8080;
}

This works well with files like:

/modules/contextual/images/gear-select.png
/sites/all/themes/unm/images/twitter.png
/sites/all/themes/unm/images/icon-bg.png
/sites/all/themes/unm/favicon.ico

Those files are served from nginx and they never touch Apache.

My problem is with files like:

/sites/default/files/styles/medium/public/field/image/20130720.jpg?itok=u2KK3COA
/sites/all/libraries/superfish/css/superfish.css?n5i2r9

That's it: files with a token or version string in the query string (used to improve caching with expires max and allow correct image update in the browser by changing the token).

For those files, I need to "strip" the query string for the try_files, and match:

/sites/default/files/styles/medium/public/field/image/20130720.jpg?itok=u2KK3COA
---> strip query string
---> /sites/default/files/styles/medium/public/field/image/20130720.jpg (file exists)

/sites/all/libraries/superfish/css/superfish.css?n5i2r9
---> strip query string
----> /sites/all/libraries/superfish/css/superfish.css (file exists)

I first tested the following:

location ~* ^(.*\.)(jpg|jpeg|gif|png|ico|css|...|js|htm|html)(\?.*)$ {
try_files "$1$2" @apache;
}

But it didn't work. Those files continued reaching apache.

Then I read that location regexp matching doesn't include the query string, so I don't know how to "match" the "uri/filename.ext?string" pattern and how to test it against the filename without the query string.

I also tried:

# ... = snip of extensions for readability purposes
location ~* \.(jpg|jpeg|gif|png|ico|css|...|js|htm|html) {
try_files $request_uri $uri @apache;
}

(removing the $ at the end of the regexp matches everything, both file.png and file.png?version)

I thought that this will try first file.png?version on disk, then file.png and then a fallback to apache, but when I try that, the site is sloooooow, takes ages to load (timeouts? I don't know why).

So ... what I'm doing wrong? How do I "try_files" my static files with token? Must I use if() with something like:

location ~* ^(.*\.)(jpg|jpeg|gif|png|ico|css|...|js|htm|html) {

if ($is_args) {
# do something
# but ... if was supossed to break the next try_files!!! :?
}
try_files $request_uri @apache;
}

Thanks for any help.
Re: try_files and static content with query string (token/version)
May 20, 2014 03:01AM
Hi all...

Auto-update. I found this:

# http://blog.bripkens.de/2012/03/nginx-cache-busting/

server {
# ...

location ~* ^/static/(\w+)/([^/]+)_\d+\.(js|css|png|jpg|jpeg|gif|ico)$ {
alias /srv/www/static/$1/$2.$3;
add_header Vary Accept-Encoding;
expires max;
}

# ...
}


The problem is that if /srv/www/static/$1/$2.$3 does not exist, I lose the fallback to @apache ...

Any other idea? :?

Thanks for any help.
Re: try_files and static content with query string (token/version)
May 26, 2014 10:21AM
Well ... it seems that $uri is what I was looking for:

# ... = snip of extensions for readability purposes
location ~* ^(.*\.[^.]*)(jpg|jpeg|gif|png|...|woff|ttf|svg)(\?.*)?$ {
try_files $uri @apache;
}

I expanded the regexp a bit to be sure that I'm matching the text after the last dot in the uri.

Hope this helps somebody.
Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 69
Record Number of Users: 6 on February 13, 2018
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready