Nginx configuration location and index - nginx

This is how my configuration looks like...
server {
listen 80;
server_name 127.0.0.1 localhost;
location = / {
index index.html;
}
location / {
root /etc/nginx/html/app1;
}
}
In my folder app1 I have two files, index.html & home.html
If I browse http://localhost/ or http://localhost/index.html or http://localhost/home.html page it comes up well.
When I change the configuration like so...
server {
listen 80;
server_name 127.0.0.1 localhost;
location = / {
index home.html;
}
location / {
root /etc/nginx/html/app1;
}
}
http://localhost:8888/index.html > works
http://localhost:8888/home.html > works
http://localhost:8888/ > 403 forbidden!!!
Can someone please tell me what is wrong?

Because the priority of location = {} is higher location / {}.
So it first matches the location = {}.
In you case, there isn't root in the location = {}.

Related

ngnix URL with context(virtual) path

My ngix site config file is given below. I want to add context path to my URL
I can access site by http://localhost:8888, but I want to add context path to my site URL like http://localhost:8888/MyApp
server {
listen 8888;
server_name localhost;
location{
root "C:/nginx/Share/dist";
index index.html index.htm;
}
}
Thanks in advance
You need to change the base location for this
server {
listen 8888;
server_name localhost;
location / {
# since we have nothing on root we can redirect to /MyApp/ if we want
return 302 /MyApp;
}
location /MyApp {
root "C:/nginx/Share/dist";
index index.html index.htm;
}
}

how to configure nginx for different paths

I would like to configure nginx for different locations on / and I have the following config.
server {
listen 80;
server_name bla.com;
location = / { // on exact route match such as 'bla.com'
root /usr/share/nginx/html/folder; // it has index.html
}
location / { // rest all url such as bla.com/* are captured here eg. bla.com/temp/12
root /usr/share/nginx/html/dist; // different folder
}
}
How can I do this?
I also tried the below config with no luck -
server {
listen 80;
server_name bla.com;
root /usr/share/nginx/html;
location = / {
try_files $uri /folder/index.html; // it has index.html
}
location / { // rest all url such as bla.com/* are captured here eg. bla.com/temp/12
try_files $uri /dist/index.html; // different folder
}
}
You can achieve this by creating something like this:
server {
index index.html index.htm;
server_name test.example.com;
location / {
root /var/www/website1/public_html;
}
location /temp {
root /var/www/website2/public_html;
}
}
You can also do this with individual files like this:
location /robots.txt {
alias /var/www/website/public_html/robots.txt;
}

nginx how to serve 'admin' subdomain request fro a specific root folder?

Is it possible to serve all requests to example.com ( and www.example.co)
from the root folder
location / {
root html;
index index.html index.htm;
}
but all requests to admin.example.com from a another folder 'admin' on the same server ?
The admin.example.com should be configured as a separate server in nginx config, but noone restricts you from pointing its root inside other server's root directory
server {
listen 80;
server_name admin.example.com;
root /some/path/example.com/admin;
location / {
index index.html;
}
}
server {
listen 80;
server_name www.example.com example.com;
root /some/path/example.com;
location / {
index index.html index.htm;
}
}
Use different server directives, this way:
server {
server_name admin.example.com;
root admin;
location / {
index index.html index.htm;
}
}
server {
server_name example.com;
root html;
location / {
index index.html index.htm;
}
}
Also, it is better to use only one root directive at server level, not to repeat it across location blocks (cf pitfalls).

Nginx configuration location

I have the following Nginx configuration file...
server {
listen 80;
server_name 127.0.0.1 localhost;
location = /index.html {
root /etc/nginx/html/app1;
index index.html;
}
location / {
root /etc/nginx/html/app1;
index index.html;
}
location /common/ {
root /etc/nginx/html/common;
}
}
And the folder structure is like so...
html\app1
html\common
When I try to browse...
http://localhost/ > Works
http://localhsot/index.html > Works
http://localhost/common/somefile.txt > Doesn't work
What am I missing?
You should use alias instead of root:
server {
listen 80;
server_name 127.0.0.1 localhost;
location / {
root /etc/nginx/html/app1;
index index.html;
}
location /common {
alias /etc/nginx/html/common;
}
}
If you use root in common the 127.0.0.1/common/somefile.txt will try /etc/nginx/html/common/common/somefile.txt (notice the two common). If you check nginx's logs you can see it.
I am adding my own answer since I finally got it working. Posting it here, so it might help others...
server {
listen 80;
server_name 127.0.0.1 localhost;
location = /index.html {
root /etc/nginx/html/app1;
index index.html;
}
location / {
root /etc/nginx/html/app1;
index index.html;
}
location ^~ /common/ {
root /etc/nginx/html;
}
}
Basically, the way Nginx was trying was /etc/nginx/html/common/common. Removing the common from root worked. Also found that http://localhost:8888/common/ needed to have a trailing /.
Because it firstly match the location /. You can do it like this:
server {
listen 80;
server_name 127.0.0.1 localhost;
location = /index.html {
root /etc/nginx/html/app1;
index index.html;
}
location / {
root /etc/nginx/html/app1;
index index.html;
}
location ^~ /common/ {
root /etc/nginx/html/common;
}
}
EDIT:
Yeah. It seems some complicated. You can do it like this:
First, you need create a new server:
server {
listen 80;
server_name common.com; # A virtual host
root /etc/nginx/html/common;
}
Then, you need modify the config above like this:
server {
listen 80;
server_name 127.0.0.1 localhost;
location = /index.html {
root /etc/nginx/html/app1;
index index.html;
}
location / {
root /etc/nginx/html/app1;
index index.html;
}
location ^~ /common/ {
rewrite ^/common(/.*)$ $1 break; # rewrite the /common/
proxy_set_header Host common.com; # it will requests common.com which the server of 127.0.0.1. then will match the above server.
proxy_pass http://127.0.0.1;
}
}

nginx rule to serve root

I would like to nginx to serve a static file from website root ( : http://localhost:8080/ ) but it serves my proxy pass; it serves "/" rule instead of "= /".
Here is what my nginx config look like :
listen 0.0.0.0:8080;
server_name localhost;
set $static_dir /path/to/static/
location = / {
# got index.html in /path/to/static/html/index.html
root $static_dir/html/;
}
location / {
# ...
proxy_pass http://app_cluster_1/;
}
Did i miss something ?
Use this one:
location = / {
index index.html;
}
location = /index.html {
root /your/root/here;
}

Resources