server 192.168.78.200 backup; #预留的备份服务器
server 192.168.78.33 down; #当前服务器不参与负载均衡}
ip_hash 配置
upstream web_server{
ip_hash;
server 192.168.78.120;
server 192.168.78.123;
server 192.168.78.33 down; #如果这台服务器宕机,则用down表示当前服务器暂不参与负载均衡}
使用 ip_hash 的时候,不能使用 weight 和 backup。
第三方模块(fair)
- 备份已安装的 Nginx
由于使用第三方模块,需要重新编译 Nginx,所以需要关闭已经开启的 Nginx 进程,对已经安装好的 Nginx 进行备份,便于恢复。
cp -r /usr/local/nginx /usr/local/nginx_old
- 重新编译安装 Nginx
先从 github 获取 fair 模块,下载到 zip 到 root 目录下,解压。
unzip nginx-upstream-fair-master.zip #解压mv nginx-upstream-fair-master nginx-upstream-fair #重命名
如果没有 unzip 命令,则需要用 yum install unzip 安装。
cd nginx-1.10.1#配置./configure
–prefix=/usr/local/nginx
–with-http_ssl_module
–add-module=/root/nginx-upstream-fair#编译安装make && make install
- 配置 fair 的负载均衡
打开新安装的 Nginx 配置文件,在 http 块下实现 fair 的负载均衡。
server{
listen 80;
server_name test.test;
location / {
proxy_pass http://web_server }}upstream web_server{
server 192.168.78.128;
server 192.168.78.132;
fair;
【一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义】 浏览器打开:qq.cn.hn/FTf 免费领取
#使用fair}
缓存配置
对于一些含有大量内容的网站来说,随着访问量的增多,对于经常被访问的内容,如果每一次都从服务器中获取,则给服务器很大的压力。所以我们可以利用反向代理服务器对访问频率较多的内容进行缓存,有利于节省后端服务器的资源。
原理
web 缓存服务器位于内容源 web 服务器和客户端之间,当客户端访问一个 url 时,缓存服务器请求内容源服务器,并将响应结果缓存到内存或硬盘,当下一次请求同一个 url 时,缓存服务器直接将已缓存的内容输出给客户端,这样就减少了再次向内容源服务器请求的次数。
永久缓存配置(proxy_store)
Nginx 提供了 proxy_store 指令用于缓存内容服务器响应到本地,若不手动删除,则一直存在。
server{
listen 80;
server_name test.test;
location / {
root cache; #制定个缓存文件的保存目录
proxy_store on; #开启本地缓存
proxy_store_access user:rw group:rw all:r; #设置缓存的读写规则
proxy_temp_path cache_tmp; #设置反向代理时接收的数据临时存储文件的目录,该目录会自动创建
#利用正则匹配缓存文件、目录或符号链接是否存在,如果不存在再执行块语句
if(!-e $request_filename){
proxy_pass http://192.168.78.128;
}
}}
临时缓存(proxy_cache)
Nginx 服务器提供了 proxy_cache 指令设置临时缓存。采用 md5 算法将请求链接进行 hash 后,根据具体配置生成缓存文件目录,保存响应数据。
在缓存服务器上配置 nginx.conf 中的 **http** 块。
#代理临时目录proxy_temp_path /usr/local/nginx/proxy_temp_dir; #设置缓存服务器接收内容服务器响应内容使用的临时目录#web缓存目录和参数设置proxy_cache_path /usr/local/nginx/proxy_cache_dir levels=1:2 keys_zone=cache_one:50minactive=1m max_size=500m; #自定义保存目录,
配置 server 块:
server {
listen 80;
server_name test.test;
#增加两个响应头,用于获取访问的服务器地址与缓存是否成功
add_header X-Via $server_addr;
add_header X-Cache $upstream_cache_status;
location /{
proxy_cache cache_one; #设置缓存区域名称
proxy_cache_key h o s t host hosturi i s _ a r g s is_args is_argsargs; #以域名、uri、参数组合成web缓存的key,nginx根据key值哈希
proxy_cache_valid 200 10m; #对200状态码设置缓存时间
proxy_cache_valid 304 1m;
proxy_cache_valid any 1m; #其他未设置的缓存1分钟
proxy_pass http://192.168.78.128;
}}
缓存清理配置
因为 Nginx 不支持清理制定 url 的缓存,需要借助第三方模块来实现。例如 ngx_cache_purge
- 备份已安装的 Nginx
在添加 ngx_cache_purge 模块之前,关闭 Nginx 服务,备份已有的 Nginx 服务。
cp -r /usr/local/nginx /usr/local/nginx_old2
- 重新编译安装 Nginx
在 github 获取 ngx_cache_purge zip 包,并解压安装。
unzip ngx_cache_purge-master.zipmv ngx_cache_purge-master /usr/local/ngx_cache_purge#进入ngxin文件的解压目录配置一下cd nginx-1.10.1
./configure
–prefix=/usr/local/nginx
–with-http_ssl_module
–add-module=/usr/local/ngx_cache_purge#编译和安装make && make install
- 配置缓存清理功能
使用 proxy_cache_purge 指令实现缓存清理。
注意:
-
指定的缓存区名称和 proxy_cache_purge 指令中出现的缓存区名称一致。
-
key 值设置规则要一致。
-
清理缓存的 location 的编写位置在所有 location 之前,防止其他正则 location 提前匹配。



