Bootstrap FreeKB - Nginx (Web Server) - Virtual Hosts
Nginx (Web Server) - Virtual Hosts

Updated:   |  Nginx (Web Server) articles

Before web servers had virtual hosts, you would have had to had a physical server for each website that you wanted to produce.

 

Virtual hosts allow you to run multiple sites from a single physical server.

 

Let's say you want to create two sites, site1 and site2. In the /etc/nginx/nginx.conf file, create two server blocks, one for site1 and another for site 2. Notice www.site1.com has default_site. If there is some problem producing www.site2.com, the default site will be produced.

server {
  listen       80 default_server;
  listen       [::]:80 default_server;
  server_name  www.site1.com;
  root         /var/www/site1;
  }

server {
  listen       80;
  listen       [::]:80;
  server_name  www.site2.com;
  root         /var/www/site2;
  }

 

Create the /var/www/site1 and /var/www/site2 directories.

[root@server1 ~]# mkdir /var/www/site1
[root@server1 ~]# mkdir /var/www/site2

 

Create an index.html file for site1 and site2. Add unique content to each index.html file. For example, one could have Welcome to site1 and the other could have Welcome to site2.

[root@server1 ~]# mkdir /var/www/site1/index.html
[root@server1 ~]# mkdir /var/www/site2/index.html

 

The ps command can be used to determine if your system is using init or systemd. If PID 1 is init, then you will use the service command. If PID 1 is systemd, then you will use the systemctl command.

If your system is using systemd, use the systemctl command to start and enable nginx.

systemctl enable nginx
systemctl start nginx
systemctl status nginx

 

If your system is using init, use the chkconfig and service commands to start and enable nginx.

chkconfig nginx on
service nginx start
service nginx status

 


SELinux

Use the ls -Z command to view the SELinux permissions of the files in the /var/www/site1 and /var/www/site2 directories. In this example, the index.html context is var_t. nginx needs the type to be httpd_sys_content_t.

[root@server1 ~]# ls -Z  /var/www/site1
-rwxrwxrwx. root root unconfined_u:object_r:admin_home_t:s0 index.html

 

Use the restorecon command to update all of the directories and files used by nginx to have httpd_sys_content_t.

[root@server1 ~]# restorecon -Rv  /var/www/site1
[root@server1 ~]# restorecon -Rv  /var/www/site2

 

Use the ls -Z command again to confirm the context is httpd_sys_content_t.

[root@server1 ~]# ls -Z  /var/www/site1
-rwxrwxrwx. root root unconfined_u:object_r:httpd_sys_content_t:s0 index.html

 

Navigating to www.site1.com will now display Welcome to site1, and www.site2.com will display Welcome to site2.




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 845c30 in the box below so that we can be sure you are a human.