Subdomains not working on nginx
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.
Related videos on Youtube
ragtime.sp
Software Engineering student at the University of Westminster (London) and passionate Web Developer!
Updated on September 18, 2022Comments
-
ragtime.sp over 1 year
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"
-
Mat over 8 yearsAre all three configuration files linked in the sites-enabled folder?
-
ragtime.sp over 8 yearsYes, all of them are.
-
Paul over 8 yearsDo you have access logs and error logs set up somewhere in your configuration? Anything helpful there?
-
-
ragtime.sp over 8 yearsThanks to your answer I realized what was wrong. I actually forgot to include my second subdomain to nginx.conf! Rookie mistake :/
-
Paul over 8 yearsThat is what @Mat asked you about in the very first comment.
-
ragtime.sp over 8 years@Paul no, it's a different thing. They were all linked to sites-enabled, but the second subdomain wasn't in nginx.conf
-
Paul over 8 yearsThat is the purpose of linking to sites-enabled.
nginx.conf
typically has the lineinclude .../nginx/sites-enabled/*;
for this purpose. -
ragtime.sp over 8 yearsNot in my version at least. There is not reference at all to sites enabled, only to sites available.
-
Paul over 8 yearsMost people compile nginx to have a
sites-enabled
directory where you create a symlink to asites-available
file that contains desired configurations.