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!
Related
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.
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.
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.
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.
on site, favicon.ico is kept in the /images/ directory, not the root. how do I tell nginx to look there?
have tried - which looks right but does not work:
location = /favicon.ico$ { rewrite /(.*) /images/$1 last; }
returns 404 Not Found
the file is there: requesting http://www.example.com/images/favicon.ico is successful
location = /favicon.ico {
root /path/to/your/images;
}
http://nginx.org/en/docs/http/request_processing.html
http://nginx.org/en/docs/http/converting_rewrite_rules.html
http://nginx.org/r/location
http://nginx.org/r/root
http://nginx.org/r/alias
http://wiki.nginx.org/Pitfalls
We can also solve this with rewrite. (I had to solve it using rewrite, because I'm using a try_files directive.)
Here's my config:
# Long cache times for static content
location ~ /_/static/(.+)$ {
# hold the last five versions of our static content
try_files
/_/static_477526f-master/$1
/_/static_05c8613-release/$1
/_/static_05c8613-master/$1
/_/static_db26497-release/$1
/_/static_db26497-master/$1;
expires 365d;
add_header Cache-Control public;
add_header Access-Control-Allow-Origin *;
}
location = /favicon.ico {
rewrite . /_/static/favicon.ico;
expires 14d;
add_header Cache-Control public;
}
The . regex matches anything, and the second entry rewrites the url. Since this is a favicon-specific location, we can just hardcode it instead of using regex capturing and $1.