Nginx reads file from multiple locations - nginx

Pardon me, I'm new to nginx, if the question is vague, please point me to some directions.
I have an image xyz.jpg in Disk-1, and an image abc.jpg in Disk-2, when I call url:../image/xyz.jpg, the xyz.jpg (Disk-1) shows up by using alias in my nginx's file . Question is:
Is there any way to call ../image/abc.jpg, the abc.jpg (Disk-2) will show up ?
location /image/ {
expires max;
add_header Pragma public;
add_header Cache-Control "public";
alias /image_data/;
}
Edit: I used try_files but it didn't work
location #image2 {
alias /image_data_2/;
try_files $uri $uri/ =404
}
location /image/ {
expires max;
add_header Pragma public;
add_header Cache-Control "public";
alias /image_data/;
try_files $uri $uri/ #image2;
}

So you want to check two folders for images to match URIs beginning with /image/.
You could use a regular expression location block to capture the part of the URI following the prefix, which avoids using the alias directive. The regular expression locations are evaluated in order, so the placement of the block within the server block is significant. See this document for details.
Within the protection of the location block, the root is set to a common parent directory (in this case /) and construct the path to the files using try_files terms.
For example:
location ~ ^/image/(.*)$ {
expires max;
add_header Pragma public;
add_header Cache-Control "public";
root /;
try_files /image_date/$1 /image_data_2/$1 =404;
}
See this document for details.

Related

Setting cache-control on all folders' assets except one

I need to apply the following location rule for every single folder of my app with the exception of /forum and its children:
location ~* \.(?:jpg|jpeg|gif)$ {
expires 1M;
access_log off;
add_header Cache-Control "public";
}
I tried out setting it to location ~* ^/forum/.*\.(?:jpg|jpeg)$ but it doesn't look like it's working the way I want it to.
This is the solver I came up with:
location /forum {
...
}
location / {
location ~* \.(?:jpg|jpeg|gif)$ {
...
}
...
}
Simply separate the location directives and adjust commands accordingly.

NGINX: search file in subdirectories

Part of nginx config:
location = / {
root www/html;
try_files $uri $uri/ $uri.html;
}
location ~* \.(jpg|jpeg|png)$ {
root www/resources/img;
try_files $uri $uri/;
#autoindex on;
expires 1y;
add_header Cache-Control public;
}
location ~* \.(css)$ {
root www/resources/css;
expires 1y;
add_header Cache-Control public;
}
location ~* \.(js)$ {
root www/resources/js;
expires 1y;
add_header Cache-Control public;
}
In directory www/resources/img I have 1 image file 1.jpg and 1 subfolder which contain another file 2.jpg. So, if I do request like localhost/1.jpg I get my image, but if I do request localhost/2.jpg it returns 404 not found. How to setup nginx to search file in subfolders?
Try changing below block of code, for if the file not found in root, find it in /subfolder/$uri,
location ~* \.(jpg|jpeg|png)$ {
root www/resources/img;
try_files $uri /subfolder/$uri $uri/;
#autoindex on;
expires 1y;
add_header Cache-Control public;
}
Switch your error_log on, to see which file is being accessed, and you might correct from their as well.

Nginx conf exclude directory from location ~* \.(?:js)$

How can I exclude all URLs with a directory called dynamic in the following location block:
location ~* \.(?:js)$ {
expires 1y;
access_log off;
add_header Cache-Control "public";
}
Here's the entire config, most of this comes from herokus php nginx buildpack
http://pastebin.com/xQ4BDtwr
( stackex won't let me post "mostly code" )
I would add the following location:
location /path/to/dynamic/ {
location ~* \.js$ {internal;}
}
The key is to override the ~* \.(?:js)$ regex location with a prefix location. Then you don't have to worry about where it appears in your config.
It could be solved with another regex location ~ /dynamic/.*\.js$ {internal;}, but then you would need to be sure it always comes before the ~* \.(?:js)$ location; another problem waiting to happen when your config grows.

Fallback to default/shared file in nginx

I would like to have a default robots.txt file served from a shared place (absolute path), if there's no relative one.
I tried this without luck:
location = /robots.txt {
expires 30d;
add_header Cache-Control public;
try_files /robots.txt /var/www/shared/robots.txt =404;
}
But it just returns 404.
I ended up with this which seems to work:
location = /robots.txt {
expires 30d;
add_header Cache-Control public;
try_files /robots.txt #shared;
}
location #shared {
root /var/www/shared;
}
I must admit though, that I liked "try_files" better, but it just doesn't seem to work with absolute paths.
If anyone has a better/other solution, I would love to see!

nginx - serve only images

I'm trying to setup nginx so "static.domain.com" can only serve images. This is what I have come up with, but I know it can be done more efficiently. I want to serve 403.html if someone tries to access any .htm, .php, directory (anything else I'm missing?) files. Of course, with the exception of 403.htm and static.htm files.
Any ideas how I can secure this properly?
server {
listen xx.xx.xx.xx:80;
server_name static.domain.com;
root /www/domain.com/httpdocs;
index static.htm;
access_log off;
error_log /dev/null crit;
error_page 403 /403.html;
# Disable access to .htaccess or any other hidden file
location ~ /\.ht {
deny all;
}
location ~* \.php {
deny all;
}
# Serve static files directly from nginx
location ~* \.(jpg|jpeg|gif|png|bmp|ico|pdf|flv|swf|exe|html|htm|txt|css|js) {
add_header Cache-Control public;
add_header Cache-Control must-revalidate;
expires 7d;
}
}
Why not move the images up and then deny all?
location ~* \.(jpg|jpeg|gif|png|bmp|ico|pdf|flv|swf|exe|html|htm|txt|css|js) {
add_header Cache-Control public;
add_header Cache-Control must-revalidate;
expires 7d;
}
location / {
deny all;
}
there is no syntax for NOT matching a regular expression. Instead, match the target regular expression and assign an empty block, then use location / to match anything else. -From http://wiki.nginx.org/HttpCoreModule#location
Edit: Removed "=" from "location /"
To quote the docs:
location = / {
# matches the query / *only.*
}
location / {
# matches *any query*, since all queries begin with /, but regular
# expressions and any longer conventional blocks will be
# matched first.
}
My bad.

Resources