Nginx技术深度剖析(2)

Wesley13
• 阅读 367

Nginx虚拟主机配置实战:

虚拟主机的概念:

所谓虚拟主机就是在Web主机里的一个独立的网站站点,这个站点对应独立的域名(也可能是IP地址或端口),具有独立的程序及资源目录,可以独立的对外提供服务供用户访问。

这个独立的站点在配置里是由一定格式的标签段标记,对于Apache软件来说,一个独立的虚拟主机的标签段通常包含在内,而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的启动是否正常。

最大限度的保证服务重启后可以迅速访问网站。

如果有问题就立刻使用上一版的备份文件,使得影响用户的时间最短。

本文出自 “帅帅的小哥哥” 博客,请务必保留此出处http://xvjunjie.blog.51cto.com/12360960/1955228

点赞
收藏
评论区
推荐文章
blmius blmius
2年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Jacquelyn38 Jacquelyn38
2年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
美味蟹黄堡 美味蟹黄堡
1年前
VPS作用都有啥?
国内VPS服务器我们可以将它用在哪些地方,首先我们就要知道作为VPS主机,它所拥有哪些功能。今天就做一个简单的使用介绍,采用的环境是3A网络的VPS服务器,性价比较高。每个VPS服务器都可分配独立公网IP地址、独立操作系统、独立超大空间、独立内存、独立CPU资源、独立执行程序和独立系统配置等。VPS用户除了可以分配多个虚拟主机及无限企业邮箱外,更具有独立服
Stella981 Stella981
2年前
KVM调整cpu和内存
一.修改kvm虚拟机的配置1、virsheditcentos7找到“memory”和“vcpu”标签,将<namecentos7</name<uuid2220a6d1a36a4fbb8523e078b3dfe795</uuid
Stella981 Stella981
2年前
Nginx + lua +[memcached,redis]
精品案例1、Nginxluamemcached,redis实现网站灰度发布2、分库分表/基于Leaf组件实现的全球唯一ID(非UUID)3、Redis独立数据监控,实现订单超时操作/MQ死信操作SelectPollEpollReactor模型4、分布式任务调试Quartz应用
Wesley13 Wesley13
2年前
nginx配置虚拟主机相关教程
1.配置虚拟主机配置虚拟主机即:在一台服务器上启动多个网站;区分不同的网站方式:域名不同,端口不同;1.1通过端口区分虚拟主机1.1.1server节点的介绍nginx的配置文件路径:/usr/local/nginx/conf/nginx.conf一个se
Wesley13 Wesley13
2年前
Java多线程导致的的一个事物性问题
业务场景我们现在有一个类似于文件上传的功能,各个子站点接受业务,业务上传文件,各个子站点的文件需要提交到总站点保存,文件是按批次提交到总站点的,也就是说,一个批次下面约有几百个文件。      考虑到白天提交这么多文件会影响到子站点其他系统带宽,我们将分站点的文件提交到总站点这个操作过程独立出来,放到晚上来做,具体时间是晚上7:00到早上7:00。
美味蟹黄堡 美味蟹黄堡
1年前
VPS原理探究
1.VPS的原理 VPS是采用VPS(VirtualPrivateServer)技术,将一部物理服务器分割成多个虚拟专享服务器。分割后形成的VPS虚拟主机都可分配独立公网IP地址、独立操作系统Windows/Linux、独立享用空间、独立内存、独立的CPU资源、独立执行程序和独立系统配置等。用户可自行安装程序,单独重启、关闭服务器。2.VPS虚拟主机的特点
Python进阶者 Python进阶者
3个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这