您的位置:首页 > 运维架构 > Nginx

Nginx技术深度剖析(2)

2017-08-10 19:00 337 查看
Nginx虚拟主机配置实战:

虚拟主机的概念:
所谓虚拟主机就是在Web主机里的一个独立的网站站点,这个站点对应独立的域名(也可能是IP地址或端口),具有独立的程序及资源目录,可以独立的对外提供服务供用户访问。
这个独立的站点在配置里是由一定格式的标签段标记,对于Apache软件来说,一个独立的虚拟主机的标签段通常包含在<VirtualHost></VirtualHost>内,而nginx软件则使用一个server{}标签标识一个虚拟主机。
(1)基于域名的虚拟主机
是企业中应用最广泛的主机类型。
(2)基于IP的虚拟主机
一般不同的业务有需要使用多个IP地址的场景都会在均衡器上进行VIP上绑定,而不是Web上绑定IP区分不同的虚拟机。
(3)基于端口的虚拟主机
此类虚拟主机对应企业应用,主要为公司内部的网站提供服务。
---------------------------------------------------------------------------------------------------------------
配置基于域名的Web服务:
---------------------------------------------------------------------------------------------------------------
(1)修改配置文件:
vim /application/nginx/conf/nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name www.smartbro.com; #设置域名
location / {
root html/www; #修改站点的目录
index index.html index.htm;
}
}
}
:wq
(2)创建域名对应的站点目录及内容
mkdir /application/nginx/html/www #创建站点目录
echo 'Welcome to www.smartbro.com!' > /application/nginx/html/www/index.html #追加文本到主页文件
/application/nginx/sbin/nginx -t #检查配置文件的语法错误
nginx: the configuration file /application/nginx-1.13.4//conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.13.4//conf/nginx.conf test is successful
/application/nginx/sbin/nginx -s reload #平滑加载Nginx配置文件
netstat -tunlap | grep 80 #查看端口号
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 37232/nginx
ps -aux | grep nginx #查看进程
root 37232 0.0 0.1 44764 1784 ? Ss 07:47 0:00 nginx: master process /application/nginx/sbin/nginx
nginx 39418 0.0 0.1 45220 1808 ? S 14:10 0:00 nginx: worker process
root 39422 0.0 0.0 103308 852 pts/0 S+ 14:12 0:00 grep nginx
(3)修改本地hosts文件:
vim /etc/hosts
192.168.10.3 www.smartbro.com
:wq
访问测试:
curl http://www.smartbro.com #使用浏览器测试
Welcome to www.smartbro.com!
修改Windows的hosts文件:
路径:%SYSTEMROOT%\System32\drivers\etc\hosts
使用NotePad++打开编辑。
# 192.168.10.3 www.smartbro.com
在Windows命令终端测试:
C:\Users\xvGe>ping www.smartbro.com #测试域名的连通性
正在 Ping smartbro.com [104.131.163.31] 具有 32 字节的数据:
来自 104.131.163.31 的回复: 字节=32 时间=284ms TTL=51
来自 104.131.163.31 的回复: 字节=32 时间=284ms TTL=51
来自 104.131.163.31 的回复: 字节=32 时间=283ms TTL=51
来自 104.131.163.31 的回复: 字节=32 时间=284ms TTL=51
104.131.163.31 的 Ping 统计信息:
数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失),
往返行程的估计时间(以毫秒为单位):
最短 = 283ms,最长 = 284ms,平均 = 283ms
------------------------------------------------------------------------------------------------------------------
配置基于多个域名的Web服务:
------------------------------------------------------------------------------------------------------------------
修改配置文件/application/nginc/conf/nginx.conf:
增加sever区块:
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
#网站的服务器区块
server {
listen 80;
server_name www.smartbro.com; #定义网址
location / {
root html/www; #定义网页文件目录
index index.html index.htm;
}
}
#论坛网站的服务器区块
server {
listen 80;
server_name bbs.smartbro.com; #定义网址
location / {
root html/bbs; #定义网页文件目录
index index.html index.htm;
}
}
#网盘网站的服务器区块
server {
listen 80;
server_name pan.smartbro.com; #定义网址
location / {
root html/pan; #定义网页文件目录
index index.html index.htm;
}
}

}
/application/nginx/sbin/nginx -t #检查语法错误
nginx: the configuration file /application/nginx-1.13.4//conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.13.4//conf/nginx.conf test is successful
/application/nginx/sbin/nginx -s reload #平滑重启Nginx
修改本地hosts文件:
192.168.10.3 www.smartbro.com bbs.smartbro.com pan.smartbro.com
创建网站目录和首页文件:
mkdir /application/nginx/html/bbs
mkdir /application/nginx/html/pan
echo 'Welcome to bbs.smartbro.com' > /application/nginx/html/bbs/index.html
echo 'Welcome to pan.smartbro.com' > /application/nginx/html/pan/index.html
使用浏览器进行测试:
curl http://bbs.smartbro.com Welcome to bbs.smartbro.com
curl http://pan.smartbro.com Welcome to pan.smartbro.com
-----------------------------------------------------------------------------------------
配置基于端口的主机配置实战:
-----------------------------------------------------------------------------------------
修改Nginx主配置文件:
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80; #默认的端口号
server_name www.smartbro.com;
location / {
root html/www;
index index.html index.htm;
}
}

server {
listen 8080; #修改端口号
server_name www.smartbro.com;
location / {
root html/bbs;
index index.html index.htm;
}
}

