>/var/wwwroot/site1/index.htmlecho-e"site2">>/var/"/> >/var/wwwroot/site1/index.htmlecho-e"site2">>/var/">

Home >Operation and Maintenance >Nginx >How to configure Nginx virtual host in CentOS7.3

How to configure Nginx virtual host in CentOS7.3

WBOY
WBOYforward
2023-05-15 20:55:191513browse

Experimental environment

A minimally installed centos 7.3 virtual machine

Configuring the basic environment

1. Install nginx

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

2. Establish 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

How to configure Nginx virtual host in CentOS7.3

3. Turn off the centos firewall

setenforce 0
systemctl stop firewalld
systemctl disable firewalld

How to configure Nginx virtual host in CentOS7.3

Configure port-based virtual host

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 / {
 }
}

How to configure Nginx virtual host in CentOS7.3

3. Start the nginx service

systemctl start nginx

4. Access two sites on the host machine

http://192.168 .204.135:8081/
http://192.168.204.135:8082/

How to configure Nginx virtual host in CentOS7.3

How to configure Nginx virtual host in CentOS7.3

##Configure a virtual host based on domain name

1. Re-edit the nginx configuration file

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

2. Delete the original content and re-add the following content

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 / {
 }
}

How to configure Nginx virtual host in CentOS7.3

3. Restart the

nginx service

systemctl restart nginx

4. Modify the

hosts file

on windows Edit the

c:\windows\system32\drivers\etc\hosts file,

Add the following content (modify it according to the actual situation)

192.168.204.135 site1 .test.com

192.168.204.135 site2.test.com

How to configure Nginx virtual host in CentOS7.3

5. Access both on the host machine Site

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

How to configure Nginx virtual host in CentOS7.3

How to configure Nginx virtual host in CentOS7.3

Configure an IP-based virtual host

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 re-add the following content

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 / {
 }
}

How to configure Nginx virtual host in CentOS7.3

4. Restart

nginxService

systemctl restart nginx

5. Access two sites on the host machine

http://192.168.204.151/
http://192.168. 204.152/

How to configure Nginx virtual host in CentOS7.3

How to configure Nginx virtual host in CentOS7.3

The above is the detailed content of How to configure Nginx virtual host in CentOS7.3. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete