How to Create an Nginx Virtual Host (AKA Server Blocks)

How to Create an Nginx Virtual Host (AKA Server Blocks)

What is a virtual host?

A virtual host is an Apache term, however, is normally used by Nginx users equally well. The proper term for Nginx is server engine block. Both of these words have the lapp mean which is basically the feature of being able to host multiple websites on a single server. This is extremely useful given that you own multiple sites and do n’t want to go through the drawn-out ( and expensive ) process of setting up a new web server for each site. In this guide, we ‘re going to through the three steps of setting up an Nginx virtual host on a Ubuntu 16.04 machine .

Step 1 – Creating a new site

The first step in this march is to actually create and populate a directory for your new web site. In Nginx, all virtual host site files are stored within the /var/www/ directory. consequently, you can create a folder within that directory, called testsite.com and add a sample file called index.php. To create the testsite.com directory use the following dominate :

mkdir /var/www/testsite.com

adjacent, you can create the index.html file and add some text to it using the play along command :

echo "Hello World" > /var/www/testsite.com/index.php

last, ensure that you ‘ve setup the proper file permissions so that Nginx can access it. To do this, use the adopt snip :

chmod -R 755 /var/www/testsite.com

Step 2 – Configuring your Nginx virtual hosts

nowadays that you ‘ve created a site booklet and added a sample file under /testsite.com, you ‘ll need to configure your Nginx virtual host or server blocks for testsite.com. virtual host config files are typically located in the /etc/nginx/sites-available directory. You may besides notice that your server has a /etc/nginx/sites-enabled folder, which is where file shortcuts ( symbolic links ) are placed. You can use the sites-enabled booklet to well enable or disable a virtual host by creating or removing symbolic links. now that you ‘ve located the sites-available folder, you ‘ll want to cd into that booklet and create a virtual host config file for testsite.com. A config file can either be created from scratch or you can copy the nonpayment configuration file and make any needed changes. In this font, were going to copy the nonpayment file that ‘s presently within the sites-available directory. To do so, use the follow snip :

cp /etc/nginx/sites-available/default /etc/nginx/sites-available/testsite.com.conf

You will then need to modify the contents to testsite.com.conf to match the paths and directories of your newly created site. An exemplar of a barebones Nginx server parry that ‘s configured to rewrite requests to HTTPS and serve a Let ‘s Encrypt SSL certificate will look alike to the come :

server {
    listen 80;

    server_name testsite.com;

    rewrite ^ https://testsite.com$request_uri? permanent;
}

server {
    listen 443 ssl http2;

    ssl_certificate /etc/letsencrypt/live/testsite.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/testsite.com/privkey.pem;
    ssl_stapling on;

    server_name testsite.com;

    root /var/www/testsite.com;

    location / {
        try_files $uri /index.php?$args;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
    }
}

once you ‘re done editing your virtual host file, be sure to save the file. Next, you ‘ll need to create a symbolic liaison in sites-enabled to the newly created nginx virtual host files within the sites-available folder :

ln -s /etc/nginx/sites-available/testsite.com.conf /etc/nginx/sites-enabled/testsite.com.conf

last, restart Nginx with the follow command :

service nginx restart

Step 3 – Testing your setup

Your virtual server file should now be properly configured for your new setup. Of class, there are many modifications that can be made such as adding leverage browser caching confirm or CORS subscribe, although the exercise above should be enough to get you started. once you ‘ve properly defined your DNS settings at the vane host degree ( or whichever DNS service provider you ‘re using ), you should now be able to access your web site immediately by entering the domain into a vane browser. alternatively, if you have n’t so far configured your DNS settings or just do not want the locate to go alive yet, you can modify your calculator ‘s hosts file. To do this, use the play along command in your calculator ‘s CLI

vi /etc/hosts

then, add the IP address of your actual server followed by the knowledge domain name you are configuring, for exercise :

# Virtual Hosts Local Test
1.2.3.4 testsite.com

immediately you should be able to save the file and access it from within a web browser .

Summary

Nginx virtual hosts or server blocks are a great means to add extra websites to the same origin server. The issue of configuration possibilities for a given web site are about endless when you start modifying the virtual host configuration file to suit your the specific needs of your locate .

Leave a Reply

Your email address will not be published.