server {
listen 8090; #修改端口号
server_name www.smartbro.com;
location / {
root html/pan;
index index.html index.htm;
}
}

}
/application/nginx/sbin/nginx -t #检查语法配置
nginx: the configuration file /application/nginx-1.13.4//conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.13.4//conf/nginx.conf test is successful
/application/nginx/sbin/nginx -s reload #平滑重启Nginx
测试访问:
curl http://www.smartbro.com Welcome to www.smartbro.com!
curl http://www.smartbro.com:8080 Welcome to bbs.smartbro.com
curl http://www.smartbro.com:8090 Welcome to pan.smartbro.com
------------------------------------------------------------------------------------------------
配置基于IP的虚拟主机实战:
------------------------------------------------------------------------------------------------
(1)在服务器增加多个网卡:
poweroff #关机
单击[编辑虚拟机设置]--->[添加]--->[网络适配器]--->[下一步]--->[自定义]--->[VMnet2(仅主机模式)]--->[完成]
单击[编辑虚拟机设置]--->[添加]--->[网络适配器]--->[下一步]--->[自定义]--->[VMnet3(仅主机模式)]--->[完成]
开启虚拟机
现在总共有3张网卡:
eth0:192.168.10.3
eth1:192.168.20.3
eth2:192.168.30.3
rm -rf /etc/udev/rules.d/* #删除udev规则
vim /etc/sysconfig/network-scripts/ifcfg-eth0 #修改网卡配置
DEVICE=eth0
HWADDR=00:0C:29:1C:30:C8
TYPE=Ethernet
ONBOOT=yes
BOOTPROTO=none
IPADDR=192.168.10.3
PREFIX=24
:wq #保存退出
cp /etc/sysconfig/network-scripts/ifcfg-eth0 /etc/sysconfig/network-scripts/ifcfg-eth1 #复制并改名网卡配置文件
cp /etc/sysconfig/network-scripts/ifcfg-eth0 /etc/sysconfig/network-scripts/ifcfg-eth2
vim /etc/sysconfig/network-scripts/ifcfg-eth1 #修改网卡地址
DEVICE=eth1 #更改网卡的名字
HWADDR=00:0C:29:1C:30:D2 #更改网卡的MAC地址
TYPE=Ethernet
ONBOOT=yes
BOOTPROTO=none
IPADDR=192.168.20.3 #更改网卡的IP地址
PREFIX=24
:wq #保存退出
vim /etc/sysconfig/network-scripts/ifcfg-eth1 #修改网卡地址
DEVICE=eth2 #更改网卡的名字
HWADDR=00:0C:29:1C:30:DC #更改网卡的MAC地址
TYPE=Ethernet
ONBOOT=yes
BOOTPROTO=none
IPADDR=192.168.30.3 #更改网卡的IP地址
PREFIX=24
:wq #保存退出
reboot #重启
ifconfig -a #查看所有网卡信息
eth0 Link encap:Ethernet HWaddr 00:0C:29:1C:30:C8
inet addr:192.168.10.3 Bcast:192.168.10.255 Mask:255.255.255.0 #网卡的IP地址已经生效
inet6 addr: fe80::20c:29ff:fe1c:30c8/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:51 errors:0 dropped:0 overruns:0 frame:0
TX packets:46 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:5998 (5.8 KiB) TX bytes:6909 (6.7 KiB)

eth1 Link encap:Ethernet HWaddr 00:0C:29:1C:30:D2
inet addr:192.168.20.3 Bcast:192.168.20.255 Mask:255.255.255.0 #网卡的IP地址已经生效
inet6 addr: fe80::20c:29ff:fe1c:30d2/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:3 errors:0 dropped:0 overruns:0 frame:0
TX packets:12 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:276 (276.0 b) TX bytes:828 (828.0 b)

eth2 Link encap:Ethernet HWaddr 00:0C:29:1C:30:DC
inet addr:192.168.30.3 Bcast:192.168.30.255 Mask:255.255.255.0 #网卡的IP地址已经生效
inet6 addr: fe80::20c:29ff:fe1c:30dc/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:3 errors:0 dropped:0 overruns:0 frame:0
TX packets:12 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:276 (276.0 b) TX bytes:828 (828.0 b)

lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:228 errors:0 dropped:0 overruns:0 frame:0
TX packets:228 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:14900 (14.5 KiB) TX bytes:14900 (14.5 KiB)
修改Nginx的主配置文件:
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 192.168.30.3; #设置监听的IP地址,可以后面加上设定的端口号,例如192.168.10.3:80
server_name www.smartbro.com;
location / {
root html/www;
index index.html index.htm;
}
}

server {
listen 192.168.20.3; #设置监听IP地址
server_name bbs.smartbro.com;
location / {
root html/bbs;
index index.html index.htm;
}
}

server {
listen 192.168.10.3; #设置监听IP地址
server_name pan.smartbro.com;
location / {
root html/pan;
index index.html index.htm;
}
}

}
/application/nginx/sbin/nginx -t
nginx: the configuration file /application/nginx-1.13.4//conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.13.4//conf/nginx.conf test is successful
启动Nginx:
/application/nginx/sbin/nginx
curl http://192.168.10.3 Welcome to pan.smartbro.com
curl http://192.168.20.3 Welcome to bbs.smartbro.com
curl http://192.168.30.3 Welcome to www.smartbro.com!
----------------------------------------------------------------------------------------
企业场景中重启Nginx后的检测策略:
----------------------------------------------------------------------------------------
在企业运维实战场景中,每一个配置操作处理完毕后都应该进行快速有效的检查。
启动Nginx的时候会通过脚本获取header信息或模拟用户访问指定的URL来自动检查Nginx的启动是否正常。
最大限度的保证服务重启后可以迅速访问网站。
如果有问题就立刻使用上一版的备份文件,使得影响用户的时间最短。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Web服务器 Nginx