2

I decided to give a try to nginx, however it seems I am stuck at the really beggining. I have a server running Debian and I am trying to configure nginx with 1 domain + 2 subdomains. When I access the main domain it shows what it's supposed to. When I access the first subdomain it access to the correct folder, but when I access to the second subdomain it shows the main domain instead. Here is what I modified so far:

sites-available (main domain) works ok

server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/domain.com/public_html/;
index index.php index.html index.htm index.nginx-debian.html;
server_name www.domain.com domain.com;
location / {
    try_files $uri $uri/ =404;
}
}

sites-available (first subdomain) works ok

server{
listen 80;
listen [::]:80;
root /var/www/sub1.domain.com;
index index.php index.html index.htm index.nginx-debian.html;
server_name sub1.domain.com;
location / {
    try_files $uri $uri/ =404;
}
}

sites available (second subdomain) redirects to main domain

server{
listen 80;
listen [::]:80;
root /var/www/sub2.domain.com;
index index.php index.html index.htm index.nginx-debian.html;
server_name sub2.domain.com;
location / {
try_files $uri $uri/ =404;
}
}

I created a symlink of this files to sites-enabled and I also added them to /etc/hosts

Server IP            www.domain.com               domain.com
Server IP            sub1.domain.com
Server IP            sub2.domain.com

The domain and both subdomains have an A record pointing to the server IP. I restarted nginx and the server several times, cleared the cookies and cache on all browsers, tried from different PC... I can't see what is wrong in the configuration of the second subdomain.

Any help will be appreciated!!

ED: here are some logs:

ERROR.LOG

2015/11/17 22:41:16 [notice] 10184#0: signal process started
2015/11/17 22:56:46 [notice] 10891#0: signal process started
2015/11/17 23:06:37 [notice] 11332#0: signal process started

It is basically full of those ones.

ACCESS.LOG

94.23.253.89 - - [17/Nov/2015:23:13:24 +0100] "GET / HTTP/1.1" 200 18 "-" "curl/7.38.0"
94.23.253.89 - - [17/Nov/2015:23:14:30 +0100] "GET / HTTP/1.1" 200 18 "-" "lwp-request/6.03 libwww-perl/6.08"
94.23.253.89 - - [17/Nov/2015:23:14:48 +0100] "GET / HTTP/1.1" 200 18 "-" "lwp-request/6.03 libwww-perl/6.08"
94.23.253.89 - - [17/Nov/2015:23:15:35 +0100] "GET / HTTP/1.1" 200 18 "-" "lwp-request/6.03 libwww-perl/6.08"
94.23.253.89 - - [17/Nov/2015:23:15:52 +0100] "GET / HTTP/1.1" 200 18 "-" "lwp-request/6.03 libwww-perl/6.08"
94.23.253.89 - - [17/Nov/2015:23:15:58 +0100] "GET / HTTP/1.1" 200 18 "-" "lwp-request/6.03 libwww-perl/6.08"
94.23.253.89 - - [17/Nov/2015:23:16:05 +0100] "GET / HTTP/1.1" 200 18 "-" "lwp-request/6.03 libwww-perl/6.08"
4
  • Are all three configuration files linked in the sites-enabled folder? – Mat Nov 17 '15 at 21:37
  • Yes, all of them are. – ragtime.sp Nov 17 '15 at 21:38
  • Do you have access logs and error logs set up somewhere in your configuration? Anything helpful there? – Paul Nov 17 '15 at 22:16
  • @Paul I just edited the main post with the logs. Nothing strange that I can see. Also tried curl and get from a terminal and I get the same result. – ragtime.sp Nov 17 '15 at 22:23
1
server {
        set $docroot "/var/www/domain.com/public_html/";
        listen 80 default_server;
        server_name www.domain.com domain.com;
        root $docroot;
        try_files $uri $uri/ /index.php?$args;
        index index.php index.html index.htm;
}

server {
        set $docroot "/var/www/sub1domain.com/public_html/";
        listen 80;
        server_name www.sub1.domain.com sub1.domain.com;
        root $docroot;
        try_files $uri $uri/ /index.php?$args;
        index index.php index.html index.htm;
}

server {
        set $docroot "/var/www/sub2.domain.com/public_html/";
        listen 80;
        server_name www.sub2.domain.com sub2.domain.com;
        root $docroot;
        try_files $uri $uri/ /index.php?$args;
        index index.php index.html index.htm;
}

Make sure that in the sites enabled they are sim-links to the sites-available directory. You can put the above into one file or you can put each into a septate file inside the sites-available directory.

To test your config with nginx use "nginx -t" this will show you if your config is correct without affecting the server.

6
  • Thanks to your answer I realized what was wrong. I actually forgot to include my second subdomain to nginx.conf! Rookie mistake :/ – ragtime.sp Nov 17 '15 at 22:43
  • That is what @Mat asked you about in the very first comment. – Paul Nov 17 '15 at 22:57
  • @Paul no, it's a different thing. They were all linked to sites-enabled, but the second subdomain wasn't in nginx.conf – ragtime.sp Nov 18 '15 at 17:55
  • 1
    That is the purpose of linking to sites-enabled. nginx.conf typically has the line include .../nginx/sites-enabled/*; for this purpose. – Paul Nov 18 '15 at 18:03
  • Not in my version at least. There is not reference at all to sites enabled, only to sites available. – ragtime.sp Nov 18 '15 at 23:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.