一、Nginx 配置安装
1. 下载文件
wget http://nginx.org/download/nginx-1.8.0.tar.gz
2. 解压
tar -zxvf nginx-1.8.0.tar.gz
3. 安装依赖
yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel
4. 编译
cd nginx-1.8.0/
./configure
make & make install
5. 启动
cd /usr/local/nginx/sbin/
./nginx
6. 关闭&重启
./nginx -s stop #终止nginx
./nginx -s reload #重新加载配置文件
二、Nginx 反向代理
1. 单台服务反向代理
server{
listen 9003;
server_name localhost;
location / {
proxy_pass http://127.0.0.1:8080/;
}
}
2. 多态服务反向代理
server{
listen 9003;
server_name localhost;
location client {
proxy_pass http://127.0.0.1:8081/;
}
location admin {
proxy_pass http://127.0.0.1:8082/;
}
}
nginx location语法
在nginx配置⽂件中,location主要有这⼏种形式:
1)正则匹配 location ~ /lagou { }
2)不区分⼤⼩写的正则匹配 location ~* /lagou { }
3)匹配路径的前缀 location ^~ /lagou { }
4)精确匹配 location = /lagou { }
5)普通路径前缀匹配 location /lagou { }
优先级
4 > 3 > 2 > 1 > 5
三、负载均衡
负载均衡策略
- 轮询
每个机器按顺序分配。
# 注意:upstream节点与 server同级
upstream hetongServer{
server 111.111.111.111:8080;
server 111.111.111.111:8082;
}
#注意:尾部的/不能丢
location /abc/ {
proxy_pass http://lagouServer/;
}
- 权重
weight 代表权重,默认为1 权重越高分配的请求越多(用于服务器性能不统一情况)
# 注意:upstream节点与 server同级
upstream hetongServer{
server 111.111.111.111:8080 weight=1;
server 111.111.111.111:8082 weight=2;
}
- ip_bash (ip哈希)
按照请求机器的ip的hash结果分配,每一个客户端的请求分配给固定的目标服务器,解决tomcat集群下 session问题。
# 注意:upstream节点与 server同级
upstream hetongServer{
ip_hash;
server 111.111.111.111:8080;
server 111.111.111.111:8082;
}
- 最少连接数
upstream hetongServer{
least_conn;
server 111.111.111.111:8080;
server 111.111.111.111:8082;
}
四、动静分离
- 创建静态资源存放目录
# 在/usr/local/nginx 目录下创建staticData/static目录,用于存放静态文件
# 在/usr/local/nginx/staticData/static 目录下存放静态资源文件
[root@10-13-52-50 nginx] pwd
/usr/local/nginx
[root@10-13-52-50 nginx] ll
total 4
drwx------. 2 nobody root 6 Mar 3 22:58 client_body_temp
drwxr-xr-x. 2 root root 4096 Mar 4 22:33 conf
drwx------. 2 nobody root 6 Mar 3 22:58 fastcgi_temp
drwxr-xr-x. 2 root root 40 Mar 3 22:52 html
drwxr-xr-x. 2 root root 58 Mar 3 22:58 logs
drwx------. 2 nobody root 6 Mar 3 22:58 proxy_temp
drwxr-xr-x. 2 root root 19 Mar 4 00:46 sbin
drwx------. 2 nobody root 6 Mar 3 22:58 scgi_temp
drwxr-xr-x. 3 root root 20 Mar 4 22:28 staticData
drwx------. 2 nobody root 6 Mar 3 22:58 uwsgi_temp
- nginx.conf 配置
# server节点下 配置location 转发节点
location /static/ {
root staticData;
}
五、健康检查机制
Nginx 的健康检测分为两种类型,主动检测和被动检测,默认的非商用 Nginx 采用的是被动检测。
所谓的被动检测是指只有访问了该服务器之后发现服务器不可用了,才会将其标识为不可用,并且在一定时间内禁止请求分发到该服务器上,而不是主动以一定的频率去检查服务器是否可用。
被动检查机制
健康检测有两个重要参数 max_fails 和 fail_timeout
- fail_timeout 定义了健康检查的执行时长
- max_fails 表示服务不可用的最大尝试次数
fail_timeout 定义了健康检查的执行时长,而 max_fails 表示服务不可用的最大尝试次数,当一定时间内(此时间由 fail_timeout 定义),发生了一定次数的服务器不响应的事件(此次数由 max_fails 定义),那么 Nginx 就会将该服务器标识为不可用的服务器,并且在一定时间内禁止请求分发到该服务器。默认情况下 max_fails 设置为 1,当它设置为 0 时表示禁用此服务器的运行状况检查
upstream cluster{
# 如果 10s 内发生了两次服务不可用的情况就会将该服务器标识为不可用的状态
server srv1.example.com max_fails=2 fail_timeout=10s;
server srv2.example.com max_fails=2 fail_timeout=10s;
}
主动检查机制
主动检查方案有两种
- 商用Nginx Plus
- nginx_upstream_check_module模块
Nginx Plus 和 nginx_upstream_check_module 模块的主动健康检查配置大体都是一样的
upstream backend {
server srv1.example.com;
server srv2.example.com;
check interval=3000 rise=1 fall=3 timeout=2000 type=http;
#表示发送请求的内容
check_http_send "HEAD /status HTTP/1.0\r\n\r\n";
#服务器正常情况下的响应状态码,如果后端服务器的响应状态包含在此配置中,则说明是健康的状态
check_http_expect_alive http_2xx http_3xx;
}
六、缓存
我们可以开启 Nginx 的静态资源缓存,将一些不变的静态文件,比如图片、CSS、JS 等文件进行缓存,这样在客户端访问这些资源时就不用去访问服务器了,因此响应的速度就可以大幅提升,并且节省了宝贵的服务器资源。
Nginx 开启缓存需要在 http 节点中配置 proxy_cache_path 信息,以及 server 节点中配置要缓存资源的后缀名
http {
#缓存的目录信息,以及缓存的保存时间 inactive,还有缓存的大小等信息
proxy_cache_path /data/cache levels=1:2 keys_zone=nuget-cache:20m max_size=50g inactive=1d;
include nginx_proxy.conf;
server {
listen 80;
server_name srv1.example.com;
# 要缓存的文件的后缀
location ~ .*\.(gif|jpg|png|css|js)(.*) {
#关闭日志功能
access_log off;
add_header Cache-Control "public,max-age=24*3600";
#第一次没有缓存时的请求地址
proxy_pass http://localhost:8080;
}
}
}