Ubuntu 14.04 Step 5 How To Set Up Nginx Server Blocks (Virtual Hosts) version: 1.0a Ubuntu 14.04 Step 5 How To Set Up Nginx Server Blocks (Virtual Hosts) Virtual hosting is a method for hosting multiple domain names (with separate handling of each name) on a single server (or pool of servers). This allows one server to share its resources, such as memory and processor cycles, without requiring all services provided to use the same host name. The term virtual hosting is usually used in reference to web servers but the principles does carry over to other internet services. One widely used application is shared web hosting. Shared web hosting prices are lower than a dedicated web server because many customers can be hosted on a single server. It is also very common for a single entity to want to use multiple names on the same machine so that the names can reflect services offered rather than where those services happen to be hosted. There are two main types of virtual hosting, name-based and IP-based. Name-based virtual hosting uses the host name presented by the client. This saves IP addresses and the associated administrative overhead but the protocol being served must supply the host name at an appropriate point. In particular, there are significant difficulties using name-based virtual hosting with SSL/TLS. IP-based virtual hosting uses a separate IP address for each host name, and it can be performed with any protocol but requires a dedicated IP address per domain name served. Port-based virtual hosting is also possible in principle but is rarely used in practice because it is unfriendly to users. Name-based and IP-based virtual hosting can be combined: a server may have multiple IP addresses and serve multiple names on some or all of those IP addresses. This technique can be useful when using SSL/TLS with wildcard certificates. For example, if a server operator had two certificates, one for *.example.com and one for *.example.net, he could serve foo.example.com and bar.example.com off the same IP address but would need a separate IP address for baz.example.net.i By default, Nginx on Ubuntu 14.04 has one server block enabled by default. It is configured to serve documents out of a directory at: /usr/share/nginx/html We won't use the default since it is easier to work with things in the /var/www directory. Ubuntu's Nginx package does not use /var/www as its document root by default due to a Debian policy about packages utilizing /var/www. Since we are users and not package maintainers, we can tell Nginx that this is where we want our document roots to be. Specifically, we want a directory for each of our sites within the /var/www directory and we will have a directory under these called html to hold our actual files. For the case of this tutorial we will be using site1.com, site2.com, site3.com, site4.com, site5.com and site6.com as our example domain names. Please fill in your own domain names. Page: 1 Ubuntu 14.04 Step 5 How To Set Up Nginx Server Blocks (Virtual Hosts) version: 1.0a 1. First, we need to create the necessary directories. We can do this with the following command. The -p flag tells mkdir to create any necessary parent directories along the way: sudo mkdir -p /var/www/site1.com/html sudo mkdir -p /var/www/site2.com/html sudo mkdir -p /var/www/site3.com/html sudo mkdir -p /var/www/site4.com/html sudo mkdir -p /var/www/site5.com/html sudo mkdir -p /var/www/site6.com/html PLEASE note: where it says site1.com, site2.com etc you replace these with the domain names you are going to be using. You can use 1 site or as many as you like... Keep in mind that the more sites, especially if they are busy the more server resources you will be using. 2. Now that you have your directories created, we need to transfer ownership to our regular user. We can use the $USER environmental variable to substitute the user account that we are currently signed in on. This will allow us to create files in this directory without allowing our visitors to create content.ii sudo chown -R $USER:$USER /var/www/site1.com/html sudo chown -R $USER:$USER /var/www/site5.com/html sudo chown -R $USER:$USER /var/www/site4.com/html sudo chown -R $USER:$USER /var/www/site3.com/html sudo chown -R $USER:$USER /var/www/site6.com/html sudo chown -R $USER:$USER /var/www/site2.com/html 3. The permissions of our web roots should be correct already if you have not modified your umask value, but we can make sure by typingiii: sudo chmod -R 755 /var/www Create Server Block Files for Each Domain Now that we have the content we wish to serve, we need to actually create the server blocks that will tell Nginx how to do this. By default, Nginx contains one server block called default which we can use as a template for our own configurations. We will begin by designing our first domain's server block, which we will then copy over for our second domain and make the necessary modifications. Create the First Server Block File 1. As mentioned above, we will create our first server block con-fig file by copying over the default file: Page: 2 Ubuntu 14.04 Step 5 How To Set Up Nginx Server Blocks (Virtual Hosts) version: 1.0a sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/site1.com sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/site2.com sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/site3.com sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/site4.com sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/site5.com sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/site6.com 2. Now, open the new file you created in your text editor with root privileges (you have to do this one site at a time): sudo nano /etc/nginx/sites-available/site1.com sudo nano /etc/nginx/sites-available/site2.com sudo nano /etc/nginx/sites-available/site3.com sudo nano /etc/nginx/sites-available/site4.com sudo nano /etc/nginx/sites-available/site5.com sudo nano /etc/nginx/sites-available/site6.com Ignoring the commented lines, the file will look similar to this: server { listen 80 default_server; listen [::]:80 default_server ipv6only=on; root /usr/share/nginx/html; index index.html index.php default.html index.htm; server_name localhost; location / { try_files $uri $uri/ =404; } } First, we need to look at the listen directives. Only one of our server blocks can have the default_server specification. This specifies which block should server a request if the server_name requested does not match any of the available server blocks. We are eventually going to disable the default server block configuration, so we can place the default_server option in either this server block or in the one for our other site. I'm going to leave the default_server option enabled in this server block, but you can choose whichever is best for your situation. 3. The next thing we're going to have to adjust is the document root, specified by the root directive. Point it to the site's document root that you created: Page: 3 Ubuntu 14.04 Step 5 How To Set Up Nginx Server Blocks (Virtual Hosts) version: 1.0a from: root /usr/share/nginx/html; to: root /var/www/site1.com/html; Note: Each Nginx statement must end with a semi-colon (;), so check each of your lines if you are running into problems. 4. Next, we want to modify the server_name to match requests for our first domain. We can additionally add any aliases that we want to match. We will add a www.example.com alias to demonstrate: from: server_name localhost; to: server_name site1.com www.site1.com; When you are finished, your file will look something like this: server { listen 80 default_server; listen [::]:80 default_server ipv6only=on; root /var/www/site1.com/html; index index.html index.php default.html index.htm; server_name example.com www.example.com; location / { try_files $uri $uri/ =404; } } 5. That is all we need for a basic configuration. Save and close the file to exit. (control x, y, enter) Create the Second Server Block File (If you only want one Server Block then you dont need to do this part) 1. Now that we have our initial server block configuration, we can use that as a basis for our second file. Copy it over to create a new file: sudo cp /etc/nginx/sites-available/site1.com /etc/nginx/sites-available/site2.com 2. Open the new file with root privileges in your editor: sudo nano /etc/nginx/sites-available/site2.com Page: 4 Ubuntu 14.04 Step 5 How To Set Up Nginx Server Blocks (Virtual Hosts) version: 1.0a 3. In this new file, we're going to have to look at the listen directives again. If you left the default_server option enabled in the last file, you'll have to remove it in this file. Furthermore, you'll have to get rid of the ipv6only=on option, as it can only be specified once per address/port combination: from: listen 80 default_server; to: listen 80; from: listen [::]:80 default_server ipv6only=on; to: listen [::]:80; 4. Adjust the document root directive to point to your second domain's document root: from: root /usr/share/nginx/html; to: root /var/www/site2.com/html; 5. Adjust the server_name to match your second domain and any aliases: from: server_name site1.com; to: server_name site2.com www.site2.com; Your file should look something like this with these changes: server { listen 80; listen [::]:80; root /var/www/site2.com/html; index index.html index.php index.htm; server_name site2.com www.site2.com; location / { try_files $uri $uri/ =404; } } 6. When you are finished, save and close the file. (control x, y, enter) Step Four — Enable your Server Blocks and Restart Nginx You now have your server blocks created, we need to enable them. We can do this by creating symbolic links from these files to the sites-enabled directory, which Nginx Page: 5 Ubuntu 14.04 Step 5 How To Set Up Nginx Server Blocks (Virtual Hosts) version: 1.0a reads from during startup. 1. We can create these links by typing: sudo ln -s /etc/nginx/sites-available/site1.com /etc/nginx/sites-enabled/ sudo ln -s /etc/nginx/sites-available/site2.com /etc/nginx/sites-enabled/ sudo ln -s /etc/nginx/sites-available/site3.com /etc/nginx/sites-enabled/ sudo ln -s /etc/nginx/sites-available/site4.com /etc/nginx/sites-enabled/ sudo ln -s /etc/nginx/sites-available/site5.com /etc/nginx/sites-enabled/ sudo ln -s /etc/nginx/sites-available/site6.com /etc/nginx/sites-enabled/ These files are now in the enabled directory. However, the default server block file we used as a template is also enabled currently and will conflict with our file that has the default_server parameter set. 2. We can disable the default server block file by simply removing the symbolic link. It will still be available for reference in the sites-available directory, but it won't be read by Nginx on startup: sudo rm /etc/nginx/sites-enabled/default 3. We also need to adjust one setting really quickly in the default Nginx configuration file. Open it up by typing: sudo nano /etc/nginx/nginx.conf 4. We just need to uncomment one line. Find and remove the comment from this (control w, will give you Namo's search feature: from: ;server_names_hash_bucket_size 64; to server_names_hash_bucket_size 64; 5. Save your nginx.conf file (control x, y, enter) 6. Now, we are ready to restart Nginx to enable your changes. You can do that by typing: sudo service nginx restart Nginx should now be serving all of your domain names. Sub Domain's The Domain Name System (DNS) has a tree structure or hierarchy, with each non-RR (resource record) node on the tree being a domain name. A subdomain is a domain that is part of a larger domain; the only domain that is not also a subdomain is the root domain. For example, west.example.com and Page: 6 Ubuntu 14.04 Step 5 How To Set Up Nginx Server Blocks (Virtual Hosts) version: 1.0a east.example.com are subdomains of the example.com domain, which in turn is a subdomain of the com top-level domain (TLD). A "subdomain" expresses relative dependence, not absolute dependence: for example, wikipedia.org comprises a subdomain of the org domain, and en.wikipedia.org comprises a subdomain of the domain wikipedia.org. In theory this subdivision can go down to 127 levels deep, and each DNS label can contain up to 63 characters, as long as the whole domain name does not exceed a total length of 255 characters. But in practice most domain registries limit at 253 characters. Subdomains in this context are defined by editing the DNS zone file pertaining to the parent domain. However, there is a lively debate over the use of the term “subdomain” when referring to names which map to the Address record (A; host) and various other types of zone records which may map to any public IP address destination and any type of server. Certain groups insist that it is inappropriate to use the term “subdomain” to refer to any mapping other than that provided by zone NS (name server) records and any server-destination other than that of a domain name server. Notwithstanding the terminology debate, many prominent public DNS providers use the term “subdomain” to refer to names which map to A (host) records which may map to any type of host or destination-server.iv For a sub domain, lets say test.site6.com. We would do the followingv: 1. Copy our default into a new area called test sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/test.site6.com 2. Open the new file with root privileges in your editor: sudo nano /etc/nginx/sites-available/test.site6.com Your file should look like this, just change the test.site6.com to the your choice of a sub domain and your domain name. server { listen 80; listen [::]:80; root /var/www/site2.com/html; index index.html index.php index.htm; server_name test.site6.com; location / { try_files $uri $uri/ =404; } } 3. Now create the link so it goes live. Page: 7 Ubuntu 14.04 Step 5 How To Set Up Nginx Server Blocks (Virtual Hosts) version: 1.0a sudo ln -s /etc/nginx/sites-available/test.site6.com /etc/nginx/sites-enabled/ 4. Now, we are ready to restart Nginx to enable your changes. You can do that by typing: sudo service nginx restart Composed for Kalasiddhi Grid by Drang Po http://www.kalasiddhigrid.com/ Page: 8 i ii iii iv v From: https://en.wikipedia.org/wiki/Virtual_hosting For more information see: https://help.ubuntu.com/community/FilePermissions For more information see: https://help.ubuntu.com/community/FilePermissions Coped from: https://en.wikipedia.org/wiki/Subdomain See https://www.digitalocean.com/community/questions/how-to-setup-subdomain-on-nginx-server for more information.