![]() |
VOOZH | about |
When using the Nginx web server, server blocks (similar to the virtual hosts in Apache) can be used to encapsulate configuration details and host more than one domain off of a single server.
In this guide, weโll discuss how to configure server blocks in Nginx on an Ubuntu 14.04 server.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
This tutorial series helps sysadmins set up a new web server using the LEMP stack, focusing on Nginx setup with virtual blocks. This will let you serve multiple websites from one Droplet.
Youโll start by setting up your Ubuntu 14.04 server and end with multiple virtual blocks set up for your websites. An Nginx configuration guide is included at the end for reference.
Former Senior Technical Writer at DigitalOcean, specializing in DevOps topics across multiple Linux distributions, including Ubuntu 18.04, 20.04, 22.04, as well as Debian 10 and 11.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
This is great. Thank you!
Great tutorial, very thorough - thanks!
Good article - just one nit-picky thing:
sudo service nginx restart is perfectly valid, but if you have a busy site, you might drop a couple of connections.
Safer, cleaner (and easier!) is to follow this tip Iโve been using since I found it on stackoverflow, which comes in very useful if youโre making lots of changes or experimenting and tweaking:
alias n='/etc/init.d/nginx configtest && sudo /etc/init.d/nginx reload'
from then on, just hit n and enter. Instance new config, tested and reloaded if itโs valid.
If you like the alias, just
sudo nano ~/.bash_aliases
add that alias line following the correct format and then
source ~/.bash_aliases
so you wonโt need to reload the shell.
(I wonโt delve into the whole /var/www vs /usr/share/nginx/html debate because Iโm still not decided which is correct! )
Like a charm. Thanks!
Awesome, thanks!!
Hey, this worked perfectly with my first two test domains, but for some reason once I moved my actual domain in itโs pointing to one of the test domains rather than to its own folder even though I followed the exact same steps. Any idea why?
Thanks!
@jcalifa: Could you post your Nginx configurations on a pastebin? That would make it much easier to help you.
And how i redirect www.domain.com to domain.com in this config file of a domains?
@gugaalves: Add a new virtualhost with the following config:
server {
listen 80;
server_name www.domain.com;
return 301 http://domain.com$request_uri;
}
and restart nginx.
I found that the line
server_names_hash_bucket_size: 64;
Caused and โunknown directiveโ error when starting nginx and has to remove the colon for it to work:
server_names_hash_bucket_size 64;
Also, this line did not exist to be un-commented as descrived above, so you add it inside the http area:
http {
server_names_hash_bucket_size 64;
...
}
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.