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
3. Turn off the centos firewall
setenforce 0 systemctl stop firewalld systemctl disable firewalld
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 / { } }
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/
##Configure a virtual host based on domain name
1. Re-edit the nginx configuration filevim /etc/nginx/conf.d/vhosts.conf
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 / { } }
nginx service
systemctl restart nginx
hosts file
c:\windows\system32\drivers\etc\hosts file,
192.168.204.135 site1 .test.com
192.168.204.135 site2.test.com
http://site1.test.com/
http://site2.test.com/
Configure an IP-based virtual host
1. Add two IP addresses to the virtual machineifconfig ens33:1 192.168.204.151 ifconfig ens33:2 192.168.204.152
vim /etc/nginx/conf.d/vhosts.conf
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 / { } }
nginxService
systemctl restart nginx
http://192.168.204.151/
http://192.168. 204.152/
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!