I'm new to Nginx and I'm trying to get subdomains working.
What I would like to do is take my domain (let's call it example.com
) and add:
sub1.example.com
,sub2.example.com
, and also havewww.example.com
available.I know how to do this with Apache, but Nginx is being a real head scratcher.
I'm running Debian 6.
My current /etc/nginx/sites-enabled/example.com:
server {
server_name www.example.com example.com;
access_log /srv/www/www.example.com/logs/access.log;
error_log /srv/www/www.example.com/logs/error.log;
root /srv/www/www.example.com/public_html;
location / {
index index.html index.htm;
}
location ~ \.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /srv/www/www.example.com/public_html$fastcgi_script_name;
}
}
It is working to serve example.com and www.example.com.
I have tried to add a second server block in the same file like:
server {
server_name www.example.com example.com;
access_log /srv/www/www.example.com/logs/access.log;
error_log /srv/www/www.example.com/logs/error.log;
root /srv/www/www.example.com/public_html;
server {
server_name sub1.example.com;
access_log /srv/www/example.com/logs/sub1-access.log;
error_log /srv/www/example.com/logs/sub1-error.log;
root /srv/www/example.com/sub1;
}
location / {
index index.html index.htm;
}
location ~ \.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /srv/www/www.example.com/public_html$fastcgi_script_name;
}
}
No luck. Any ideas? I'd super appreciate any feedback.
Nginx supports hosting multiple domains using server blocks. All your websites can be stored on a single server if you select a VPS as your hosting platform, allowing you the freedom to take charge of the situation yourself.
Yes, It's possible. For Example. You would either Contact your Host to set this, or if you have the ability to point DNS your self then do so. Just make sure that the site you want to serve on that subdomain exists on the server that it's pointing to.
The mistake is putting a server block inside a server block, you should close the main server block then open a new one for the sub domains
server {
server_name example.com;
# the rest of the config
}
server {
server_name sub1.example.com;
# sub1 config
}
server {
server_name sub2.example.com;
# sub2 config
}
You just need to add the following line in place of your server_name
server_name xyz.com *.xyz.com;
And restart Nginx. That's it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With