How to configure Nginx virtual host in CentOS 7.3

How to configure Nginx virtual host in CentOS 7.3

Experimental environment

A minimally installed CentOS 7.3 virtual machine

Configure the basic environment

1. Install nginx

yum install -y epel-*
yum isntall -y nginx vim

2. Create the site root directory of the virtual machine host

mkdir /var/wwwroot
mkdir /var/wwwroot/site1
mkdir /var/wwwroot/site2
echo -e "site1" >> /var/wwwroot/site1/index.html
echo -e "site2" >> /var/wwwroot/site2/index.html 

3. Disable CentOS firewall

setenforce 0
systemctl stop firewalld
systemctl disable firewalld 

Configuring port-based virtual hosts

1. Edit the nginx configuration file

vim /etc/nginx/conf.d/vhosts.conf

2. Add the following content

server {
  listen 8081;
  root /var/wwwroot/site1;
  index index.html;

  location / {
  }
}
server {
 listen 8082;
 root /var/wwwroot/site2;
 index index.html;

 location / {
 }
} 

3. Start nginx service

systemctl start nginx

4. Access two sites on the host

http://192.168.204.135:8081/
http://192.168.204.135:8082/

Configuring domain-based virtual hosts

1. Re-edit the nginx configuration file

vim /etc/nginx/conf.d/vhosts.conf

2. Delete the original content and add the following content again

server {
  listen 80;
  server_name site1.test.com;
  root /var/wwwroot/site1;
  index index.html;

  location / {
  }
}
server {
 listen 80;
 server_name site2.test.com;
 root /var/wwwroot/site2;
 index index.html;

 location / {
 }
} 

3. Restart nginx service

systemctl restart nginx

4. Modify the hosts file on Windows

Edit C:\Windows\System32\drivers\etc\hosts file,

Add the following content (modify according to actual situation)

192.168.204.135 site1.test.com

192.168.204.135 site2.test.com

5. Access two sites on the host

http://site1.test.com/
http://site2.test.com/

Configuring IP-based virtual hosts

1. Add two IP addresses to the virtual machine

ifconfig ens33:1 192.168.204.151
ifconfig ens33:2 192.168.204.152

2. Re-edit the nginx configuration file

vim /etc/nginx/conf.d/vhosts.conf

3. Delete the original content and add the following content again

server {
  listen 192.168.204.151:80;
  root /var/wwwroot/site1;
  index index.html;

  location / {
  }
}
server {
 listen 192.168.204.152:80;
 root /var/wwwroot/site2;
 index index.html;

 location / {
 }
} 

4. Restart nginx service

systemctl restart nginx

5. Access two sites on the host

http://192.168.204.151/
http://192.168.204.152/

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Alibaba Cloud Centos7 installation and configuration of SVN
  • How to add Nginx to system services in CentOS7
  • Detailed explanation of Nginx installation, SSL configuration and common commands under Centos7.x
  • Solution for not being able to use pip after installing python3.7.1 on centos6.5
  • Solution to the error when installing Docker on CentOS version
  • Three methods to modify the hostname of Centos7
  • How to set up scheduled backup tasks in Linux centos
  • Linux centOS installation JDK and Tomcat tutorial
  • How to build Jenkins+Maven+Git continuous integration environment on CentOS7
  • Centos7.5 configuration java environment installation tomcat explanation

<<:  Solve the problem of shrinking Mysql transaction log and log files being too large to shrink

>>:  JavaScript implements circular carousel

Recommend

Use Firebug tool to debug the page on iPad

How to debug a page on iPad? When using iOS 5, you...

Detailed explanation of how to configure Nginx web server sample code

Overview Today we will mainly share how to config...

MySQL transaction, isolation level and lock usage example analysis

This article uses examples to describe MySQL tran...

Detailed explanation of script debugging mechanism in bash

Run the script in debug mode You can run the enti...

Detailed explanation of nginx-naxsi whitelist rules

Whitelist rule syntax: BasicRule wl:ID [negative]...

Implementation of Docker deployment of web projects

The previous article has installed the docker ser...

Solution to SNMP4J server connection timeout problem

Our network management center serves as the manag...

Detailed explanation of the reasons why MySQL connections are hung

Table of contents 1. Background Architecture Prob...

Zabbix's psk encryption combined with zabbix_get value

Since Zabbix version 3.0, it has supported encryp...

A brief discussion on the characteristics of CSS float

This article introduces the characteristics of CS...

A brief introduction to JavaScript arrays

Table of contents Introduction to Arrays Array li...

Linux uses lsof command to check file opening status

Preface We all know that in Linux, "everythi...

Methods and techniques for quickly displaying web page images

1. Use .gifs rather than .jpgs. GIFs are smaller ...

MySQL Quick Data Comparison Techniques

In MySQL operation and maintenance, a R&D col...