Nginx : Serve JPG images from different root and location - nginx

I have 3 different stockage area : "avatars" , "articles", "trends" where I store my images.
I want to "link" the URL "/trends/123.jpg" to trends folder , "/avatars/23.jpg" to avatar and so on.
Configuration 1:
server
{
listen 8089;
server_name localhost;
root /var/www;
location /trends/
{
alias /var/storage/hottrend/;
}
location ~* ^.+\.(jpeg|gif|png|jpg)
{
add_header Cache-control "public";
access_log off;
expires 90d;
}
}
Configuration 1 : "GET /trends/123.jpg" never match /trends/ location, why ?
Configuration 2:
server
{
listen 8089;
server_name localhost;
root /var/www;
location ~ ^/trends/(.*)\.jpg$
{
rewrite ^/trends/(.*)$ /$1 break;
root /var/storage/hottrend;
}
location ~* ^.+\.(jpeg|gif|png|jpg)
{
add_header Cache-control "public";
access_log off;
expires 90d;
}
}
Configuration 2: The last rule with the caching stuff is not matched. What is the best approach to server JPG files from different location/root ?

The two configurations have different but related problems. The two issues are:
the order in locations are matched; and
what happens when a location is matched.
I'll first explain how it works, and then I'll address your configurations.
How it works
Location matching
You can find the details on this nginx wiki page, but I have always found the wording to be confusing. (It mixes implementation details in the description of behaviour.) What it means is that locations are matched in the following order:
exact matches like location = /robots.txt
eager non-regex prefixes like location ^~ /trends/
regex matches like location ~* \.(jpg|png), or case-sensitive location ~ \.(jpg|png)
lazy non-regex prefixes like location /trends/ or location /
If multiple regular expressions match, then the first match beats the others. If multiple non-regex prefix match, I think it selects the most specific match -- I'll check this and update.
Location behaviour
A matching location is responsible for serving the designated content. It is also responsible for providing cache-control headers and so on. You can have a location that matches particular URL patterns to apply specific headers, but that location must also serve the content. If it cannot serve the content, you will most likely get an 404 error -- it won't look for other matching locations.
Lastly, be extra careful if you have a rewrite within a location. An internal redirect can happen earlier than some directives, in which case, those directive may not apply before the redirect causes the locations to be searched again.
Configuration 1
Your trends location is a lazy non-regex prefix, so it would only match if the regex location fails to match. You can fix this by using an eager non-regex match, such as
location ^~ /trends {
...
}
However, doing so will highlight the other configuration problem.
Configuration 2
You have two locations that could potentially match jpg files. Only one will succeed. If the first matches, then the cache control of the second location won't be applied. If the second matches, then the alias won't take effect.
The fix is to make sure that all directives needed are applied within the location that matches. You can either be explicit in one file, such as
location ^~ /trends
{
alias /var/storage/hottrend;
add_header Cache-control "public";
access_log off;
expires 90d;
}
location ~* ^.+\.(jpeg|gif|png|jpg)
{
add_header Cache-control "public";
access_log off;
expires 90d;
}
The neater solution for managing directives that must be applied to several locations is to factor those details into another file, and then include it at both locations. (Mohammad AbuShady did so in his example.) Something like this:
# Inside your main .conf
location ^~ /trends
{
alias /var/storage/hottrend;
include image-headers.conf;
}
location ~* ^.+\.(jpeg|gif|png|jpg)
{
include image-headers.conf;
}
# Inside image-headers.conf
add_header Cache-control "public";
access_log off;
expires 90d;

well I don't usually use alias but I think this is a good example to use alias for, you can put your caching settings inside /etc/nginx/image_caching.conf. If trends is only used for images i would not try to complicate the regex and assume that it always has images
location /trends {
alias /var/storage/trends;
include /etc/nginx/image_caching.conf;
}
And for your example config, you should have not added /trends inside root /var/storage/trends; because it's already used in the location, so i think it would show 404 becuase it's looking for /var/storage/trends/trends/123.jpg

Nginx checks locations defined as regular expressions in the order in which they appear in the configuration file and uses the location with the first matching expression.
Therefore, you should make sure location ~\.jpg$ appears before any other locations defined as regular expressions

