您的位置:首页 > 运维架构 > 反向代理

nginx实战(四)反向代理配置缓存及负载均衡

2018-10-19 13:45 1271 查看

前言

反向代理,是指用户通过同一服务器访问服务器后端各被代理服务器的访问方式。(详见百度百科 https://baike.baidu.com/item/反向代理/7793488
 

nginx 反向代理的应用场景

1、实现外网用户对内网服务器的访问,相对保护内网服务器安全及节约网络资源
2、增加缓存,缓解对内网服务器访问的压力
3、通过负载均衡提高业务处理能力及高可用性
 

访问内网配置

### 代理参数,设置头信息
cat>conf/conf.d/proxy.conf<<EOF
proxy_set_header   Host              $host:$server_port;
proxy_set_header   Referer           $http_referer;
proxy_set_header   Cookie            $http_cookie;
proxy_set_header   X-Real-IP         $remote_addr;
proxy_set_header   X-Forwarded-For   $proxy_add_x_forwarded_for;
proxy_set_header   X-Forwarded-Proto $scheme;
### websocket 支持
#proxy_http_version 1.1;
#proxy_set_header Upgrade $http_upgrade;
#proxy_set_header Connection "upgrade";
EOF

## 添加到nginx.conf 中
sed -r -i "/include conf./i\    include proxy.conf'" nginx.conf

cat > conf/conf.d/8081.conf<<EOF
upstream tomcatserver1 {
server 172.0.0.49: 8081;
}
server {
listen       80;
server_name  8081.max.com;
access_log  logs/8081.access.log  main;
location / {
proxy_redirect off;
proxy_pass   http://tomcatserver1;
}
}

EOF

systemctl restart nginx

如上配置,一个最基础的反向代理环境就搭建好了
 
 

缓存配置

##配置缓存参数
mkdir -p /data/nginx-temp /data/nginx-cache
chown -R nginx:nginx /data/nginx-temp /data/nginx-cache

cat >proxy_cache.conf<<EOF
proxy_temp_path /data/nginx-temp;  #缓存临时文件路径
proxy_cache_path /data/nginx-cache levels=1:2 keys_zone=nginx-cache:20m max_size=50m inactive=1m; #缓存保存的路径
EOF

## 添加到nginx.conf 中
sed -r -i "/include conf./i\    include proxy_cache.conf;" nginx.conf

proxy_cache_path 参数说明:

  • levels 指定该缓存空间有两层hash目录,第一层目录为1个数字或者字母,第二层为2个数字或者字母
  • keys_zone 指的是缓存空间名称。 20m 为内存缓存空间大小
  • max_size 指的是缓存文件可以占用的最大空间。
  • inactive 指的是如果一个缓存文件多长时间不被访问,就会被删除。(天:d、秒:s、分:m)
##静态文件缓存配置

cat > conf/conf.d/8081.conf
upstream tomcatserver1 {
server 172.0.0.49: 8081;
}
server {
listen       80;
server_name  8081.max.com;
access_log  logs/8081.access.log  main;
location / {
proxy_redirect off;
proxy_pass   http://tomcatserver1;
}
location ~.*\.(gif|jpg|png|htm|html|css|js|flv|ico|swf)(.*) {
proxy_redirect off;   #[关闭跳转]
proxy_cache nginx-cache;  #[缓存的空间 -- proxy_cache.conf 中定义的  ]
proxy_cache_valid 200 302 1h; #[不同http状态缓存时间不同]
proxy_cache_valid 301 1d;
proxy_cache_valid any 1m;

### 强制缓存后端文件,忽略后端服务器的相关头信息
proxy_ignore_headers Set-Cookie Cache-Control;
proxy_hide_header Cache-Control;
proxy_hide_header Set-Cookie;
###
expires 30d;  #[告诉浏览器缓存有效期-- 30天内可以直接访问浏览器缓存]
proxy_passhttp://static;
}
}

EOF

systemctl restart nginx

 

nginx 负载均衡调度算法及配置

 

负载方式 负载说明
Round‑Robin(默认) 接收到的请求按照顺序逐一分配到不同的后端服务器,即使在使用过程中,某一台后端服务器宕机,nginx会自动将该服务器剔除出队列,请求受理情况不会受到任何影响。 这种方式下,可以给不同的后端服务器设置一个权重值(weight),用于调整不同的服务器上请求的分配率;权重数据越大,被分配到请求的几率越大;该权重值,主要是针对实际工作环境中不同的后端服务器硬件配置进行调整的。
Least Connections 下一次请求选择后端最小链接的服务器
ip_hash 使用IPv4 地址的前3个字节或者IPv6的整个地址用来计算的哈希值进行匹配,这样的算法下一个固定ip地址的客户端总会访问到同一个后端服务器。
hash 通过用户端定义的关键词,如文本,变量或两者组合进行标记分类,并将同一分类的请求始终请求到一个后端服务器,适合后端是cache 的场景。

weight轮询

upstream cluster {
server a weight=5 max_fails=1   fail_timeout=10s;
server b weight=1;
server c weight=1;
server d weight=5 max_fails=1   fail_timeout=10s backup;
}
server {
listen 80;
location / {
proxy_pass http://cluster;
}
}

按照上述配置,Nginx每收到7个客户端的请求,会把其中的5个转发给后端a,把其中的1个转发给后端b,把其中的1个转发给后端c。

server指令支持如下参数

  • weight = number 设置当前服务器的权重, 默认为 1.
  • max_fails = numbe 默认情况下, 失败次数是 1.0为关闭这个服务器.
  • fail_timout = number 超时时间为 10 秒.
  • max_conns=number
  • backup 标识这个服务器是个备份服务器,即当集群中服务器都失效时启用该服务器
  • down 标识这个服务器是个无效的服务器.

Least Connections

upstream cluster {
least_conn;
server a weight=5 max_fails=1   fail_timeout=10s;
server b weight=1;
server c weight=1;
server d weight=5 max_fails=1   fail_timeout=10s backup;

}
server {
listen 80;
location / {
proxy_pass http://cluster;
}
}

ip_hash

upstream cluster {
ip_hash;
server a weight=5 max_fails=1   fail_timeout=10s;
server b weight=1;
server c weight=1;
server d weight=5 max_fails=1   fail_timeout=10s backup;
}
server {
listen 80;
location / {
proxy_pass http://cluster;
}
}

hash

upstream backend {
hash $request_uri consistent;

server backend1.example.com;
server backend2.example.com;
}

其他用法

转发给后端服务

location /path/ {
proxy_pass http://172.0.0.14:8080/path/;
}

转发发给外网域名

location /baidu/ {
proxy_set_header Host www.baidu.com;
proxy_pass https://www.baidu.com/;
}

强制http转https访问

server {
listen  80;
server_name     api.yourdomain.com;
location / {
rewrite ^ /(.*) https://api.yourdomain.com/$1 permanent;
break;
}
error_page 497 https://$host:$server_port$request_uri;
}

server {
listen       443 ssl;
server_name  api.yourdomain.com;

access_log logs/ssl_api_access.log;

ssl on;
ssl_certificate conf.d/certs/api.yourdomain.com_bundle.crt;
ssl_certificate_key conf.d/certs/api.yourdomain.com.key;
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #按照这个协议配置
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;#按照这个套件配置
ssl_prefer_server_ciphers on;

location / {
proxy_pass http://tomcat_servers/$2;
}

location /v1.0/ {
proxy_pass http://tomcat_servers/v1.0/;
proxy_cookie_path /v1.0/ /;
proxy_redirect off;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  nginx 负载均衡
相关文章推荐