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.
Related
I've a static folder with this structure,
└── static
├── images
├── locales
└── robots.txt
I've set location directive for this folder to cache the files publicly.
location /static {
access_log off;
log_not_found off;
expires 1y;
autoindex off;
add_header Cache-Control "public";
}
I want to change the Cache-Control header to public, stale-while-revalidate=60, stale-if-error=60 for all the json files that are inside locales folder.
I've tried nested location but without success,
location /static {
access_log off;
log_not_found off;
expires 1y;
autoindex off;
add_header Cache-Control "public";
location /static/locales/.*/.*\.json$ {
expires 1w;
add_header Cache-Control "public,stale-while-revalidate=60, stale-if-error=60";
}
}
Apparently I was really close to the solution, all I needed is to add ~ in front of the regex at the nested location,
This works!
location /static {
access_log off;
log_not_found off;
expires 1y;
autoindex off;
add_header Cache-Control "public";
location ~ /static/locales/.*/.*\.json$ {
expires 1w;
add_header Cache-Control "public,stale-while-revalidate=60, stale-if-error=60";
}
}
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 use this code for add expires to static files:
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires 30d;
add_header Pragma public;
add_header Cache-Control “public”;
log_not_found off;
}
I have some random images in the virutual directory /image/. When the code above is in Nginx, the random images get a 404.
I dont need expires for the directory /image/. For this directory the code should not run. How is it possible to write something like this in Nginx language:
If not /image/
{
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$
{
expires 30d;
add_header Pragma public;
add_header Cache-Control “public”;
log_not_found off;
}
}
Try this
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$
{
if ($request_uri !~ ^/image/.*) {
expires 30d;
}
add_header Pragma public;
add_header Cache-Control "public";
log_not_found off;
}
When I post to a 404 page, nginx is stripping the post data and changing the request method to "GET". How do I prevent this? I have nginx configured to serve a php file as the error 404 page that is in the root of the domain directory. I would like to keep a log of whatever is posted to it.
nginx.conf
server {
listen 80;
error_page 404 = /404.php;
set $oldhost $host;
root /var/www/www/$oldhost;
location ~* \.(?:ico|css|js|gif|jpg|png|html|htm)$ {
expires 3d;
add_header Pragma public;
add_header Cache-Control "public";
}
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
location / {
index index.php index.htm index.html;
try_files $uri $uri/ /index.php?$args;
}
location ~ .php$ {
location ~ \..*/.*\.php$ {return 404;}
expires 1d;
add_header Pragma public;
client_max_body_size 80m;
add_header Cache-Control "public";
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
fastcgi_intercept_errors on;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
This happens because when nginx cannot find a page, it will redirect (GET) the user to that 404.php page you defined.
What you need to do is to add it to the try_files directive, so nginx will try to post to it when it cannot find the others, or make it that you can handle 404 error directly on your index.php (like wordpress does).
EDIT:
location / {
index index.php index.htm index.html;
try_files $uri $uri/ /index.php?$args 404.php 404.php?$args;
}
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!