Nginx Rule - if not in directory - nginx

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;
}

Related

Nginx reduce repeat directive

I have bunch of location directive in my Nginx config and most of them have same add_header, is there any way that I can introduce variable in nginx or any generator for all this config? specially for location directive.
is there any way to set default for add_header so stop repeating in each location directive?
here is 3 locations out of 50 directive in my config:
location ^~ /example/1/ {
proxy_buffering off;
proxy_pass http://10.10.10.113:8124/geoserver/ws/wms;
add_header Pragma no-cache;
add_header Cache-Control private;
add_header Cache-Control no-cache;
add_header Cache-Control no-store;
add_header Expires -1;
}
location ^~ /example-org {
proxy_buffering off;
proxy_pass http://10.10.10.199:9126/geoserver/wms;
add_header Pragma no-cache;
add_header Cache-Control private;
add_header Cache-Control no-cache;
add_header Cache-Control no-store;
add_header Expires -1;
}
location ^~ /example-org/1 {
proxy_buffering off;
proxy_pass http://10.10.10.199:18124/geoserver/wms;
add_header Pragma no-cache;
add_header Cache-Control private;
add_header Cache-Control no-cache;
add_header Cache-Control no-store;
add_header Expires -1;
}
edit #1 (after Richard Smith comment)
add_header Pragma no-cache;
add_header Cache-Control private;
add_header Cache-Control no-cache;
add_header Cache-Control no-store;
add_header Expires -1;
location ^~ /example/1/ {
proxy_buffering off;
proxy_pass http://10.10.10.113:8124/geoserver/ws/wms;
}
location ^~ /example-org {
proxy_buffering off;
proxy_pass http://10.10.10.199:9126/geoserver/wms;
}
location ^~ /example-org/1 {
proxy_buffering off;
proxy_pass http://10.10.10.199:18124/geoserver/wms;
}

Set cache-control header for a specific sub-folder inside statics folder

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";
}
}

Trying to add another location directive in an nginx server with proxy cache

I have a website with a couple of pages and images and I have setup an nginx server to handle the website. When I add a location directive to handle the images, the entire website does not show up and look broken.
The website looks perfect when the bottom of my nginx file looks like this:
location = /favicon.ico {
access_log off;
log_not_found off;
}
location = /robots.txt {
allow all;
access_log off;
log_not_found off;
}
location ~ /\. {
deny all;
}
#location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
# proxy_cache sinuscache;
# add_header Pragma public;
# add_header Cache-Control "public";
# expires 1d;
# log_not_found off;
#}
location ~* (\.bak|\.off|\.config|\.exe|\.sql|\.fla|\.psd|\.ini|\.log|\.sh|\.inc|\.swp|\.dist)$ {
deny all;
add_header Pragma public;
add_header Cache-Control "public";
expires -1d;
access_log off;
}
location / {
include /etc/nginx/sites-settings/denyips.conf;
proxy_pass http://127.0.0.1:9099;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_cache sinuscache;
}
}
When I un-comment
#location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
# proxy_cache sinuscache;
# add_header Pragma public;
# add_header Cache-Control "public";
# expires 1d;
# log_not_found off;
#}
The website breaks. If I do a
service nginx configtest
I receive no errors.
The new location block is incomplete - as a minimum you will want to include proxy_pass http://127.0.0.1:9099; and probably many of the other statements from the location / block. See how nginx processes a request.
Some of the statements can be placed into the server block to avoid replication. For example:
proxy_set_header X-Forwarded-For $remote_addr;
proxy_cache sinuscache;
location / {
include /etc/nginx/sites-settings/denyips.conf;
proxy_pass http://127.0.0.1:9099;
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
include /etc/nginx/sites-settings/denyips.conf;
proxy_pass http://127.0.0.1:9099;
add_header Pragma public;
add_header Cache-Control "public";
expires 1d;
log_not_found off;
}

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.

Set Nginx expires header for all js and css except some files

I use Nginx config to set js, css expires header
location ~* \.(js|css)$ {
expires 30d;
}
How can I skip specific files (ex: abc.js, style.css) from being cached?
Try:
location ~* \.(js|css)$ {
location ~* (file\.js|name\.css)$ {
expires off;
}
expires 30d;
}
or
location ~* (file\.js|name\.css)$ {
expires off;
}
location ~* \.(js|css)$ {
expires 30d;
}
This should do it.
location ~* \.(js|css)$ {
expires 30d;
}
location = /path/to/abc.js { expires off; }
location = /path/to/style.css { expires off; }
See the location documentation for a full explanation.

Resources