Nginx learning notes virtual machine host

Virtual host web service publishing needs to meet three preconditions: IP, PORT and domain name; A web server can only publish one web by default; In...

Virtual host

web service publishing needs to meet three preconditions: IP, PORT and domain name;

A web server can only publish one web by default;

In order to save resource cost, publishing multiple web requires virtual host. Therefore, virtual host is to divide a server into multiple "virtual" servers. Each virtual host can be divided into independent domain name and independent directory.

IP based virtual host

The requirement for publishing multiple web pages on a host based on IP is that the host has two or more IPS

The test does not meet the requirements of multiple network cards, so create a virtual network card. The configuration is as follows

[root@localhost ~]# ifconfig eth0:1 10.16.0.100/24 up
server { listen 10.16.0.9:81; #server_name localhost; charset utf-8; #access_log logs/host.access.log main; location / { root html/a; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } server { listen 10.16.0.100:81; #server_name localhost; charset utf-8; #access_log logs/host.access.log main; location / { root html/b; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }

Port based

As the name implies, port based means that port cannot be repeated

server { listen 81; #server_name localhost; charset utf-8; location / { root html/a; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } server { listen 82; #server_name localhost; charset utf-8; location / { root html/b; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }

Domain name

Domain name involves DNS resolution. Domain name can only be accessed after being resolved. Therefore, domain name needs to be added to / etc/hosts

[root@localhost conf]# cat /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 10.16.0.9 www.a.com 10.16.0.9 www.b.com

nginx is configured as follows:

server { listen 81; server_name www.a.com; charset utf-8; location / { root html/a; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } server { listen 81; server_name www.b.com; charset utf-8; location / { root html/b; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }

Access test:

[root@localhost conf]# curl http://www.a.com:81 this is web a [root@localhost conf]# curl http://www.b.com:81 this is web b









2 December 2019, 15:45 | Views: 7742

Add new comment

For adding a comment, please log in
or create account

0 comments