Nginx dynamic root from subdomain - nginx

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

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;

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 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 subdomain configuration

I have nginx acting as a reverse proxy to apache. I now need to add a new subdomain
that will serve files from another directory, but at the same time I want all location and proxy_pass directives that I have for the default host to apply to the subdomain also.
I know that if I copy the rules from the default host to the new subdomain it will work, but is there a way for the subdomain to inherit the rules?
Below is a sample configuration
server {
listen 80;
server_name www.somesite.com;
access_log logs/access.log;
error_log logs/error.log error;
location /mvc {
proxy_pass http://localhost:8080/mvc;
}
location /assets {
alias /var/www/html/assets;
expires max;
}
... a lot more locations
}
server {
listen 80;
server_name subdomain.somesite.com;
location / {
root /var/www/some_dir;
index index.html index.htm;
}
}
Thanks
You could move the common parts to another configuration file and include from both server contexts. This should work:
server {
listen 80;
server_name server1.example;
...
include /etc/nginx/include.d/your-common-stuff.conf;
}
server {
listen 80;
server_name another-one.example;
...
include /etc/nginx/include.d/your-common-stuff.conf;
}
Edit: Here's an example that's actually copied from my running server. I configure my basic server settings in /etc/nginx/sites-enabled (normal stuff for nginx on Ubuntu/Debian). For example, my main server bunkus.org's configuration file is /etc/nginx/sites-enabled and it looks like this:
server {
listen 80 default_server;
listen [2a01:4f8:120:3105::101:1]:80 default_server;
include /etc/nginx/include.d/all-common;
include /etc/nginx/include.d/bunkus.org-common;
include /etc/nginx/include.d/bunkus.org-80;
}
server {
listen 443 default_server;
listen [2a01:4f8:120:3105::101:1]:443 default_server;
include /etc/nginx/include.d/all-common;
include /etc/nginx/include.d/ssl-common;
include /etc/nginx/include.d/bunkus.org-common;
include /etc/nginx/include.d/bunkus.org-443;
}
As an example here's the /etc/nginx/include.d/all-common file that's included from both server contexts:
index index.html index.htm index.php .dirindex.php;
try_files $uri $uri/ =404;
location ~ /\.ht {
deny all;
}
location = /favicon.ico {
log_not_found off;
access_log off;
}
location ~ /(README|ChangeLog)$ {
types { }
default_type text/plain;
}

Resources