nginx redirect to external folder - nginx

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

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 serve multi index files from location

I want to archive that serving variable html file through different uri, below is my config.
server {
listen 8888;
server_name localhost;
location / {
root html/test
index foo.html
}
location /another {
root html/test
index bar.html
}
}
I want request for localhost:8888/another then response bar.html which present in my test directory, but I'm failed :(
how could I fix above config, thanks for your time.
The filename is constructed from the value of the root directive and the URI. So in this case:
location /another {
root html/test;
index bar.html;
}
The URI /another/bar.html will be located at html/test/another/bar.html.
If you want the value of the location directive to be deleted from the URI first, use the alias directive.
location /another {
alias html/test;
index bar.html;
}
The URI /another/bar.html will be located at html/test/bar.html.
See this document for details.

nginx how to include rewrite outside of root

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.

Resources