Server Blocks, often referred to as Nginx virtual host are a feature of the Nginx web server that allows you to host multiple websites on one server. As opposed to setting up and configuring a server for each domain, hosting a number of websites on a single machine saves both time and money.
Procedure:
Create Directory Structure: To host multiple site on Nginx need to create individual directory structure to store data. In example I have created 2 site virtual host under /var/www directory
# mkdir -p /var/www/site1.com/html
# mkdir -p /var/www/site2.com/html
Change Permission on directory: Change ownership on both site directories using chown command
# chown -R $user1.$user1 /var/www/site1.com/html
# chown -R $user2.$user2 /var/www/site2.com/html
# chmod -R 755 /var/www
Create index.html in both Directory: Create index.html file on both site directory respectively.
Setup Environment for Server Block: Before setup server block need to create 2 directory for setup.
- Sites-available : Directory store server blocks
- Sites-enable : Directory which tell Nginx to publish and block share content
Open Nginx configuration and modify file
vi /etc/nginx/nginx.config [ Inside http block add following two lines]
include /etc/nginx/sites-enabled/*.conf
server_names_hash_bucket_size 64;
First line to check sites-enabled directory and second line for increase memory is reserved for examine multiple domain name.
After made changes run below command to verify the configuration
# nginx -t
If syntax is OK then test was successful as in image.
Create Virtual Host for the first website with cp and make a copy exact copy of file
# cp /etc/nginx/conf.d/default.conf /etc/nginx/sites-available/site1.com.conf
Open configuration file using vi editor
# vi /etc/nginx/sites-available/site1.com.conf
You need to edit below 3 lines in configuration
server name site1.com www.site1.com;
root /var/www/site1.com/html;
try_files $uri $uri/ =404;
Same configuration you need to done for site2 just need to change name.
Enable Site configuration or Server block: To enable configuration need to link site available with site enable.
# ln -s /etc/nginx/sites-available/site1.com.conf /etc/nginx/sites-enabled/site1.com.conf
Restart Nginx Service:
# systemctl restart nginx
# vi /etc/hosts
ip_address site1.com
ip_address site2.com
Verify Server Blocks Setup
http://site1.com
http://site2.com
No comments:
Post a Comment