• Categories
    Category
  • Categories
    Category
  • News
  • Tutorials
  • Forums
  • Tags
  • Users
Tutorial Comments FAQ Related Articles

How to create virtual host in NGINX (Both Name and IP based)

316

To create virtual host in NGINX (Both Name and IP based)

Nginx is an open source high performance Load balancer and web server software. It supports HTTP, HTTPS, SMTP, POP3, and IMAP protocols as a reverse proxy server. Creating virtual host in NGINX is explained in this article.

Testing Environment

Operating System : CentOS 6
IP-Address : 192.168.11.182 and 192.168.11.183
Web Server : Nginx
Virtual-host Domains : www.testing1.com and www.testing2.com.


Installation of Nginx

Install the Nginx package from the epel repository by using the following command.

[root@linuxhelp~]# yum install --enablerepo=epel nginx
Loaded plugins: aliases, changelog, fastestmirror, kabi, security
Setting Up install process
Resolving dependencies
-->  Running transaction check
-->  Package nginx.x86_64 1.0.15-12.el6 will be installed.
.
.
.
Installaed:
 nginx.x86_64 1.0.15-12.el6  is installed
Complete!

Start the nginx service.

[root@linuxhelp~]# service httpd stop
Stopping httpd:   [ ok ]
[root@linuxhelp~]# service nginx start
Starting nginx:     [ ok ]

Name-based Virtual Hosts

Create a user, to avoid permission issues.

[root@linuxhelp~]# useradd user1

Then create the directories for the virtual domains in /var/www path.

[root@linuxhelp~]# mkdir /var/www/testing1.com/public_html
[root@linuxhelp~]# mkdir /var/www/testing2.com/public_html

Go to that public_html directory and create the index file with some html code.

[root@linuxhelp~]# cd /var/www/testing1.com/public_html
[root@linuxhelp public_html]# vim index.html
< html> 
< head> 
< title> 
TESTING 1
< /title> 
< /head> 
< body> 
This is the first domain
< /body> 
< /html> 

Create a directory " sites-available" in /etc/nginx path

[root@linuxhelp~]# mkdir /etc/nginx/sites-available

Go to the sites-available directory and create two config files with the name of our virtual domains for easy reference.

[root@linuxhelp~]# cd /etc/nginx/sites-available
[root@linuxhelp sites-available]# touch testing1.com.conf testing2.com.conf

Add the following lines into testing1.com.conf file.

 server{
        listen          80 
        server_name     testing1.com www.testing1.com 
        location /{
                root /var/www/testing1.com/public_html 
                index index.html 
                }
}

Add the following contents into testing2.com.cong file.

server{
        listen       80 
        server_name     testing2.com www.testing2.com 
        location /{
                root /var/www/testing2.com/public_html 
                index index.html 
                }
}

Now change the ownership to user1 and the permissions as 755 for all the config and index files.

[root@linuxhelp sites-available]# chgrp user1 *
[root@linuxhelp sites-available]# ls -l
total 8
-rw-r--r-- 1 root user1 147 Jun 27 22:27 testing1.com.conf
-rw-r--r-- 1 root user1 147 Jun 27 22:28 testing2.com.conf
[root@linuxhelp~]# cd /var/www/testing1.com/public_html
[root@linuxhelp public_html]# chgrp user1 index.html

Follow the above steps for another domain(www.testing2.com) also.

Edit the nginx.conf file as shown below:
Uncomment the " user nginx" line and modify the " include" path as follows.

