Redirect to another html file - nginx

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.

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

How can I serve a particular file for root url in Nginx?

I need to serve file from my filesystem for GET / request. I tried the following:
location = / {
index page2257160.html;
root /var/www/site/;
}
The rest of requests are proxied to backend:
location / {
proxy_pass http://localhost:1234;
}
But when I do the request, instead of serving the file from filesystem, nginx asks backend about /page2257160.html, backend returns 404, nginx sends this 404 back to client.
How can I fix this?
The index directive performs an internal redirect, so you will need a second location to match the rewritten URI. For example:
root /var/www/site/;
location = / {
index page2257160.html;
}
location = /page2257160.html {
}
See this document for details.
You can achieve the same thing with one location block and a try_files directive. For example:
location = / {
root /var/www/site/;
try_files /page2257160.html =404;
}
See this document for more.

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

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