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

Detailed Introduction to the MySQL Keyword Distinct

Introduction to the usage of MySQL keyword Distin...

Determine whether MySQL update will lock the table through examples

Two cases: 1. With index 2. Without index Prerequ...

MySQL 5.7.17 installation and configuration graphic tutorial

Features of MySQL: MySQL is a relational database...

Detailed explanation of using javascript to handle common events

Table of contents 1. Form events 2. Mouse events ...

...

3 different ways to clear the option options in the select tag

Method 1 Copy code The code is as follows: documen...

Detailed tutorial on installation and configuration of nginx under Centos7

Note: The basic directory path for software insta...

MySQL uses events to complete scheduled tasks

Events can specify the execution of SQL code once...

Markup language - web application CSS style

Click here to return to the 123WORDPRESS.COM HTML ...

Implementation example of video player based on Vue

When the existing video player cannot meet the ne...

Vue3 (Part 2) Integrating Ant Design Vue

Table of contents 1. Integrate Ant Design Vue 2. ...

Vue routing to implement login interception

Table of contents 1. Overview 2. Routing Navigati...

Use momentJs to make a countdown component (example code)

Today I'd like to introduce a countdown made ...

Install Memcached and PHP Memcached extension under CentOS

Regarding the high-performance distributed memory...