Nginx实现多虚拟主机详细介绍
Posted 白-胖-子
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Nginx实现多虚拟主机详细介绍相关的知识,希望对你有一定的参考价值。
nginx虚拟主机
- 虚拟Web主机指的是在同一台物理服务器中发布多个Web站点或应用
- 独立的网站,独立的项目甚至独立的功能模块都可以使用虚拟主机进行发布
- Nginx可以基于不同的IP、不同的端口以及不同的域名实现不同的虚拟主机
- 虚拟主机内的资源收到虚拟主机配置文件内容约束,与其他虚拟主机相对独立
Nginx的配置文件的组成部分:
- 主配置文件:nginx.conf
- 子配置文件: include conf.d/*.conf
- fastcgi, uwsgi,scgi 等协议相关的配置文件
- mime.types:支持的mime类型,MIME
Nginx配置虚拟主机的方式
主配置文件中添加虚拟主机
- 在Nginx主配置文件nginx.conf中的http功能语句块内添加server字段
- server字段定义了虚拟主机的配置内容
http {
...
... #各server的公共配置
server { #每个server用于定义一个虚拟主机,第一个server为默认虚拟服务器
...
}
server {
...
server_name #虚拟主机名
root #主目录
alias #路径别名
location [OPERATOR] URL { #指定URL的特性
...
if CONDITION {
...
}
}
}
}
子配置文件添加虚拟主机
- 在Nginx主配置文件nginx.conf中的http功能语句块内添加includ字段
- 在外部子配置文件中添加server字段,定义虚拟主机配置内容
- 独立的网站或者项目应该写各自独立的<项目名>.conf配置文件
http {
......
# 在配置文件的最后面添加此行,
# 注意不要放在最前面,会导致前面的全局命令无法生效
include /apps/nginx/conf/conf.d/*.conf;
}
虚拟主机server字段配置说明
# 设置一个虚拟机主机
server { # 可以包含自己的全局块,同时也可以包含多个location模块。
## 如本虚拟主机监听的端口、本虚拟主机的名称和IP配置,多个server 可以使用一个端口,比如都使用80端口提供web服务
listen 80; # 配置server监听的端口
server_name localhost; # 本server的名称,当访问此名称的时候nginx会调用当前serevr内部的配置进程匹配。
# location其实是server的一个指令,为nginx服务器提供比较多而且灵活的指令,都是在location中体现的
location / { # 主要功能是基于nginx接受到的请求字符串,对用户请求的UIL进行匹配,并对特定的指令进行处理,
## 包括地址重定向、数据缓存和应答控制等功能都是在这部分实现,
### 另外很多第三方模块的配置也是在location模块中配置。
root html; # 相当于默认页面的目录名称,默认是安装目录的相对路径,可以使用绝对路径配置。
index index.html index.htm; # 默认的页面文件名称
}
error_page 500 502 503 504 /50x.html; # 错误页面的文件名称
location = /50x.html { # location处理对应的不同错误码的页面定义到/50x.html,这个跟对应其server中定义的目录下。
root html; #定义默认页面所在的目录
}
}
创建虚拟主机
- 确认子配置文件存放目录
[ -d /apps/nginx/vhost.d ] || mkdir -pv /apps/nginx/vhost.d
mkdir: created directory '/apps/nginx/vhost.d'
- 确认主配置文件已引用子配置文件
grep 'include' /apps/nginx/conf/nginx.conf
include mime.types;
# include fastcgi_params;
include /apps/nginx/conf/conf.d/*.conf;
- 如果没有引用,则在主配置文件nginx.conf中的http语句段内添加include字段
sed -ri.bak '/# HTTPS server/a include \\/apps\\/nginx\\/vhost.d\\/\\*\\.conf\\;' /apps/nginx/conf/nginx.conf
grep 'include' /apps/nginx/conf/nginx.conf
include mime.types;
# include fastcgi_params;
include /apps/nginx/conf/conf.d/*.conf;
include /apps/nginx/vhost.d/*.conf;
include /apps/nginx/conf/conf.d/*.conf;
1.创建一个基本的虚拟主机
- nginx装好以后默认已有一个站点
curl 127.0.0.1
<!DOCTYPE html>
<html>
<head>
<title>Welcome to 10.0.0.189 !</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to 10.0.0.189 !</h1>
<p>If you see this page, the 10.0.0.189 web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://10.0.0.189 .org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://10.0.0.189 .com/">nginx.com</a>.</p>
<p><em>Thank you for using 10.0.0.189 .</em></p>
</body>
</html>
- 可以把默认站点注销掉喽
1.1先把网站构建出来
[root@C8-189 ~]# [ -d /webdb/onepiece ] || mkdir -pv /webdb/onepiece && echo "One Piece!" >/webdb/onepiece/index.html
mkdir: created directory '/webdb'
mkdir: created directory '/webdb/onepiece'
[root@C8-189 ~]# cat /webdb/onepiece/index.html
One Piece!
1.2添加虚拟主机配置文件
cat > /apps/nginx/vhost.d/onepiece.conf <<SUN
server {
listen 80;
server_name localhost;
location / {
root /webdb/onepiece;
}
}
SUN
1.3注销掉默认主页
- 在主配置文件nginx.conf 中将默认主页的server语句段注释掉即可
[root@C8-189 ~]# vim /apps/nginx/conf/nginx.conf
1.4检查配置并重新加载配置文件使生效
[root@C8-189 ~]# nginx -t
nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /apps/nginx/conf/nginx.conf test is successful
[root@C8-189 ~]# nginx -s reload
[root@C8-189 ~]# curl 127.0.0.1
One Piece!
2.创建一个基于端口号的虚拟主机
- 使用不同的端口号可以创建独立的虚拟主机
2.1先把网站构建出来
[root@C8-189 ~]# [ -d /webdb/towpiece ] || mkdir -pv /webdb/towpiece && echo "Tow Piece!" >/webdb/towpiece/index.html
mkdir: created directory '/webdb/towpiece'
[root@C8-189 ~]# cat /webdb/towpiece/index.html
Tow Piece!
2.2添加虚拟主机配置文件
cat > /apps/nginx/vhost.d/towpiece.conf <<SUN
server {
listen 82;
server_name localhost;
location / {
root /webdb/towpiece;
}
}
SUN
2.3检查配置并重新加载配置文件使生效
[root@C8-189 ~]# nginx -s reload
[root@C8-189 ~]# curl 127.0.0.1
One Piece!
[root@C8-189 ~]# curl 127.0.0.1:82
Tow Piece!
- 查看端口,发现已经打开了80和82两个端口
ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0.0.0.0:111 0.0.0.0:*
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 128 0.0.0.0:82 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:111 [::]:*
LISTEN 0 128
3.创建一个基于IP的虚拟主机
- 当同一台服务器可以配置多个IP地址时,使用不同的IP可以创建独立的虚拟主机
3.1先把网站构建出来
[root@C8-189 ~]# [ -d /webdb/threepiece ] || mkdir -pv /webdb/threepiece && echo "Three Piece!" >/webdb/threepiece/index.html
mkdir: created directory '/webdb/threepiece'
[root@C8-189 ~]# cat /webdb/threepiece/index.html
Three Piece!
3.2增加物理机IP
ip addr add 10.0.0.3/24 dev eth0
[root@C8-189 ~]# ip addr add 10.0.0.3/24 dev eth0
[root@C8-189 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:0c:29:af:1a:22 brd ff:ff:ff:ff:ff:ff
inet 10.0.0.189/24 brd 10.0.0.255 scope global noprefixroute eth0
valid_lft forever preferred_lft forever
inet 10.0.0.3/24 scope global secondary eth0
valid_lft forever preferred_lft forever
3.3添加虚拟主机配置文件
cat > /apps/nginx/vhost.d/threepiece.conf <<SUN
server {
listen 10.0.0.3:80;
server_name localhost;
location / {
root /webdb/threepiece;
}
}
SUN
3.4检查配置并重新加载配置文件使生效
[root@C8-189 ~]# nginx -t
nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /apps/nginx/conf/nginx.conf test is successful
[root@C8-189 ~]# nginx -s reload
[root@C8-189 ~]# curl 127.0.0.1
One Piece!
[root@C8-189 ~]# curl 127.0.0.1:82
Tow Piece!
[root@C8-189 ~]# curl 10.0.0.3
Three Piece!
4.创建一个基于域名的虚拟主机
- ngxin可以使用解析至本机IP地址的不同域名创建独立的虚拟主机
4.1先把网站构建出来
[root@C8-189 ~]# [ -d /webdb/fourpiece ] || mkdir -pv /webdb/fourpiece && echo "Four Piece!" >/webdb/fourpiece/index.html
mkdir: created directory '/webdb/fourpiece'
[root@C8-189 ~]# cat /webdb/fourpiece/index.html
Four Piece!
4.2修改本地hosts文件模拟域名解析
[root@C8-189 ~]# cat >> /etc/hosts <<SUN
> 127.0.0.1 onepiece.sun.co
> 127.0.0.1 towpiece.sun.co
> 10.0.0.3 threepiece.sun.co
> 10.0.0.3 fourpiece.sun.co
> SUN
[root@C8-189 ~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
127.0.0.1 onepiece.sun.co
127.0.0.1 towpiece.sun.co
10.0.0.3 threepiece.sun.co
10.0.0.3 fourpiece.sun.co
[root@C8-189 ~]# fping -An {one,tow,three,four}piece.sun.co
onepiece.sun.co (127.0.0.1) is alive
towpiece.sun.co (127.0.0.1) is alive
threepiece.sun.co (10.0.0.3) is alive
fourpiece.sun.co (10.0.0.3) is alive
4.3添加虚拟主机配置文件
cat > /apps/nginx/vhost.d/fourpiece.conf <<SUN
server {
listen 80;
server_name onepiece.sun.co;
location / {
root /webdb/onepiece;
}
}
server {
listen 80;
server_name towpiece.sun.co;
location / {
root /webdb/towpiece;
}
}
server {
listen 80;
server_name threepiece.sun.co;
location / {
root /webdb/threepiece;
}
}
server {
listen 80;
server_name fourpiece.sun.co;
location / {
root /webdb/fourpiece;
}
}
SUN
4.4检查配置并重新加载配置文件使生效
[root@C8-189 ~]# nginx -t
nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /apps/nginx/conf/nginx.conf test is successful
[root@C8-189 ~]# nginx -s reload
[root@C8-189 ~]# curl {one,tow,three,four}piece.sun.co
One Piece!
Tow Piece!
Three Piece!
Three Piece!
[root@C8-189 ~]# curl fourpiece.sun.co
Three Piece!
[root@C8-189 ~]# cat /webdb/fourpiece/index.html
Four Piece!
- 因基于IP的虚拟主机将所有访问10.0.0.3的请求都丢给了threepiece
- 所以在基于域名的虚拟主机fourpiece也指向10.0.0.3的时候就会显示threepiece
- 修改DNS解析或者添加新的IP已实现fourpiece
[root@C8-189 ~]# vim /etc/hosts
[root@C8-189 ~]# nginx -s reload
[root@C8-189 ~]# ping fourpiece.sun.co
PING fourpiece.sun.co (127.0.0.1) 56(84) bytes of data.
64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.018 ms
64 bytes from localhost (127.0.0.1): icmp_seq=2 ttl=64 time=0.020 ms
64 bytes from localhost (127.0.0.1): icmp_seq=3 ttl=64 time=0.035 ms
^C
--- fourpiece.sun.co ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 69ms
rtt min/avg/max/mdev = 0.018/0.024/0.035/0.008 ms
[root@C8-189 ~]# curl fourpiece.sun.co
Four Piece!
5.创建一个基于子目录的二级子站
- 有时候我们使用相同域名或IP的方式增加独立子站
- 放着独立域名不用,将独立网站或项目放在已有域名子目录下进行访问
- 将已有独立域名重写到已有域名二级子目录
- 实现https证书全站覆盖时此方法有用
5.1先把网站构建出来
[root@C8-189 ~]# [ -d /webdb/fivepiece ] || mkdir -pv /webdb/fivepiece && echo "Five Piece!" >/webdb/fivepiece/index.html
mkdir: created directory '/webdb/fivepiece'
cat /webdb/fivepiece/index.html
Five Piece!
5.2添加虚拟主机配置文件
- 在已有网站的配置文件中server字段内部添加location字段
- 并在location字段中添加alias指明项目所在位置
cat /apps/nginx/vhost.d/threepiece.conf
server {
listen 10.0.0.3:80;
server_name threepiece.sun.co;
location / {
root /webdb/threepiece;
}
location /fivepiece {
alias /webdb/fivepiece;
}
}
- 创建新的虚拟主机文件
- 利用重写技术将独立域名重镜像至主站域名二级子目录
cat > /apps/nginx/vhost.d/fivepiece.conf <<SUN
server {
listen 10.0.0.3:80;
server_name fivepiece.sun.co;
location / {
root /data/nginx/html/pc;
index index.html;
rewrite / http://threepiece.sun.co/fivepiece permanent;
}
}
SUN
5.3检查配置并重新加载配置文件使生效
[root@C8-189 ~]# nginx -t
nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /apps/nginx/conf/nginx.conf test is successful
[root@C8-189 ~]# nginx -s reload
- 查看threepiece.sun.co网站内容
[root@C8-189 ~]# curl threepiece.sun.co
Three Piece!
- 查看threepiece.sun.co二级目录/fivepiece内容
[root@C8-189 ~]# curl threepiece.sun.co/fivepiece
<html>
<head><title>301 Moved Permanently</title></head>
<body>
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx</center>
</body>
</html>
[root@C8-189 ~]# curl -L threepiece.sun.co/fivepiece
Five Piece!
- 查看fivepiece.sun.co网站内容
[root@C8-189 ~]# curl fivepiece.sun.co
<html>
<head><title>301 Moved Permanently</title></head>
<body>
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx</center>
</body>
</html>
[root@C8-189 ~]# curl -L fivepiece.sun.co
Five Piece!
以上是关于Nginx实现多虚拟主机详细介绍的主要内容,如果未能解决你的问题,请参考以下文章
技术干货实战- 分布式集群部署模式下Nginx如何实现用户登录Session共享(含详细配置与代码实战)