nginx how to include rewrite outside of root - nginx

How do you call get a file that outside of the root to the be processed. i was reading about the alias and couldnt get it working. so ive tried adding a new root within the location no luck.
this is a cutdown of my config file
server {
listen 443;
server_name domain.com;
---
root ../var/www/domain/public_html;
# works as being called form within the root
location /login {
rewrite ^/login /account/login permanent;
}
#need to
location /validation/code.png {
root /var/www/domain/include;
rewrite ^/validation/code.png /captcha/display_captcha.php;
}
}

You are rewriting the png file to a php file. This will create a sub request for /captcha/display_captcha.php. Is there a location for php in your config? Assuming the php location uses the general root, when the sub request hits this, /captcha/display_captcha.php will not be found an you will get a 404 error.
Your best bet is to copy the php location and create a php location specifically for the php file.
server {
listen 443;
server_name domain.com;
root /var/www/domain/public_html;
...
location = /validation/code.png {
rewrite ^/validation/code.png /captcha/display_captcha.php;
}
location ~ ^/captcha/display_captcha.php {
root /var/www/domain/include
...
# copy php processing code from normal php location.
}
}
Better still, just use '/captcha/display_captcha.php' directly in your html and drop '/validation/code.png' altogether.

Related

Same root folder for multiple website different robots.txt

I have a first website with this configuration:
server {
server_name mywebsite.com;
root /var/www/website/prod;
...
}
and a second one like this with the same root folder:
server {
server_name pro.mywebsite.com;
root /var/www/website/prod;
...
}
And the only problem is they have the same robots.txt file.
Is it possible to tell the second server to link the file robots.txt to another file like robots-pro.txt?
And by the way the url http://pro.mywebsite.com/robots.txt would open the file robots-pro.txt.
For the main domain I did to block robots-denyall.txt:
location /robots-denyall.txt {
return 404;
}
And for the subdomain which I don't want to be referenced:
location = /robots.txt {
alias /var/www/mywebsite/prod/robots-denyall.txt;
}

Nginx multiple domains shared resources

I have the following folders:
/web/domain1/
/web/domain2/
/web/shared/
I want domain1 and domain2 to share static files from /web/shared/ but I am having trouble creating the mapping in nginx.
domain1: /assets/ mapped to /web/shared/
domain2: /admin/assets/ mapped to /web/shared/
server{
server_name domain1;
root /web/domain1/;
location / {
rewrite /assets/(.*) /web/shared/$1;
}
}
This gives me 404 error.
Define a location for URIs that begin with /assets/ (see this document for details). Use the alias directive, as the root directive cannot be used in this case (see this document for details).
For example:
location /assets/ {
alias /web/shared/;
}
This works
location /assets/(.*) {
alias /web/shared/$1;
}

Redirect to another html file

I have configuration file:
server {
listen 80;
root /path/to/file/;
location /api/ {
proxy_pass http://0.0.0.0:5000/api/;
}
location /docs/ {
index /path/to/another/index/filename.html;
}
}
But when i'm trying to call /docs/ in browser i see in error.log that server trying to find docs file in root /path/to/file/ instead of returning another html file from second location block.
How can i proxy request /docs/ to another docs.html that located on file system?
index just describes what file to serve per default. The keyword you need is root:
location /docs {
root /path/to/another/index;
index filename.html;
}
Now the web server will serve /path/to/another/index/filename.html when the route /docs/ is accessed.

Nginx rewrite location path

Is there a way I can add a rewrite which will dynamically set the location/path of the file?
The following is from the nginx config:
server {
root /media;
server_name media.domain.com;
location / {
autoindex off;
}
I have images with names like "e9m7L4_1.jpg" that are stored in a directory according to the first 6 letters/numbers of the filename, for example:
e9m7L4_1.jpg (stored in)-> e/9/m/7/L/4/e9m7L4_1.jpg
km40lj_1.jpg (stored in)-> k/m/4/0/l/j/km40lj_1.jpg
Currently I can access it like this:
http://media.domain.com/e/9/m/7/L/4/e9m7L4_1.jpg
Is there a way to rewrite the location using filename passed to nginx so it could be accessed like this, without the long directory path/prefix:
http://media.domain.com/e9m7L4_1.jpg
Thanks
You could try this:
server {
server_name media.domain.com;
root /media;
location / {
rewrite ^/((.)(.)(.)(.)(.)(.).+)$ /$2/$3/$4/$5/$6/$7/$1 break;
}
}

nginx redirect to external folder

This is my nginx vhost conf:
upstream demo {
server 127.0.0.1:9002 max_fails=250 fail_timeout=180s;
}
server {
listen 80;
server_name _;
root /home/web/public;
location /demo/ {
proxy_pass http://demo/;
}
location /demo/assets/ {
#this is where I need to point localhost/demo/assets (and all of it's subfolders) to another folder
#the line below obviously doesn't work
root /home/user/Workspaces/demo/public;
}
}
The root folder is pointing to /home/web/public, but I need the url localhost/demo/assets/ and all of it's subfolders to go to another (external) folder. How do I do that?
I'm running nginx 1.4.3
You should use the alias directive, as well as read the docs. Just a quote from it:
A path to the file is constructed by merely adding a URI to the value of the root directive. If a URI has to be modified, the alias directive should be used.
Example:
location /demo/assets/ {
alias /home/user/Workspaces/demo/public/;
}

Resources