Quantcast
Channel: Nginx Forum - How to...
Viewing all articles
Browse latest Browse all 4759

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

$
0
0
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.

Viewing all articles
Browse latest Browse all 4759

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>