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

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.

Related

Nginx Rewrite Rule for the nested location

I have my domain config file setup for Nginx. I have set domain like www.example.com and I have all sub directory also inside that like basefolder/index.html, basefolder/abc1/signup/index.html, basefolder/abc2/signup/index.html, basefolder/signup/index.html etc as n number can be exists.
I can directly call them through domain/dir/ which will call the index.html file available in that directory. I have some specific query string set for the "signup" folders only like "www.example.com/signup/?query=a12seddf" or "www.example.com/abc1/signup/?query=a12seddf". I want to covert them to the path like "www.example.com/signup/a12seddf" or "www.example.com/abc1/signup/a12seddf". I tried with the rewrite rule but it works only if I give the whole location till the signup but I want to write the single rule which will work on all signup folder instead of writing the rewrite rule for all.
I tried below code but it won't work for me and giving 404 on accessing the beautify URL.
location ~* signup/ {
rewrite ^/signup/([a-z0-9A-Z]+)/?$ /signup/?query=$1 break;
}
Please find the whole sample working config file given below with names example.com.conf in nginx/sites-enabled folder:
server {
listen 80;
server_name example.com www.example.com;
root /var/www/html/sites/example.com;
error_page 404 /error_404.html;
location = /error_404.html {
root /var/www/html;
internal;
}
location ~* \.(?:ico|css|js|gif|jpe?g|png)$ {
expires 30d;
add_header Pragma public;
add_header Cache-Control "public";
}
rewrite ^/abc1/signup/(.*)$ /abc1/signup/index.html?_l=$1 last;
rewrite ^/signup/(.*)$ /signup/index.html?_l=$1 last;
}
Thanks.

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.

Vanilla Forums sitemapindex.xml file inaccessible by nginx

My forum is installed on the url: example.com/forums
I have used nginx and Vanilla to "prettify" the urls. I've set
/forum/conf/config.php, “RewriteUrls” to “True”.
and in my nginx.conf:
location /forums {
index index.php index.htm index.html;
location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|xml)$ {
access_log off;
log_not_found off;
expires 30d;
}
try_files $uri $uri/ #forums;
}
location #forums {
rewrite ^/forums(.+)$ /forums/index.php?p=$1 last;
}
The problem is I installed the sitemap plugin by Vanilla Forums.
and the resulting sitemap is supposed to be located at
example.com/forums/sitemapindex.xml
But when I navigate there nginx gives me a 404.
How do I solve this?
The problem is that the URI /forums/sitemapindex.xml is being processed by the location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|xml)$ block and not being forwarded to /forums/index.php.
If you do not serve static .xml files, you could simply remove the |xml term from the regular expression.
Otherwise, you will need to make that URI a special case, for example:
location = /forums/sitemapindex.xml {
rewrite ^ /forums/index.php?p=/sitemapindex.xml last;
}

with nginx, how do you rewrite a request to a different directory?

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.

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