# Nginx issue with subdomain



## fred974 (Jun 24, 2014)

Hello everyone,

I currently have an NGINX server block as follow:


```
server {
    listen      80 default_server;
    server_name test.example.org www.example.org;
	access_log logs/domain1.access.log main;
	root /var/www/domain_1/live;
    ...
}
```
... So when *I* type http://www.example.org, I end up in my  root directory /var/www/domain_1/live which is good.
My question is how can I get test.example.org to go to another root directory.
I would like the root to be:

```
root /var/www/domain_1/test;
```

Thank you in advance

Fred


----------



## SirDice (Jun 24, 2014)

You need to create another server block where you define the second site.


----------



## fred974 (Jun 24, 2014)

Can I create server block only for subdomain like :

```
server {
    listen      80;
    server_name test.example.org;
   root /var/www/domain_1/test;
    ...
}
server {
    listen      80;
    server_name test2.example.org;
   root /var/www/domain_1/test2;
    ...
}
```


----------



## drhowarddrfine (Jun 24, 2014)

Yes if you only have one IP address for that server. Otherwise, you need to include the IP address on the listen line: 127.0.0.1:80;


----------



## fred974 (Jun 25, 2014)

drhowarddrfine said:
			
		

> Yes if you only have one IP address for that server. Otherwise, you need to include the IP address on the listen line: 127.0.0.1:80;


Yes only 1 IP and 5 domains. Will that be a problem or can NGINX handle that?


----------



## SirDice (Jun 25, 2014)

fred974 said:
			
		

> Will that be a problem or can NGINX handle that?


That won't be a problem.


----------



## kpa (Jun 25, 2014)

fred974 said:
			
		

> drhowarddrfine said:
> 
> 
> 
> ...



That's the exact usage case the virtual domains were invented for. You have one public IP address and multiple FQDNs. The web server decides which virtual domain to serve or forward to by looking at the HTTP headers and matches the requested host name to the server name (or in some cases a more complicated rule like a regular expression) in the virtual host definitions.


----------



## Oko (Jun 25, 2014)

fred974 said:
			
		

> Can I create server block only for subdomain like :
> 
> ```
> server {
> ...



How would you reverse proxy http://www.test.org/site1 and http://www.test.org/site2 to two different web servers? I know how to do it with site1.test.org and site2.test.org but I want to use forward slash and suffixes and keep the base the same.


----------



## junovitch@ (Jun 26, 2014)

Search for "proxy_pass" in the http://nginx.org/en/docs/beginners_guide.html to reverse proxy to two distinct web servers.  Alternately, if they are on the same server you can define the root directory for each location.  The Nginx guide should get you started.


----------



## SirDice (Jun 27, 2014)

If you're only going to proxy why not use something like net/haproxy? It's specifically meant to be used in this sort of situation. You can have it switch servers based on the hostname and/or (parts of) the URL.


----------



## Oko (Jun 28, 2014)

Oko said:
			
		

> fred974 said:
> 
> 
> 
> ...



I am going to answer my own question with the code  :beer 


```
# $OpenBSD: nginx.conf,v 1.16 2014/01/28 14:48:53 stephan Exp $

#user  www;
worker_processes  4;

#syslog     local5          nginx;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
#error_log  syslog:notice|logs/error.log;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;
    index         index.html index.htm;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;
    #access_log  syslog:notice|logs/access.log main;

    #tcp_nopush     on;

    keepalive_timeout  65;

    server_tokens off;

    # Virtual hosts
    server {
        listen       24.154.107.108:80;
        server_name  http://www.test.org;
        location /www1 {
            proxy_pass http://www1.int.test.org;
            proxy_next_upstream error timeout invalid_header http_500 http_502 h
ttp_503 http_504;
            proxy_redirect off;
            proxy_buffering off;
            proxy_set_header        Host            $host;
            proxy_set_header        X-Real-IP       $remote_addr;
            proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
            }
        location /www2 {
            proxy_pass http://www2.int.test.org;
            proxy_next_upstream error timeout invalid_header http_500 http_502 h
ttp_503 http_504;
            proxy_redirect off;
            proxy_buffering off;
            proxy_set_header        Host            $host;
            proxy_set_header        X-Real-IP       $remote_addr;
            proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
            }
    }

}
```


----------

