nginx serve multi index files from location - nginx

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.

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