目录
前言-Nginx概述
Nginx是一个高性能的HTTP和反向代理web服务器,其特点是占有内存少,并发能力强,事实上nginx的并发能力在同类型的网页服务器中表现较好,中国大陆使用nginx网站用户有:百度、京东、新浪、网易、腾讯、淘宝等。
Nginx专门为性能优化而开发,性能是其最重要的考量,实现上非常注重效率,能经受高负载的考研,有报告表明能支持高达50000个并发连接数。
Nginx安装
实验环境:
- 虚拟机:VMware
- 操作系统:CentOS 7.4
- Nginx开源版下载地址:http://nginx.org/
一、解压
以下使用的nginx版本为nginx-1.21.6,将下载的nginx压缩包使用Xftp上传至linux的自定义目录中。
使用解压命令解压文件:
tar -xzvf nginx-1.21.tar.gz
二、安装
2.1编译安装
进入解压文件中,执行编译安装命令。
./configure --prefix=/usr/local/nginx
make
make install
2.2如果出现警告或报错
提示内容如下:
checking for OS
+ Linux 3.10.0-693.el7.x86_64 x86_64
checking for C compiler ... not found
./configure: error: C compiler cc is not found
安装gcc:
yum install -y gcc
提示内容如下:
./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.
安装 perl:
yum install -y pcre pcre-devel
提示内容如下:
./configure: error: the HTTP gzip module requires the zlib library.
You can either disable the module by using --without-http_gzip_module
option, or install the zlib library into the system, or build the zlib library
statically from the source with nginx by using --with-zlib=<path> option.
安装zlib:
yum install -y zlib zlib-devel
没有错误之后,再次尝试编译安装。
make & make install
三、启动Nginx
进入安装好的目录 /usr/local/nginx/sbin
./nginx 启动
./nginx -s stop 快速停止
./nginx -s quit 优雅关闭,在退出前完成已经接受的连接请求
./nginx -s reload 重新加载配置
四、关于防火墙
#关闭防火墙:
systemctl stop firewalld.service
#禁止防火墙开机启动
systemctl disable firewalld.service
#放行端口
firewall-cmd --zone=public --add-port=80/tcp --permanent
#重启防火墙
firewall-cmd --reload
五、配置Nginx系统服务
创建service脚本:
vi /usr/lib/systemd/system/nginx.service
编辑脚本内容:
[Unit]
Description=nginx - web server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
# 其中/usr/local/nginx为nginx的编译安装目录,如果采用yum安装,则无序配置该文件
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
ExecQuit=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
编辑完成后退出文件。
# 重新加载系统服务
systemctl daemon-reload
# 启动服务
systemctl start nginx.service
# 开机启动
systemctl enable nginx.service
Nginx基本使用
一、目录结构
进入Nginx的主目录我们可以看到这些文件夹:
client_body_temp conf fastcgi_temp html logs proxy_temp sbin scgi_temp uwsgi_temp
其中这几个文件夹在刚安装后是没有的,主要用来存放运行过程中的临时文件:
client_body_temp fastcgi_temp proxy_temp scgi_temp
主要目录及其功能如下:
conf # 用来存放配置文件相关
html #用来存放静态文件的默认目录 html、css等
sbin #nginx的主程序
二、基本运行原理
客户端通过网络请求向nginx服务器发送请求,nginx服务程序运行时会启动一个Master(主)进程,该进程读取并校验/conf/nginx.conf中的配置文件。同时Master进程会创建多个Worker进程接收响应请求,根据配置文件中的策略来选择反向代理或负载均衡。
三、Nginx配置与应用场景
nginx.conf文件中部分参数的含义:
worker_processes 1; #默认为1,表示开启一个业务进程
worker_connections 1024; #单个业务进程可接受连接数
include mime.types; #引入http mime类型
default_type application/octet-stream; #如果mime类型没匹配上,默认使用二进制流的方式传输。
sendfile on; #使用linux的 sendfile(socket, file, len) 高效网络传输,也就是数据0拷贝。
3.1虚拟主机
原本一台服务器只能对应一个站点,通过虚拟主机技术可以虚拟化成多个站点同时对外提供服务。
虚拟主机配置:
server {
listen 80; #监听端口号
server_name localhost; #主机名
location / {
#匹配路径
root html; #文件根目录
index index.html index.htm; #默认页名称
}
error_page 500 502 503 504 /50x.html; #报错编码对应页面
location = /50x.html {
root html;
}
}
3.2server_name匹配规则
我们需要注意的是servername匹配分先后顺序,写在前面的匹配上就不会继续往下匹配了。
#完整匹配:我们可以在同一servername中匹配多个域名
server_name vod.mmban.com www1.mmban.com;
#通配符匹配
server_name *.mmban.com
#通配符结束匹配
server_name vod.*;
#正则匹配
server_name ~^[0-9]+\.mmban\.com$;
四、反向代理
通过proxy_pass实现反向代理:
# 方式一:
location / {
#proxy_pass http:代理的主机ip地址;
proxy_pass http:192.168.101.102:80;
}
# 方式二:
upstream httpd {
server 192.168.44.102:80;
server 192.168.43.103:80;
}
location / {
proxy_pass http:httpd;
}
五、基于反向代理的负载均衡
5.1负载均衡策略
轮询:默认情况下使用轮询方式,逐一转发,这种方式适用于无状态请求。
weight(权重):指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。
upstream httpd {
server 127.0.0.1:8050 weight=10 down;
server 127.0.0.1:8060 weight=1;
server 127.0.0.1:8060 weight=1 backup;
}
#down:表示当前的server暂时不参与负载
#weight:默认为1.weight越大,负载的权重就越大。
#backup: 其它所有的非backup机器在down或者忙的时候,请求backup机器。
其他参数如下:
ip_hash #根据客户端的ip地址转发同一台服务器,可以保持回话。
least_conn #最少连接访问
url_hash #根据用户访问的url定向转发请求
fair #根据后端服务器响应时间转发请求
六、Location
location的作用是根据请求路径匹配对应的资源或处理策略。
location的匹配规则如下:
/ 通用匹配,任何请求都会匹配到。
= 精准匹配,不是以指定模式开头
~ 正则匹配,区分大小写
~* 正则匹配,不区分大小写
^~ 非正则匹配,匹配以指定模式开头的location
location匹配顺序:
- 多个正则location直接按书写顺序匹配,成功后就不会继续往后面匹配
- 普通(非正则)location会一直往下,直到找到匹配度最高的(最大前缀匹配)
- 当普通location与正则location同时存在,如果正则匹配成功,则不会再执行普通匹配
- 所有类型location存在时,“=”匹配 > “^~”匹配 > 正则匹配 > 普通(最大前缀匹配)
location ~*/(css|img|js) {
root /usr/local/nginx/static;
index index.html index.htm;
}
七、UrlRewrite
使用场景:希望隐藏后端接口及其参数。
rewrite是实现URL重写的关键指令,根据regex (正则表达式)部分内容,重定向到replacement,结尾是flag标记。
rewrite <regex> <replacement> [flag];
关键字 正则 替代内容 flag标记
#正则:perl兼容正则表达式语句进行规则匹配
#替代内容:将正则匹配的内容替换成replacement
#flag标记:rewrite支持的flag标记
#flag标记说明:
last #本条规则匹配完成后,继续向下匹配新的location URI规则
break #本条规则匹配完成即终止,不再匹配后面的任何规则
redirect #返回302临时重定向,浏览器地址会显示跳转后的URL地址
permanent #返回301永久重定向,浏览器地址栏会显示跳转后的URL地址
示例:
#原先请求地址: http://localhost/index.jsp?pageNum=1
#希望隐藏后端资源的地址和pageNum参数,使用rewrite命令重写url
rewrite ^/([0-9]+).html$ /index.jsp?pageNum=$1 break;
通过http://localhost/1.html地址向nginx服务器发送请求,正则表达式匹配后,将该地址转为http://localhost/index.jsp?pageNum=1,可以实现同样的效果
八、防盗链配置
使用场景:防止第三方引用服务器中的资源。
当用户向Nginx服务器发送页面请求时,返回的html文件中往往会内嵌一些静态资源需要在此访问后端服务器。此时这些资源的请求头中根据http协议需要添加Referer:http://服务器ip地址/xxx.html字段用以表示该请求最初来源于哪个服务器,而第三方引用的请求头中不包含该字段。可以通过在Nginx服务器中配置校验该字段来实现防盗链功能。
valid_referers none | blocked | server_names | strings ....;
- none, 检测 Referer 头域不存在的情况。
- blocked,检测 Referer 头域的值被防火墙或者代理服务器删除或伪装的情况。这种情况该头域的值不以“http://” 或 “https://” 开头。
- server_names ,设置一个或多个 URL ,检测 Referer 头域的值是否是这些 URL 中的某一个。
在需要防盗链的location中配置:
valid_referers 192.168.44.101; # 该地址为html中静态资源请求中Referer中出现的ip地址。
if ($invalid_referer) {
return 403;
}
九、高可用配置
在集群的开发环境中,往往会配置多台Nginx服务器,为了服务的高可用性,需要确保当某台Nginx服务器宕机时其他Nginx服务器能够继续运行。可以使用Keepalived服务,对Nginx服务器之间进行主从配置,当主Nginx服务器宕机,从Nginx服务器接力运行。
9.1 安装Keepalived
下载地址:
https://www.keepalived.org/download.html#
使用 ./configure 编译安装,如果遇见如下报错:
configure: error:
!!! OpenSSL is not properly installed on your system. !!!
!!! Can not include OpenSSL headers files. !!!
安装依赖
yum install openssl-devel
yum方式安装:
yum install keepalived
9.2 配置
使用yum安装后配置文件在
/etc/keepalived/keepalived.conf
9.3最小配置
主Nginx服务器配置:
global_defs {
router_id lb111 #名称
}
vrrp_instance atguigu {
state MASTER #主从配置
interface ens33 #网卡信息
virtual_router_id 51
priority 100
advert_int 1
authentication {
# 认证信息:同一组Nginx服务器信息一致
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
# 两台Nginx服务器共用一个虚拟ip地址
192.168.44.200
}
}
从Nginx服务器配置:
global_defs {
router_id lb110
}
vrrp_instance atguigu {
state BACKUP
interface ens33
virtual_router_id 51
priority 50
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.44.200
}
}
启动服务
systemctl start keepalived