#user  nginx 
worker_processess  1 
http{
    include  /etc/nginx/mime.types 
    include  /etc/nginx/sites-available/*.conf 
    default_type application/octet-stream 
    .
    .
    .
}

create-virtual-host-NGINX-Both-Name-and-IP-based-Load-balancer-web-server-reverse-proxy-server-config_file

After all the setup is done, edit the hosts file.

[root@linuxhelp~]# vim /etc/hosts

192.168.11.182  testing1.com  www.testing1.com
192.168.11.182  testing2.com  www.testing2.com

Restart the nginx service.

[root@linuxhelp~]# service nginx restart
Stopping nginx:      [ ok ]
Starting nginx:        [ ok ]

Open the browser and navigate to the first virtual domain, http://testing1.com or http://www.testing1.com, the content in testing1.com' s index file will be displayed.

create-virtual-host-NGINX-Both-Name-and-IP-based-Load-balancer-web-server-reverse-proxy-server-first-virtual-domain

And for second virtual domain, type http://testing2.com or http://www.testing2.com
create-virtual-host-NGINX-Both-Name-and-IP-based-Load-balancer-web-server-reverse-proxy-server-second-virtual-domain


IP based virtual hosts

IP based virtual hosting, can be performed in two interface cards or in the same interface, split the eth0 interface into two for temporary configuration.
Check the interface by running the ifconfig command.

[root@linuxhelp1 ~]# ifconfig
eth0      Link encap:Ethernet  HWaddr 00:0C:29:4A:BF:EA  
          inet addr:192.168.11.182  Bcast:192.168.11.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe4a:bfea/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:50968 errors:0 dropped:0 overruns:0 frame:0
          TX packets:36601 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:73713310 (70.2 MiB)  TX bytes:2642608 (2.5 MiB)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:138 errors:0 dropped:0 overruns:0 frame:0
          TX packets:138 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:13218 (12.9 KiB)  TX bytes:13218 (12.9 KiB)

To split the eth0 interface run the following command.

[root@linuxhelp1 ~]# ifconfig eth0:1 192.168.11.183
[root@linuxhelp1 ~]# ifconfig
eth0      Link encap:Ethernet  HWaddr 00:0C:29:4A:BF:EA  
          inet addr:192.168.11.182  Bcast:192.168.11.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe4a:bfea/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:50968 errors:0 dropped:0 overruns:0 frame:0
          TX packets:36601 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:73713310 (70.2 MiB)  TX bytes:2642608 (2.5 MiB)

eth0:1    Link encap:Ethernet  HWaddr 00:0C:29:4A:BF:EA  
          inet addr:192.168.11.183  Bcast:192.168.11.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:138 errors:0 dropped:0 overruns:0 frame:0
          TX packets:138 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:13218 (12.9 KiB)  TX bytes:13218 (12.9 KiB)

After the split up is done, edit the virtual domain' s config files. Point the first IP (192.168.11.182) to first virtual domain with default port number 80.

[root@linuxhelp1 ~]# vim /etc/nginx/sites-available/testing1.com.conf
server {
    listen    192.168.11.182:80 
    server_name    testing1.com www.testing1.com
    location /{
        root /var/www/testing1.com/public_html 
        index index.html 
        }
}

pointing the second IP (192.168.11.183) to second virtual domain.

[root@linuxhelp1 ~]# vim /etc/nginx/sites-available/testing2.com.conf

server {
    listen    192.168.11.183:80 
    server_name    testing3.com www.testing2.com
    location /{
        root /var/www/testing2.com/public_html 
        index index.html 
        }
}

Now restart the nginx service.

[root@linuxhelp~]# service nginx restart
Stopping nginx:      [ ok ]
Starting nginx:        [ ok ]

Open the browser and navigate to http://< IP_address>
It opens the index file of testing1.com domain.
create-virtual-host-NGINX-Both-Name-and-IP-based-Load-balancer-web-server-reverse-proxy-server-testing1.com-domain

Open the browser and navigate to http://< IP_address>
It opens the index file of testing2.com domain.
create-virtual-host-NGINX-Both-Name-and-IP-based-Load-balancer-web-server-reverse-proxy-server-testing2.com-domain

Tags:
oliver
Author: 

Comments ( 0 )

No comments available

Add a comment

Frequently asked questions ( 5 )

Q

Is it safe to use the development branch in production?

A

In general, all releases (development or otherwise) are quite stable. This site runs the latest development version at all times. Many NGINX users tend to represent an “early adopter” crowd,

Q

Can I deploy NGINX Plus for HTTP video?

A

Yes, NGINX Plus is a very popular solution for HTTP video distribution, and a great, high-performance, highly scalable, extremely cost-efficient alternative to Wowza or Adobe products.

Q

What is thePurpose of using the Nginx?

A

Nginx is an open source high performance Load balancer and web server software. It supports HTTP, HTTPS, SMTP, POP3, and IMAP protocols as a reverse proxy server.

Q

Does NGINX Plus support load balancing for mail protocols?

A

Nginx support load balancing for HTTP/HTTPS/FastCGI/uwsgi/SCGI/memcache only. Nginx proxy and route SMTP/POP3/IMAP traffic to a single upstream rather than load-balancing it.

Q

Whether the Nginx is available for windows?

A

Yes,it is avaliable on windows.

Related Tutorials in How to create virtual host in NGINX (Both Name and IP based)

Related Tutorials in How to create virtual host in NGINX (Both Name and IP based)

How to Configure Nginx as a Reverse Proxy in CentOS
How to Configure Nginx as a Reverse Proxy in CentOS
Nov 26, 2016
How to Configure HAproxy Load Balancer with Keepalived in CentOS
How to Configure HAproxy Load Balancer with Keepalived in CentOS
Nov 21, 2016
How to setup Simple Mail Transfer Protocol (SMTP) in RHEL 7
How to setup Simple Mail Transfer Protocol (SMTP) in RHEL 7
Mar 25, 2016
How to Configure Reverse Proxy with Apache in CentOS
How to Configure Reverse Proxy with Apache in CentOS
Jan 25, 2017
How to install Multiple Nginx instances in same Server on CentOS 6
How to install Multiple Nginx instances in same Server on CentOS 6
Oct 26, 2017
How to install and configure Zimbra mail server
How to install and configure Zimbra mail server
Jul 1, 2016
How to Configure Load Balancer in Apache
How to Configure Load Balancer in Apache
Nov 18, 2016
How to configure Nginx Load Balancer in CentOS
How to configure Nginx Load Balancer in CentOS
Nov 9, 2016

Related Forums in How to create virtual host in NGINX (Both Name and IP based)

Related Forums in How to create virtual host in NGINX (Both Name and IP based)

Ubuntu
matthew class=
Failed to enable unit: Refusing to operate on linked unit file sshd.service
Apr 15, 2019
Nginx
gokulravichandran2 class=
Failed to start A high performance web server and a reverse proxy server
Dec 23, 2018
Apache
elijah class=
What is the difference between httpd and apache
Feb 18, 2017
CentOS
AadrikaAnshu class=
What does fastcgi_params file contain and what is it used for in nginx on CentOS 7.6
Jun 14, 2019
Nginx
AadrikaAnshu class=
A common misconfiguration is that the 'session.save_path' directive is not pointing to a valid directory.
Jun 15, 2019
Nginx
levi class=
php files are downloading not executing in nginx
Apr 21, 2017
Virtualhost
issacjoseph class=
"Oops! Your system prevents changing hosts file. Laragon will temporarily disable "Auto crate virtual hosts" feature!" in laragon
Nov 27, 2020
Nginx
lucky class=
[emerg] 32915#32915: bind() to 0.0.0.0:8989 failed (13: Permission denied)
Jun 13, 2019

Related News in How to create virtual host in NGINX (Both Name and IP based)

Related News in How to create virtual host in NGINX (Both Name and IP based)

Despite ISP Opposition, All Major Browsers will Eventually Roll Out DNS-over-HTTPS
Despite ISP Opposition, All Major Browsers will Eventually Roll Out DNS-over-HTTPS
Nov 12, 2019
Back To Top!
Rank
User
Points

Top Contributers

userNamenaveelansari
135850

Top Contributers

userNameayanbhatti
92510

Top Contributers

userNamehamzaahmed
32150

Top Contributers

1
userNamelinuxhelp
31040

Top Contributers

userNamemuhammadali
24500
Can you help Gibbson ?
How do i run both nginx and apache in same instance on centos

Hi...,

my server is based centos operating system and my webserver is already running on Apache.... i need to run both apache and nginx on same instance ... please help me to implement this concept...

Networking
  • Routing
  • trunk
  • Netmask
  • Packet Capture
  • domain
  • HTTP Proxy
Server Setup
  • NFS
  • KVM
  • Memory
  • Sendmail
  • WebDAV
  • LXC
Shell Commands
  • Cloud commander
  • Command line archive tools
  • last command
  • Shell
  • terminal
  • Throttle
Desktop Application
  • Linux app
  • Pithos
  • Retrospect
  • Scribe
  • TortoiseHg
  • 4Images
Monitoring Tool
  • Monit
  • Apache Server Monitoring
  • EtherApe 
  • Arpwatch Tool
  • Auditd
  • Barman
Web Application
  • Nutch
  • Amazon VPC
  • FarmWarDeployer
  • Rukovoditel
  • Mirror site
  • Chef
Contact Us | Terms of Use| Privacy Policy| Disclaimer
© 2025 LinuxHelp.com All rights reserved. Linux™ is the registered trademark of Linus Torvalds. This site is not affiliated with linus torvalds in any way.