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

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).

Related

Nginx dynamic root from subdomain

My website uses many subdomains. What I need is to root requests to each folder depending of subdomain:
src.mydomain.com to /public
api.mydomain.com to /public
Anyother subdomain xxx.mydomain.com to /dist
I tried this settings without success:
server {
listen 8080;
listen [::]:8080;
server_name ~^(?<subdomain>.+)\.mydomain\.com$;
set $folder "dist";
if ($subdomain = "src"){
set $folder "public";
}
if ($subdomain = "api"){
set $folder "public";
}
root "/home/site/wwwroot/$folder";
index index.php index.html;
location / {
index index.php index.html;
}
}
Try this:
map $http_host $webroot {
src.mydomain.com /home/site/wwwroot/public;
api.mydomain.com /home/site/wwwroot/public;
default /home/site/wwwroot/dist;
}
server {
server_name *.mydomain.com;
root $webroot;
...
}

How should I use Nginx for following?

I am trying to run my react app using Nginx.
I created build of my app (name: react-app) and placed it over here /var/www/react-app.
Then I created a conf file /etc/nginx/conf.d/react-app.conf
server {
listen 8081;
server_name localhost;
location / {
root /var/www/react-app;
index index.html index.html;
}
}
There is include /etc/nginx/conf.d/*.conf; inside /etc/nginx/nginx.conf.
Then I ran nginx and opened http://localhost:8081/ in the browser, but result is blank.
How can fix this?
You should not use index and add a try_files:
server {
listen 8081;
server_name localhost;
location / {
root /var/www/react-app;
try_files $uri $uri/ /index.html;
}
}

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

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

Resources