Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to configure Nginx virtual host with CentOS

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "CentOS how to configure Nginx virtual host". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to configure Nginx virtual host by CentOS".

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/

Thank you for reading, the above is the content of "how to configure Nginx virtual host for CentOS". After the study of this article, I believe you have a deeper understanding of how to configure Nginx virtual host for CentOS, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report