Nginx: bug using if in location, how do I rectify
I am using nginx in reverse proxy mode. In my server section I have this code to set expire and cache control of my static files.
location ~* ^.+\.(css|js|png|gif)$ {
access_log off;
expires max;
add_header Cache-Control public;
if (!-f $request_filename) {
proxy_pass http://localhost:82;
}
}
This is quite obviously creating issues.
Can someone help me correct this code to use try_files
or rewrite
?
Solution 1:
There are at least 2 techniques.
error_page
Before the introduction of try_files
directive the common method was to intercept error code 404 and process the request with a named location, e.g.
location ~* \.(css|js|png|gif)$ {
access_log off;
expires max;
add_header Cache-Control public;
error_page 404 = @upstream;
}
try_files
The equivalent using try_files
would be
location ~* \.(css|js|png|gif)$ {
access_log off;
expires max;
add_header Cache-Control public;
try_files $uri @upstream;
}
And the named location is the same for both scenarios:
location @upstream {
proxy_pass http://localhost:82;
}