Get the App
SLTechnology News&Howtos  ›  Servers  › 

CentOS 7.3.How to configure Nginx virtual host

Shulou Source: shulou.com Published: 2022-06-01 19:54:21 10月30日 Update

This article mainly introduces how to configure the Nginx virtual host in CentOS 7.3. it is very detailed and has a certain reference value. Interested friends must read it!

Experimental environment

A minimally installed CentOS 7.3virtual machine

Configure the basic environment

1. Install nginx

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

two。 Establish the site root directory of the virtual host

Mkdir / var/wwwrootmkdir / var/wwwroot/site1mkdir / var/wwwroot/site2echo-e "site1" > > / var/wwwroot/site1/index.htmlecho-e "site2" > > / var/wwwroot/site2/index.html

3. Turn off CentOS's firewall

Setenforce 0systemctl stop firewalldsystemctl disable firewalld

Configure a port-based virtual host

1. Edit nginx profile

Vim / etc/nginx/conf.d/vhosts.conf

two。 Add the following

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. Visit two sites on the host

Http://192.168.204.135:8081/

Http://192.168.204.135:8082/

Configure a domain name-based virtual host

1. Re-edit the nginx profile

Vim / etc/nginx/conf.d/vhosts.conf

two。 Delete the original content and re-add the following

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 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 (modify it according to the actual situation)

192.168.204.135 site1.test.com

192.168.204.135 site2.test.com

5. Visit two sites on the host

Http://site1.test.com/

Http://site2.test.com/

Configure a virtual host based on IP

1. Add two IP addresses to the virtual machine

Ifconfig ens33:1 192.168.204.151ifconfig ens33:2 192.168.204.152

two。 Re-edit the nginx profile

Vim / etc/nginx/conf.d/vhosts.conf

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

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

4. Restart the nginx service

Systemctl restart nginx

5. Visit two sites on the host

Http://192.168.204.151/

Http://192.168.204.152/

These are all the contents of the article "how to configure Nginx virtual hosts in CentOS 7.3.Thank you for reading!" Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Tags: Configuration content host virtual host file two site host service environment article minimum value interest address domain name actual buddy partner Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Xiaomi macOS Shulou Tech Info Docker