How to Configure Nginx Virtualhost in FreeBSD

To Configure Nginx Virtualhost in FreeBSD

Virtual hosting is a technique used to run more than one website on a single server. It allows the users to use multiple host names while sharing its resources. This article explains the configuration process of Nginx Virtual Host in FreeBSD.

Configuration of Nginx Virtualhost

First install the FEMP by using the following command.

https://www.linuxhelp.com/how-to-install-femp-in-freebsd/

Utilize the following command to check whether the Nginx is installed or not.

root@linuxhelp:~ # service -e
.
.
.
/usr/local/etc/rc.d/php-fpm
/usr/local/etc/rc.d/nginx
/usr/local/etc/rc.d/mysql-server
.
.
.
Done!

Create a new directory for virtualhost domains.

root@linuxhelp:~ # mkdir -p /var/www/vhost1.com
root@linuxhelp:~ # mkdir -p /var/www/vhost2.com 


Then create the index.html file for both virtualhost domains.

root@linuxhelp:~ # nano /var/www/vhost1.com/index.html

FIRST VHOST
This is first virtualhost....!

root@linuxhelp:~ # nano /var/www/vhost2.com/index.html

SECOND VHOST
This is Second virtualhost....!

Now you need to configure the virtual host in nginx.conf file.

Configure virtualhost within the “ http” tag.

root@linuxhelp:~ # nano /usr/local/etc/nginx/nginx.conf
.
.
.
http { 
.
.
.
#first virtualhost
server {
        listen       80 
       server_name  test.vhost1.com 
        location / {
        root /var/www/vhost1.com 
        index  index.html index.htm 
        }
           }
#second virtualhost
server {
        listen       80 
       server_name  test.vhost2.com 
        location / {
        root /var/www/vhost2.com 
        index  index.html index.htm 
        }
           }

}


Immediately add the virtualhost domain in the hosts file.

root@linuxhelp:~ # nano /etc/hosts 

192.168.5.130   test.vhost1.com
192.168.5.130   test.vhost2.com


Restart the Nginx service to take effect.

root@linuxhelp:~ # service nginx restart
Performing sanity check on nginx configuration:
nginx: the configuration file /usr/local/etc/nginx/nginx.conf syntax is ok
nginx: configuration file /usr/local/etc/nginx/nginx.conf test is successful
Stopping nginx.
Waiting for PIDS: 655.
Performing sanity check on nginx configuration:
nginx: the configuration file /usr/local/etc/nginx/nginx.conf syntax is ok
nginx: configu
ration file /usr/local/etc/nginx/nginx.conf test is successful
Starting nginx.


Now its time to access your virtual host domains in web browser. Open your web browser with the IP address.

Check your configured virtualhost as shown below,

FAQ
Q
What is the difference between Proxy and Nginx Reverse Proxy?
A
Differences Between Forward Proxy and Reverse Proxy. The main difference between the two is that forward proxy is used by the client such as a web browser whereas reverse proxy is used by the server such as a web server. Forward proxy and the client can be in the same internal network, or it can be on the Internet.
Q
Why Nginx is faster than Apache?
A
Nginx can serve more requests per second than Apache on the same hardware.
Q
We can define custom locations for the domain logs on the Nginx Web server?
A
Custom log locations can be assigned for Nginx web server for different Virtual Host.
Q
In Centos what is the default nginx configuration file?
A
On Centos the default nginx configuration file is # vim /etc/nginx/conf.d/virtual.conf
Q
How to give user and group permission for all the files in Nginx?
A
For Nginx web servers the user and group name can be assigned as
# chown -R nginx:nginx