how to configure nginx for different paths - nginx

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

Related

nginx with multiple locations directives with subdomains

I am trying to implement something like that in the nginx conf:
subdomain
sub.domain.com -> Serve html
sub.domain.com/api -> proxy to port 3001
sub.domain.com/viewer -> serve another html
subdomain2
sub2.domain.com -> proxy to port 3000
The only route that doesn't work is the viewer, I get the html from the "location /". All other configurations work well.
I tried to move the viewer to the bottom then to the top and to the middle no matter what it doesn't work.
I use CentOS7. This is the configurations currently in the server:
events {
}
http {
server {
listen 80;
listen [::]:80;
server_name www.sub.domain.com subdomain.com;
location /viewer {
root /opt/viewer/;
try_files $uri /index.html;
index index.html;
}
location / {
root /opt/client-bo/;
try_files $uri /index.html;
index index.html;
}
location /api {
proxy_pass "http://localhost:3001";
}
}
server {
listen 80;
server_name www.sub2.domain.com sub2.domain.com;
listen [::]:80;
location / {
proxy_pass "http://localhost:3000";
}
}
}
Thanks!
If your viewer app located in the /opt/viewer directory and you want it to be available under the /viewer URI prefix, you should use root /opt; for the location /viewer { ... }. Check the difference between root and alias directives.
Next, the very last argument of the try_files directive is treated as the new URI to re-evaluate, so you /index.html being treated as the new URI going to be served with the location / { ... }. You should change that directive to
try_files $uri /viewer/index.html;

nginx: empty subdomain can not work

I read this article http://bneijt.nl/blog/post/name-based-virtual-hosting-with-nginx/
and excerpts is as follow:
server {
server_name ~^((?<subdomain>.*)\.)?(?<domain>[^.]+)\.(?<tld>[^.]+)$;
if ($subdomain = "") {
set $subdomain "_";
}
location / {
index index.html;
root /srv/http/vhost/${domain}.${tld}/${subdomain};
}
}
I imitate it and write my configuration like this:
server {
server_name ~^((?<subdomain>.*)\.)aa\.com$;
if ($subdomain = "") {
set $subdomain "www";
}
location / {
root /var/www/${subdomain}.aa.com/public;
index index.html index.htm;
}
}
Every subdomain corresponds to it's folder,like this:
domain name folder
111.aa.com /var/www/111.aa.com
222.aa.com /var/www/222.aa.com
Question:
If input www.aa.com,it works,but input aa.com,it cann't work,domain name resolution is ok,what's the problem?
Try this:
server {
server_name ~^((?<subdomain>.*)\.)aa\.com$ aa.com;
if ($host ~ aa.com) {
set $subdomain "www";
}
location / {
root /var/www/${subdomain}.aa.com/public;
index index.html index.htm;
}
}
but I prefer this:
# redirect user to www.aa.com if user went to aa.com
server {
server_name aa.com;
return 301 $scheme://www.aa.com$request_uri;
}
# handle subdomain part
server {
server_name ~^((?<subdomain>.*)\.)aa\.com$;
location / {
root /var/www/${subdomain}.aa.com/public;
index index.html index.htm;
}
}

Nginx configuration location and index

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 = {}.

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

Make only 1 root file accessible, and serve it for all requests

I actually have this working, but I'd like to know if I am doing it the most efficient way, or if there are any improvements I can make to my conf file. Here is what I am attempting to do:
If any file is requested from the root, we should always serve "index.html". No other file should be accessible, and requesting anything else should be treated as if you requested "index.html". Currently I'm using rewrite, but a redirect would be okay too, and possibly preferable.
Any file under "/css" or "/js" can be requested, and requesting files from those directories that don't exist should return a 404.
Here's my current working conf file:
server {
listen 80;
server_name www.example.com;
client_max_body_size 50M;
root /var/www/mysite;
location = /index.html {
}
# map everything in base dir to one file
location ~ ^/[^/]*$ {
rewrite ^/[^/]*$ /index.html;
}
location ~ ^/css/ {
}
location ~ ^/js/ {
}
}
UPDATE
My final conf file, which is both faster under a load test and simpler than the original, is here:
server {
listen 80;
server_name example.com;
root /var/www/register;
location = /index.html {
}
# Default location, request will fallback here if none other
# location block matches
location / {
rewrite ^.*$ /index.html redirect; # 'root' location '/'
}
location /css/ {
}
location /js/ {
}
}
I'm not sure if I got this right or not, but check this answer, you always want to server index.html so it should be the default location location /
server {
server_name example.com www.example.com;
client_max_body_size 50M;
root /var/www/mysite;
index index.html;
location / {
try_files index.html =404;
}
location /(css|js) {
try_files $uri =404;
}
}

Resources