Related

NGINX ignore hash css file and fetch any css that exists

I'm cache-busting with hashed css files (app-123456.css). The css file requests are proxied to a cdn with nginx. I need to keep the files statically named on the cdn, as there is a requirement to allow the customer to modify some css and re-upload the file. How can I pass the hashed file request to cdn and return the statically named file? For example a request to app-123456.css would return app.css, if it existed on the cdn. I'm trying to use try files but have been unsuccessful. Will cache-busting still work in this scenario, if the returned file is statically named? Thanks for any help.
location ~* (.+)\.(?:\d+)\.(css)$ {
try_files $uri $1.$2 #styles;
}
location #styles {
autoindex on;
proxy_pass http://[url].net; # needs to go to http://[url].net/styles/
}
EDIT
location ~* (.+)-(?:\d+)\.(css)$ {
try_files $uri $1.$2 #styles;
}
location #styles {
autoindex on;
rewrite ^(.+)-(?:\d+)\.(css)$ /styles$1.$2 break;
proxy_pass http://[url].net; # needs to go to http://[url].net/styles/
}
Fixed
^(.+)\-([a-zA-Z0-9]*)\.(css)$
You need to modify the URI within the named location before passing it upstream with proxy_pass. This can be accomplished using a rewrite...break statement. See this document for details.
For example, using your updated regular expression:
location ~* ^(.+)\-(?:[a-zA-Z0-9]*)\.(css)$ {
try_files $uri $1.$2 #styles;
}
location #styles {
rewrite ^(.+)\-(?:[a-zA-Z0-9]*)\.(css)$ /styles$1.$2 break;
proxy_pass http://...;
}
The above solution basically applies the same regular expression to the URI twice, which seems inefficient and redundant.
If the /styles/ URI prefix is unique to the upstream server, you could perform the translation in the original try_files statement. See this document for details.
For example:
location ~* ^(.+)\-(?:[a-zA-Z0-9]*)\.(css)$ {
try_files $uri $1.$2 /styles$1.$2$is_args$args;
}
location ^~ /styles/ {
internal;
proxy_pass http://...;
}
The ^~ operator give the prefix location a high precedence (see this document for details) and the internal directive prevents the URI from being directly accessible (see this document for details).

How can I match any path containing a dot with nginx?

