1. 服务器规划
ip | 角色 | 系统版本 |
---|---|---|
192.168.1.19 | client | centos7 |
192.168.1.22 | nginx缓存服务器 | centos7 |
192.168.1.10 | webserver1 | |
192.168.1.18 | webserver2 |
2. nginx安装
- 使用 nginx-sticky-module 扩展模块实现 Cookie 会话黏贴
- 使用 ngx_cache_purge 实现更强大的缓存清除
nginx-goodies-nginx-sticky-module-ng-08a395c66e42.zip 点此下载 提取码:mubw
ngx_cache_purge-2.3.tar.gz 点此下载 提取码:hqy6
13 groupadd nginx
14 useradd nginx -g nginx -s /sbin/nologin
15 tar -zxf ngx_cache_purge-2.3.tar.gz
16 unzip nginx-goodies-nginx-sticky-module-ng-08a395c66e42.zip
22 tar -zxf nginx-1.15.4.tar.gz
24 cd nginx-1.15.4/
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-http_realip_module --with-http_ssl_module --with-http_gzip_static_module --http-client-body-temp-path=/var/tmp/nginx/client --http-proxy-temp-path=/var/tmp/nginx/proxy --http-fastcgi-temp-path=/var/tmp/nginx/fcgi --with-pcre --add-module=../ngx_cache_purge-2.3 --add-module=../nginx-goodies-nginx-sticky-module-ng-08a395c66e42
make && make install
ln -s /usr/local/nginx/sbin/nginx /usr/sbin/
注:如果你想在已安装好的 nginx 上添加第三方模块,依然需要重新编译,但为了不覆盖你
原有的配置,请不要 make install,而是直接拷贝可执行文件
cp objs/nginx /usr/local/nginx/sbin/nginx
3. 配置nginx反向代理:反向代理+健康检查+负载均衡
查看已加载的模块
[root@localhost nginx-1.15.4]# nginx -V
nginx version: nginx/1.15.4
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-http_realip_module --with-http_ssl_module --with-http_gzip_static_module --http-client-body-temp-path=/var/tmp/nginx/client --http-proxy-temp-path=/var/tmp/nginx/proxy --http-fastcgi-temp-path=/var/tmp/nginx/fcgi --with-pcre --add-module=../ngx_cache_purge-2.3 --with-http_flv_module --add-module=../nginx-goodies-nginx-sticky-module-ng-08a395c66e42
3.1 nginx-sticky-module 模块
这个模块的作用是通过 cookie 黏贴的方式将来自同一个客户端(浏览器)的请求发送到同
一个后端服务器上处理,这样一定程度上可以解决多个 backend servers 的 session 同步的问
题 —— 因为不再需要同步,而 RR 轮询模式必须要运维人员自己考虑 session 同步的实现。
另外内置的 ip_hash 也可以实现根据客户端 IP 来分发请求,但它很容易造成负载不均衡的
情况,而如果 nginx 前面有 CDN 网络或者来自同一局域网的访问,它接收的客户端 IP 是一
样的,容易造成负载不均衡现象。nginx-sticky-module 的 cookie 过期时间,默认浏览器关闭
就过期。
这个模块并不合适不支持 Cookie 或手动禁用了 cookie 的浏览器,此时默认 sticky 就会切换
成 RR。它不能与 ip_hash 同时使用。
upstream backend {
server 192.168.31.141:80 weight=1;
server 192.168.31.250:80 weight=1;
sticky;
}
配置起来超级简单,一般来说一个 sticky 指令就够了。
3.2 负载均衡与健康检查
严格来说,nginx 自带是没有针对负载均衡后端节点的健康检查的,但是可以通过默认自带
的 ngx_http_proxy_module 模块和 ngx_http_upstream_module 模块中的相关指令来完成
当后端节点出现故障时,自动切换到下一个节点来提供访问。
upstream backend {
sticky;
server 192.168.31.141:80 weight=1 max_fails=2 fail_timeout=10s;
server 192.168.31.250:80 weight=1 max_fails=2 fail_timeout=10s;
}
server {
……
location / {
proxy_pass http://backend;
}
……
}
参数说明:
- weight : 轮询权值也是可以用在 ip_hash 的,默认值为 1
- max_fails :允许请求失败的次数,默认为 1。当超过最大次数时,返回 proxy_next_upstream
模块定义的错误。 - fail_timeout : 有两层含义,一是在 10s 时间内最多容许 2 次失败;二是在经历了 2 次
失败以后,10s 时间内不分配请求到这台服务器
3.3 nginx配置实例
下面的 nginx.conf 实现 nginx 在前端做反向代理服务器的完整配置文件的例子,处理 js、png
等静态文件,jsp/php 等动态请求转发到其它服务器 tomcat/apache
user www www;
worker_processes 4;
worker_cpu_affinity 0001 0010 0100 1000;
error_log logs/error.log;
worker_rlimit_nofile 10240;
pid logs/nginx.pid;
events {
use epoll;
worker_connections 4096;
}
http {
include mime.types;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"'
'"$upstream_cache_status"';
access_log logs/access.log main;
server_tokens off;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#压缩设置
gzip on;
gzip_comp_level 6;
gzip_http_version 1.1;
gzip_proxied any;
gzip_min_length 1k;
gzip_buffers 16 8k;
gzip_types text/plain text/css text/javascript application/json application/javascript application/x-javascript application/xml;
gzip_vary on;
# http_proxy Settings
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 75;
proxy_send_timeout 75;
proxy_read_timeout 75;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
proxy_buffering on;
proxy_temp_path /usr/local/nginx/proxy_temp;
proxy_cache_path /usr/local/nginx/proxy_cache levels=1:2 keys_zone=my-cache:100m inactive=600m max_size=2g;
#负载均衡模块
upstream backend {
sticky;
server 192.168.31.141:80 weight=1 max_fails=2 fail_timeout=10s;
server 192.168.31.250:80 weight=1 max_fails=2 fail_timeout=10s;
}
#virtual host Settings
server {
listen 80;
server_name localhost;
charset utf-8;
location ~/purge(/.*) {
allow 127.0.0.1;
allow 192.168.31.0/24;
deny all;
proxy_cache_purge my-cache $host$1$is_args$args;
}
location / {
index index.php index.html index.htm;
proxy_pass http://backend;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
}
location ~.*\.(gif|jpg|png|html|htm|css|js|ico|swf|pdf)$ {
proxy_pass http://backend;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503
http_504;
proxy_cache my-cache;
add_header Nginx-Cache $upstream_cache_status;
proxy_cache_valid 200 304 301 302 8h;
proxy_cache_valid 404 1m;
proxy_cache_valid any 1d;
proxy_cache_key $host$uri$is_args$args;
expires 30d;
}
location /nginx_status {
stub_status on;
access_log off;
allow 192.168.31.0/24;
deny all;
}
}
}
4. 验证:nginx 反向代理的缓存功能
如果在缓存时间之内需要更新被缓存的静态文件怎么办呢,这时候就需要手动来清除缓存了。
ngx_cache_pure 清除缓存模块使用说明
用谷歌浏览器测试的时候,可以按 F12 调用开发工具,选择 Network 选项,我们可以看到,
Response Headers,在这里我们可以看到,我们请求的是否是缓存
从图中我们可以看到,我们访问的服务器是 192.168.31.83,缓存命中。
也可以查看缓存目录或 nginx 的访问日志
清除缓存:
上 述 配 置 的 proxy_cache_purge 指令用于方便的清除缓存,但必须按 照 第 三 方 的
ngx_cache_purge 模块才能使用
使用 ngx_cache_purge 模块清除缓存(直接删除缓存目录下的文件也算一种办法):
GET 方式请求 URL
即使用配置文件中的 location ~ /purge(/.*)
浏览器访问 http://192.168.1.22/purge/apache_pb2.gif 来清除缓存
缓存清除成功。
备注:
(1)purge 是 ngx_cache_pure 模块指令
(2)your/may/path 是要清除的缓存文件 URL 路径