If you are aware of virtual hosts in apache server blocks has similarity to it, if you wish to host more than on domain from same server you will need to these configurations.

If you have single domain then your default root directory with nginx will be /var/www/html Alright so if you wish to add another domain you will need to create another directory rather playing around with default one for example /var/www/yourdomain.com

Then please type following command in the terminal it will create directory or any required directories.

sudo mkdir -p /var/www/yourdomain.com/html

Now you need to assign rights using $user variable.

sudo chown -R $USER:$USER /var/www/yourdomain.com/html

As long as you haven’t modified umask value new directories permissions should be correct that you just created by running above command you can also make sure by running this command. it should set required permissions.

sudo chmod -R 755 /var/www/yourdomain.com

Now its time to create index page by running following command.

sudo nano /var/www/yourdomain.com/html/index.html

If on windows enter CTRL + O and hit enter in order to save the file and then do CTRL + X in order to close it

<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
</head>

<body>
The content of the document......
</body>

</html>

Now you need to create server block for the new domain that you just added in order to do that you will need to navigate here first cd to root / then do this command.

cd /etc/nginx/sites-available/

After that you will have to run this command in order to edit.

sudo nano yourdomain.com 

in order to update root configurations and server name you have to paste the code below.

server {
        listen 80;
        listen [::]:80;

        root /var/www/yourdomain.com/html;
        index index.html index.htm index.nginx-debian.html;

        server_name yourdomain.com www.yourdomain.com;

        location / {
                try_files $uri $uri/ =404;
        }
}

Now you need to enable the file by navigating here /etc/nginx/sites-enabled/

currently you are in this directory /etc/nginx/sites-available/ so you will need to type following command. it will enable two blocks

  • yourdomain.com: Will respond to requests for example.com and www.yourdomain.com.
  • default: Will respond to any requests on port 80 that do not match the other two blocks.
 sudo ln -s ../yourdomain.com /sites-enabled/ 

Hash Bucket memory Prevention.

what’s hash (nginx)

To quickly process static sets of data such as server names, map directive’s values, MIME types, names of request header strings, nginx uses hash tables. During the start and each re-configuration nginx selects the minimum possible sizes of hash tables such that the bucket size that stores keys with identical hash values does not exceed the configured parameter (hash bucket size). The size of a table is expressed in buckets. The adjustment is continued until the table size exceeds the hash max size parameter. Most hashes have the corresponding directives that allow changing these parameters, for example, for the server names hash they are server_names_hash_max_size and server_names_hash_bucket_size.

The hash bucket size parameter is aligned to the size that is a multiple of the processor’s cache line size. This speeds up key search in a hash on modern processors by reducing the number of memory accesses. If hash bucket size is equal to one processor’s cache line size then the number of memory accesses during the key search will be two in the worst case — first to compute the bucket address, and second during the key search inside the bucket. Therefore, if nginx emits the message requesting to increase either hash max size or hash bucket size then the first parameter should first be increased.

cd back to the root cd / and type following command and look for server_names_hash_bucket_size and remove #

sudo nano /etc/nginx/nginx.conf
http {
    
    server_names_hash_bucket_size 64;
    
}

after that type following command in order to check for errors.

nginx -t

Now restart the server and visit the URL yourdomain.com you should see message like this

Success! The yourdomain.com server block is working.

sudo systemctl restart nginx

(Visited 13 times, 1 visits today)