I am trying to implement a proxy using the nginx configuration
The idea is to have a http server hosting my website (my SPA). and having one route on my http server pointing to another api.
this is my nginw configuration file below
client_max_body_size 100M;
server {
listen 80;
server_name localhost;
root /usr/share/nginx/html;
index index.html index.htm;
location ^~ /proxyapi/ {
proxy_read_timeout 180s;
proxy_send_timeout 180s;
proxy_pass http://localhost:5000/;
}
location ~* /static/* {
try_files $uri =404;
expires 1y;
access_log off;
add_header Cache-Control "public";
}
# Any route that doesn't have a file extension (e.g. /devices)
location / {
try_files $uri $uri/ /index.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;
}
}
it works fine most of the time
when I call http://my-nginx-host/proxyapi/search/login, it works...
unless there is a dot in the login
http://my-nginx-host/proxyapi/search/login => works fine
http://my-nginx-host/proxyapi/search/log.in => fails with a "404 resource not found"
is there any way to make it work?
I couldn't find any solution
Matching any dot in URI can be done with this:
location ~* \..*$ {
# try_files $uri $uri/ =404;
# ...
}
Let's take the regex apart:
~* Tells nginx to do case-insensitive regex matching (~ for case-sensitive)
\. Matches to literal dot symbol .
.* The dot here equals to any symbol except whitespace, asterisk modifies the preceding symbol to "match as many of this as possible"
$ Matches end of line
You can use this regex if you want to match even "malformed" uris like log.in
EDIT: In your situation, you would have to place this regex after your
location ~* /static/* so it won't match uris with dots like /static/image.png. See notes for explanation.
Notes
Take in mind this location block will match any dot anywhere in passed URI. So it will match URIs like these /a.ssets/images/, /assets/favicon.ico too. Any non-terminating locations (ones without ^~ or =) will not be used even if they should match, if the dot regex matches and it's first matching regex location it takes precedence over anything else.
Important snippet from nginx's docs about matching preference of location:
Regular expressions are specified with the preceding “~*” modifier (for case-insensitive matching), or the “~” modifier (for case-sensitive matching). To find location matching a given request, nginx first checks locations defined using the prefix strings (prefix locations). Among them, the location with the longest matching prefix is selected and remembered. Then regular expressions are checked, in the order of their appearance in the configuration file. The search of regular expressions terminates on the first match, and the corresponding configuration is used. If no match with a regular expression is found then the configuration of the prefix location remembered earlier is used.
I was trying your solution but no luck. My problem is that context root location has a dot
https://example.com/something.else/test/me
I tried to match but no luck, I got 404 every time.
These are just some regex variation that I tried:
#location ~ /[\w.]+ {
#location ^~ ^/[\w.]+/[\w]+.* {
#location ^~ /[\w.]+/[\w]+.* {
#location ^~ [\w.]+/[\w]+.* {
#location ~* [\w.]+/[\w]+.* {
#location ~ ^[\w.]+/[\w]+.* {
location ^~ /something\.else {

nginx location with regular expression

i have the nginx server as reverse proxy for apache. just trying to have multiple locations with reg expressions but fail.
I want to set the expires header for all jpg's and png's but not for images in a specific folder
currently
location ~* ^(?!/login/)(.*)\.(jpg|png)$ { expires 30d; }
this works with request from
http://example.com/test/image.jpg
but not working with
http://example.com/login/image.jpg
also tried with several location
location ~ ^/login/$ { break; }
location ~* \.(jpg|png)$ { expires 30d; }
but not working. I also tried nested location but same problem, nothing work. Can anyone help me?
Thanks and kind regards, Tom

Expand location only for specific file types

I've a NGINX Reverse Proxy setup for my NodeJS app.
My location block looks basically like this
location / {
proxy_pass http://localhost:3000;
gzip on;
gzip_static on;
expires 86400;
add_header Cache-Control "public";
}
No I want to expand this location for specific file types (js,css,png, ..) with
expires: max;
In case I couldn't overwrite the initial expires header I would be fine leaving it in the location block.
My static files are all located in /static/.. and I just could setup a second location block, but this would generate some overhead (setting the proxy_pass again etc). So I'm curious if I can expand blocks
You'll need a second location block as proxy_pass directives (all handlers, for that matter, see this post) aren't inherited by nested location blocks.
If you want to simplify your configuration without copy-and-pasting your other configuration, consider an include directive and put the common aspects of your location block into an external file.
It is technically possible that you could get away with just one location block by adding an if statement like:
if ($request_uri ~ ^/static) {
expires max;
}
but as per the Nginx wiki's If is Evil page, you'll want to test this and very carefully. This also involves more of an overhead by invoking the regex engine; the better option is two location blocks.

Nginx location block for specific path and certain file types

I am having trouble defining a location block for certain paths and file types.
I am using wordpress and using a plugin which generates dynamic sitemaps..It redirects to path like sitemapindex.xml, which do not actually exist and nginx is trying to serve it statically.
I need to be able to pass this to apache
I need to send anything that is http://example.com/blog/*.xml to apache. This is what i am trying, which does not work.. so for instance:
http://example.com/blog/post.xml or http://example.com/blog/sitemapindex.xml
nginx config
server {
location ~* ^/blog/*.xml$ {
include /etc/nginx/proxy_params;
proxy_pass http://127.0.0.1:8080;
}
}
what is the correct syntax
Thanks
I had similar problem with my images. In my applications, images were being served from two different locations.
You can specify different sources based on url pattern. Your solution would then look something like this.
location ~* ^/blog/.+\.(xml)$ {
root /some/path/;
expires 90d;
}
location ~* \.(xml|js|jpg|png|css|html|otf|eot|svg|ttf)$ {
root /some/other/path/;
expires 30d;
index index.html;
}
Gotta escape that period
server {
location ~* ^/blog/.*\.xml$ {
proxy_pass http://127.0.0.1:8080;
}
}

Resources