007.Nginx虚拟主机
一 虚拟主机1.1 虚拟主机概念
对于Nginx而言,每一个虚拟主机相当于一个在同一台服务器中却相互独立的站点,从而实现一台主机对外提供多个 web 服务,每个虚拟主机之间是独立的,互不影响的。1.2 虚拟主机类型
通过 Nginx 可以实现虚拟主机的配置,Nginx 支持三种类型的虚拟主机配置:- 基于 IP 的虚拟主机(较少使用)
- 基于域名的虚拟主机
- 基于端口的虚拟主机
二 基于IP虚拟主机2.1 配置多IP地址
1 [root@nginx ~]# ifconfig eth0:0 172.24.8.70 broadcast 172.24.8.255 netmask 255.255.255.0 2 [root@nginx ~]# ip addr | grep 172
3 inet 172.24.8.71/24 brd 172.24.8.255 scope global noprefixroute eth0
4 inet 172.24.8.72/24 brd 172.24.8.255 scope global secondary eth0:0
提示:如上在同一台主机添加多个IP地址。2.2 创建站点目录
1 [root@nginx ~]# mkdir /usr/share/nginx/ipvhost01/ 2 [root@nginx ~]# mkdir /usr/share/nginx/ipvhost02/
3 [root@nginx ~]# echo "<h1>Ipvhost01</h1>" > /usr/share/nginx/ipvhost01/index.html
4 [root@nginx ~]# echo "<h1>Ipvhost02</h1>" > /usr/share/nginx/ipvhost02/index.html
2.3 配置虚拟主机
1 [root@nginx ~]# vi /etc/nginx/conf.d/ipvhost01.conf 2 server {
3 listen 80; #监听端口
4 server_name ipvhost01.odocker.com 172.24.8.71; #配置虚拟主机名和IP
5 location / {
6 root /usr/share/nginx/ipvhost01; #请求匹配路径
7indexindex.html; #指定主页
8 access_log /var/log/nginx/ipvhost01.access.log main;
9 error_log /var/log/nginx/ipvhost01.error.log warn;
10 }
11 }
12 server {
13 listen 80;
14 server_name ipvhost02.odocker.com 172.24.8.72;
15 location / {
16 root /usr/share/nginx/ipvhost02;
17indexindex.html;
18 access_log /var/log/nginx/ipvhost02.access.log main;
19 error_log /var/log/nginx/ipvhost02.error.log warn;
20 }
21 }
1 [root@nginx ~]# nginx -t -c /etc/nginx/nginx.conf #检查配置文件 2 [root@nginx ~]# nginx -s reload #重载配置文件
2.4 确认验证
浏览器访问:ipvhost01.odocker.com。浏览器访问:ipvhost02.odocker.com。三 基于域名虚拟主机3.1 创建站点目录
1 [root@nginx ~]# mkdir /usr/share/nginx/webvhost01/ 2 [root@nginx ~]# mkdir /usr/share/nginx/webvhost02/
3 [root@nginx ~]# echo "<h1>Webvhost01</h1>" > /usr/share/nginx/webvhost01/index.html
4 [root@nginx ~]# echo "<h1>Webvhost02</h1>" > /usr/share/nginx/webvhost02/index.html
3.2 配置虚拟主机
1 [root@nginx ~]# vi /etc/nginx/conf.d/webvhost.conf 2 server {
3 listen 80;
4 server_name webvhost01.odocker.com;
5 location / {
6 root /usr/share/nginx/webvhost01;
7indexindex.html;
8 access_log /var/log/nginx/webvhost01.access.log main;
9 error_log /var/log/nginx/webvhost01.error.log warn;
10 }
11 }
12 server {
13 listen 80;
14 server_name webvhost02.odocker.com;
15 location / {
16 root /usr/share/nginx/webvhost02;
17indexindex.html;
18 access_log /var/log/nginx/webvhost02.access.log main;
19 error_log /var/log/nginx/webvhost02.error.log warn;
20 }
21 }
1 [root@nginx ~]# nginx -t -c /etc/nginx/nginx.conf #检查配置文件 2 [root@nginx ~]# nginx -s reload #重载配置文件
3.3 确认验证
浏览器访问:webvhost01.odocker.com。浏览器访问:webvhost02.odocker.com。
四 基于端口虚拟主机4.1 创建站点目录
1 [root@nginx ~]# mkdir /usr/share/nginx/portvhost01/ 2 [root@nginx ~]# mkdir /usr/share/nginx/portvhost02/
3 [root@nginx ~]# echo "<h1>Portvhost01</h1>" > /usr/share/nginx/portvhost01/index.html
4 [root@nginx ~]# echo "<h1>Portvhost01</h1>" > /usr/share/nginx/portvhost02/index.html
3.2 配置虚拟主机
1 [root@nginx ~]# vi /etc/nginx/conf.d/portvhost.conf 2 server {
3 listen 8080;
4 server_name portvhost01.odocker.com;
5 location / {
6 root /usr/share/nginx/portvhost01;
7indexindex.html;
8 access_log /var/log/nginx/portvhost01.access.log main;
9 error_log /var/log/nginx/portvhost01.error.log warn;
10 }
11 }
12 server {
13 listen 8081;
14 server_name portvhost02.odocker.com;
15 location / {
16 root /usr/share/nginx/portvhost02;
17indexindex.html;
18 access_log /var/log/nginx/access_portvhost02.log main;
19 }
20 }
1 [root@nginx ~]# nginx -t -c /etc/nginx/nginx.conf #检查配置文件 2 [root@nginx ~]# nginx -s reload #重载配置文件
3.3 确认验证
浏览器访问:portvhost01.odocker.com:8080浏览器访问:portvhost02.odocker.com:8081
2.1 配置多IP地址
1 [root@nginx ~]# ifconfig eth0:0 172.24.8.70 broadcast 172.24.8.255 netmask 255.255.255.0提示:如上在同一台主机添加多个IP地址。2 [root@nginx ~]# ip addr | grep 172
3 inet 172.24.8.71/24 brd 172.24.8.255 scope global noprefixroute eth0
4 inet 172.24.8.72/24 brd 172.24.8.255 scope global secondary eth0:0
2.2 创建站点目录
1 [root@nginx ~]# mkdir /usr/share/nginx/ipvhost01/2 [root@nginx ~]# mkdir /usr/share/nginx/ipvhost02/
3 [root@nginx ~]# echo "<h1>Ipvhost01</h1>" > /usr/share/nginx/ipvhost01/index.html
4 [root@nginx ~]# echo "<h1>Ipvhost02</h1>" > /usr/share/nginx/ipvhost02/index.html
2.3 配置虚拟主机
1 [root@nginx ~]# vi /etc/nginx/conf.d/ipvhost01.conf2 server {
3 listen 80; #监听端口
4 server_name ipvhost01.odocker.com 172.24.8.71; #配置虚拟主机名和IP
5 location / {
6 root /usr/share/nginx/ipvhost01; #请求匹配路径
7indexindex.html; #指定主页
8 access_log /var/log/nginx/ipvhost01.access.log main;
9 error_log /var/log/nginx/ipvhost01.error.log warn;
10 }
11 }
12 server {
13 listen 80;
14 server_name ipvhost02.odocker.com 172.24.8.72;
15 location / {
16 root /usr/share/nginx/ipvhost02;
17indexindex.html;
18 access_log /var/log/nginx/ipvhost02.access.log main;
19 error_log /var/log/nginx/ipvhost02.error.log warn;
20 }
21 }
1 [root@nginx ~]# nginx -t -c /etc/nginx/nginx.conf #检查配置文件2 [root@nginx ~]# nginx -s reload #重载配置文件
2.4 确认验证
浏览器访问:ipvhost01.odocker.com。浏览器访问:ipvhost02.odocker.com。三 基于域名虚拟主机3.1 创建站点目录
1 [root@nginx ~]# mkdir /usr/share/nginx/webvhost01/ 2 [root@nginx ~]# mkdir /usr/share/nginx/webvhost02/
3 [root@nginx ~]# echo "<h1>Webvhost01</h1>" > /usr/share/nginx/webvhost01/index.html
4 [root@nginx ~]# echo "<h1>Webvhost02</h1>" > /usr/share/nginx/webvhost02/index.html
3.2 配置虚拟主机
1 [root@nginx ~]# vi /etc/nginx/conf.d/webvhost.conf 2 server {
3 listen 80;
4 server_name webvhost01.odocker.com;
5 location / {
6 root /usr/share/nginx/webvhost01;
7indexindex.html;
8 access_log /var/log/nginx/webvhost01.access.log main;
9 error_log /var/log/nginx/webvhost01.error.log warn;
10 }
11 }
12 server {
13 listen 80;
14 server_name webvhost02.odocker.com;
15 location / {
16 root /usr/share/nginx/webvhost02;
17indexindex.html;
18 access_log /var/log/nginx/webvhost02.access.log main;
19 error_log /var/log/nginx/webvhost02.error.log warn;
20 }
21 }
1 [root@nginx ~]# nginx -t -c /etc/nginx/nginx.conf #检查配置文件 2 [root@nginx ~]# nginx -s reload #重载配置文件
3.3 确认验证
浏览器访问:webvhost01.odocker.com。浏览器访问:webvhost02.odocker.com。
四 基于端口虚拟主机4.1 创建站点目录
1 [root@nginx ~]# mkdir /usr/share/nginx/portvhost01/ 2 [root@nginx ~]# mkdir /usr/share/nginx/portvhost02/
3 [root@nginx ~]# echo "<h1>Portvhost01</h1>" > /usr/share/nginx/portvhost01/index.html
4 [root@nginx ~]# echo "<h1>Portvhost01</h1>" > /usr/share/nginx/portvhost02/index.html
3.2 配置虚拟主机
1 [root@nginx ~]# vi /etc/nginx/conf.d/portvhost.conf 2 server {
3 listen 8080;
4 server_name portvhost01.odocker.com;
5 location / {
6 root /usr/share/nginx/portvhost01;
7indexindex.html;
8 access_log /var/log/nginx/portvhost01.access.log main;
9 error_log /var/log/nginx/portvhost01.error.log warn;
10 }
11 }
12 server {
13 listen 8081;
14 server_name portvhost02.odocker.com;
15 location / {
16 root /usr/share/nginx/portvhost02;
17indexindex.html;
18 access_log /var/log/nginx/access_portvhost02.log main;
19 }
20 }
1 [root@nginx ~]# nginx -t -c /etc/nginx/nginx.conf #检查配置文件 2 [root@nginx ~]# nginx -s reload #重载配置文件
3.3 确认验证
浏览器访问:portvhost01.odocker.com:8080浏览器访问:portvhost02.odocker.com:8081
2 [root@nginx ~]# mkdir /usr/share/nginx/webvhost02/
3 [root@nginx ~]# echo "<h1>Webvhost01</h1>" > /usr/share/nginx/webvhost01/index.html
4 [root@nginx ~]# echo "<h1>Webvhost02</h1>" > /usr/share/nginx/webvhost02/index.html
2 server {
3 listen 80;
4 server_name webvhost01.odocker.com;
5 location / {
6 root /usr/share/nginx/webvhost01;
7indexindex.html;
8 access_log /var/log/nginx/webvhost01.access.log main;
9 error_log /var/log/nginx/webvhost01.error.log warn;
10 }
11 }
12 server {
13 listen 80;
14 server_name webvhost02.odocker.com;
15 location / {
16 root /usr/share/nginx/webvhost02;
17indexindex.html;
18 access_log /var/log/nginx/webvhost02.access.log main;
19 error_log /var/log/nginx/webvhost02.error.log warn;
20 }
21 }
2 [root@nginx ~]# nginx -s reload #重载配置文件
4.1 创建站点目录
1 [root@nginx ~]# mkdir /usr/share/nginx/portvhost01/2 [root@nginx ~]# mkdir /usr/share/nginx/portvhost02/
3 [root@nginx ~]# echo "<h1>Portvhost01</h1>" > /usr/share/nginx/portvhost01/index.html
4 [root@nginx ~]# echo "<h1>Portvhost01</h1>" > /usr/share/nginx/portvhost02/index.html
3.2 配置虚拟主机
1 [root@nginx ~]# vi /etc/nginx/conf.d/portvhost.conf2 server {
3 listen 8080;
4 server_name portvhost01.odocker.com;
5 location / {
6 root /usr/share/nginx/portvhost01;
7indexindex.html;
8 access_log /var/log/nginx/portvhost01.access.log main;
9 error_log /var/log/nginx/portvhost01.error.log warn;
10 }
11 }
12 server {
13 listen 8081;
14 server_name portvhost02.odocker.com;
15 location / {
16 root /usr/share/nginx/portvhost02;
17indexindex.html;
18 access_log /var/log/nginx/access_portvhost02.log main;
19 }
20 }
1 [root@nginx ~]# nginx -t -c /etc/nginx/nginx.conf #检查配置文件2 [root@nginx ~]# nginx -s reload #重载配置文件
3.3 确认验证
浏览器访问:portvhost01.odocker.com:8080浏览器访问:portvhost02.odocker.com:8081原文链接:https://www.cnblogs.com/itzgr/archive/2020/07/11/13282542.html
以上是 007.Nginx虚拟主机 的全部内容, 来源链接: utcz.com/z/518247.html