How to configure Nginx virtual host in CentOS7.3

WBOY
Release: 2023-05-15 20:55:19
forward
1423 people have browsed it

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
Copy after login

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
Copy after login

How to configure Nginx virtual host in CentOS7.3

3. Turn off the centos firewall

setenforce 0
systemctl stop firewalld
systemctl disable firewalld
Copy after login

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
Copy after login
Copy after login
Copy after login

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 / {
 }
}
Copy after login

How to configure Nginx virtual host in CentOS7.3

3. Start the nginx service

systemctl start nginx
Copy after login

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
Copy after login
Copy after login
Copy after login

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 / {
 }
}
Copy after login

How to configure Nginx virtual host in CentOS7.3

3. Restart the

nginx service

systemctl restart nginx
Copy after login
Copy after login

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
Copy after login

2. Re-edit the nginx configuration file

vim /etc/nginx/conf.d/vhosts.conf
Copy after login
Copy after login
Copy after login

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 / {
 }
}
Copy after login

How to configure Nginx virtual host in CentOS7.3

4. Restart

nginxService

systemctl restart nginx
Copy after login
Copy after login
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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!