nginx之proxy反向代理模块:
location ^~ /proxy_path/ {
root "/www/html"; 这里没必要配置
index index.html; 这里也没必须配置
proxy_pass http://192.168.223.137/;
proxy_set_header Host $host:$proxy_port;
proxy_set_header X-Real-IP $remote_addr;
#proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
将http://192.168.223.136/proxy_path/ 直接反向代理到http://192.168.223.137/
这个proxy_path的url并不存在代理服务器上,它只是一个虚假的url,关注点是后端服务器的url
当然也可以如下配置:
[root@wadeson proxy_path]# ll
total 4
-rw-r--r--. 1 root root 39 Jul 22 10:17 index.html
由于后端服务器只有一个html文件,当请求的http://192.168.223.136:8080/proxy_path/test.html这个url资源时,实际找的是后端的test.html,后端没有这个文件,所以报错:
![nginx之proxy、cache、upstream模块学习 nginx之proxy、cache、upstream模块学习](https://image.shishitao.com:8440/aHR0cHM6Ly9pbWFnZXMyMDE1LmNuYmxvZ3MuY29tL2Jsb2cvMTE2ODg5Ny8yMDE3MDcvMTE2ODg5Ny0yMDE3MDcyNDE0MDkzMDgwNS00NjYyNzc5NTQucG5n.png?w=700)
location ^~/proxy_path/ { 当这里有/
proxy_pass http://192.168.223.137/proxy_path; 这里没有/时
proxy_set_header Host $host:$proxy_port;
proxy_set_header X-Real-IP $remote_addr;
#proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
![nginx之proxy、cache、upstream模块学习 nginx之proxy、cache、upstream模块学习](https://image.shishitao.com:8440/aHR0cHM6Ly9pbWFnZXMyMDE1LmNuYmxvZ3MuY29tL2Jsb2cvMTE2ODg5Ny8yMDE3MDcvMTE2ODg5Ny0yMDE3MDcyNDE0MTAxNTk5Mi0xNzQyODI4MTkyLnBuZw%3D%3D.png?w=700)
所以,url设定时,有/时一定不要丢
当使用正则表达式做模式匹配时:
location ~* \.(jpg|jpeg|gif|png)$ {
proxy_pass http://192.168.223.137/proxy_path/; 错误的
proxy_set_header Host $host:$proxy_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
检测语法时,就开始报错:
[root@wadeson nginx]# sbin/nginx -t
nginx: [emerg] "proxy_pass" cannot have URI part in location given by regular expression, or inside named location, or inside "if" statement, or inside "limit_except" block in /usr/local/nginx/conf/nginx.conf:107
nginx: configuration file /usr/local/nginx/conf/nginx.conf test failed
上面强调了做正则表达式模式匹配的时候,proxy_pass后端服务器一定不能带有url后缀,必须为
proxy_pass http://192.168.223.137; right
而不是
proxy_pass http://192.168.223.137/proxy_path/;也不是 错误
proxy_pass http://192.168.223.137/; 错误
当访问:http://192.168.223.136:8080/image.jpeg,直接请求的后端的网站根下面的该图片文件:
[root@wadeson htdocs]# pwd
/usr/local/apache2.4/htdocs
[root@wadeson htdocs]# ll
total 120
drwxr-xr-x. 4 root root 4096 Jul 17 11:47 api
-rw-r--r--. 1 root root 103548 Jul 22 10:40 image.jpeg
-rw-r--r--. 1 root root 32 Jul 20 22:41 index.html
-rw-r--r--. 1 root root 151 Jul 7 16:33 index.php.bak
drwxr-xr-x. 2 root root 4096 Jul 22 10:17 proxy_path
当请求为:http://192.168.223.136:8080/proxy_path/image.jpeg
![nginx之proxy、cache、upstream模块学习 nginx之proxy、cache、upstream模块学习](https://image.shishitao.com:8440/aHR0cHM6Ly9pbWFnZXMyMDE1LmNuYmxvZ3MuY29tL2Jsb2cvMTE2ODg5Ny8yMDE3MDcvMTE2ODg5Ny0yMDE3MDcyNDE0MTMwOTA3MS0xODgzMjQ1NzU3LnBuZw%3D%3D.png?w=700)
因为在后端服务器有proxy_path这个目录,并且目录下面有该图片:
[root@wadeson htdocs]# ll proxy_path/
total 108
-rw-r--r-- 1 root root 103548 Jul 23 18:30 image.jpeg
-rw-r--r--. 1 root root 39 Jul 22 10:17 index.html
现在:
[root@wadeson htdocs]# ll
total 120
drwxr-xr-x. 4 root root 4096 Jul 17 11:47 api
-rw-r--r--. 1 root root 103548 Jul 22 10:40 image.jpeg.bak
-rw-r--r-- 1 root root 35 Jul 23 13:59 index.html
-rw-r--r--. 1 root root 151 Jul 7 16:33 index.php.bak
drwxr-xr-x. 2 root root 4096 Jul 23 18:30 proxy_path
[root@wadeson htdocs]# ll proxy_path/
total 108
-rw-r--r-- 1 root root 103548 Jul 23 18:30 image.jpeg
-rw-r--r--. 1 root root 39 Jul 22 10:17 index.html
再次访问:http://192.168.223.136:8080/proxy_path/image.jpeg
而访问:
nginx之cache模块:
1、创建缓存的目录
mkdir /data/cache/nginx
2、修改权限,由于是worker进程进行访问,而worker进程是nginx用户
chown -R nginx:nginx /data/cache/nginx
3、配置缓存目录(http段)
proxy_cache_path /data/nginx/cache levels=1:2 keys_zone=one:30m;
4、然后在server段或者location段进行添加缓存配置
proxy_cache one;
在配置代理服务转发的location可以进行缓存
在进行图片的时候可以进行缓存
proxy_cache_valid 200 1d; 状态码为200的,将它缓存1天
![nginx之proxy、cache、upstream模块学习 nginx之proxy、cache、upstream模块学习](https://image.shishitao.com:8440/aHR0cHM6Ly9pbWFnZXMyMDE1LmNuYmxvZ3MuY29tL2Jsb2cvMTE2ODg5Ny8yMDE3MDcvMTE2ODg5Ny0yMDE3MDcyNDE0MTQxNTAyNC0xODAxMTQ4MzE3LnBuZw%3D%3D.png?w=700)
配置如下:
[root@wadeson ~]# mkdir -pv /data/cache/nginx
[root@wadeson ~]# chown -R nginx:nginx /data/cache/nginx/
proxy_cache_path /data/cache/nginx levels=1:2 keys_zone=nginx_zone:30m;
location ~* \.(jpg|jpeg|gif|png)$ {
proxy_pass http://192.168.223.137;
proxy_cache nginx_zone;
proxy_cache_valid 200 302 1d;
proxy_cache_valid 301 1h;
proxy_cache_valid any 1m;
proxy_set_header Host $host:$proxy_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
访问验证:http://192.168.223.136:8080/image.jpeg,跳转到后端
http://192.168.223.137/image.jpeg
因为进行了缓存设置,现在将后端的该图片文件进行操作:
[root@wadeson htdocs]# mv image.jpeg image.jpeg.bak
然后再次进行访问:
![nginx之proxy、cache、upstream模块学习 nginx之proxy、cache、upstream模块学习](https://image.shishitao.com:8440/aHR0cHM6Ly9pbWFnZXMyMDE1LmNuYmxvZ3MuY29tL2Jsb2cvMTE2ODg5Ny8yMDE3MDcvMTE2ODg5Ny0yMDE3MDcyNDE0MTU1MjIyNy0xNzY3MDMwMTE0LnBuZw%3D%3D.png?w=700)
发现缓存效果已经成功了
这就是实际缓存的内容:
[root@wadeson nginx]# ll 0/95/95690753e430af9fc5259dae96ddb950
-rw------- 1 nginx nginx 103995 Jul 23 11:39 0/95/95690753e430af9fc5259dae96ddb950
如果需要将缓存的内容失效,那么:
1、直接删除该内容
2、使用purge设置(具体看文档)
cache还有其他设置:
proxy_cache_min_uses number;
Sets the number of requests after which the response will be cached.
请求number次之后,响应就将会被缓存
proxy_connect_timeout time;
定义代理服务器请求到后端服务器的连接时长
nginx之upstream模块:
upstream backend { upstream指令在http段定义
server 192.168.223.137:80;
server 192.168.223.137:8080;
}
location ~* \.(jpg|jpeg|gif|png)$ {
#proxy_pass http://192.168.223.137;
#proxy_cache nginx_zone;
#proxy_cache_valid 200 302 1d;
#proxy_cache_valid 301 1h;
#proxy_cache_valid any 1m;
proxy_pass http://backend; 以正则表达式匹配的模式时,url后面没有/
proxy_set_header Host $host:$proxy_port;
proxy_set_header X-Real-IP $remote_addr;
#proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
后端是httpd的80和8080端口:
代理服务器的请求报文设置:
location ~* \.(jpg|jpeg|gif|png)$ {
#proxy_pass http://192.168.223.137;
#proxy_cache nginx_zone;
#proxy_cache_valid 200 302 1d;
#proxy_cache_valid 301 1h;
#proxy_cache_valid any 1m;
proxy_pass http://backend;
#proxy_set_header Host $host:$proxy_port;
proxy_set_header Host $proxy_host:$proxy_port;
proxy_set_header X-Real-IP $remote_addr;
#proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
设置后端的httpd日志格式:
LogFormat "%h %{X-Real-IP}i %{host}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
新增的都是前面代理服务器里面设置过了的,然后启用
CustomLog "logs/access_log" combined
当访问:http://192.168.223.136:8080/image.jpeg时。观察后端日志:
192.168.223.136 192.168.223.1 backend:80 - - [23/Jul/2017:14:22:58 +0800] "GET /image.jpeg HTTP/1.0" 304 - "-" "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko"
192.168.223.136 192.168.223.1 backend:80 - - [23/Jul/2017:14:22:58 +0800] "GET /image.jpeg HTTP/1.0" 304 - "-" "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko"
可以清晰的看到客户端地址和后端服务器的名称信息
定义调度算法:
upstream backend { ip_hash; server backend1.example.com; server backend2.example.com;
}
upstream内部健康状态检测:
max_fails=number
试图连接后端服务器,达到该次数还是不能连接,表示该后端服务器已经不能提供服务了
fail_timeout=time
试图连接后端服务器,达到多长时间后,超过该时长后,该后端服务器将不可用
如果后端服务器其中的某一个宕机后,那么将不会被提供服务
如果需要更新后端服务器,那么可以先将其中的一个服务器进行down,升级成功之后然后启用它,并能提供服务,如果升级正常,那么再执行其他的服务器:
upstream backend { ip_hash; 调度方法 server backend1.example.com; server backend2.example.com; server backend3.example.com down; server backend4.example.com; }
调度方法:
least_conn:最少连接
应用时外部的health_check健康检测:
建议专门使用一个location,执行health_check,并关掉访问日志
因为health_check是每隔一段时间就去检测web服务,所以会产生
大量日志
server { location / { proxy_pass http://dynamic; health_check; }
backup
marks the server as a backup server. It will be passed requests when the primary servers are unavailable.
backup,在其他服务器可用时将不会提供服务,如果其他服务都不可用时,那么备用的该服务器将会提供服务。
基于cookie的负载均衡调度:
基于sticky实现session绑定:
Syntax: | sticky cookie name [expires=time] [domain=domain] [httponly] [secure] [path=path]; sticky route $variable ...; sticky learn create=$variable lookup=$variable zone=name:size [timeout=time]; |
sticky绑定有三种方法
nginx之add_header:
给响应报文加上首部:
location ~* \.(jpg|jpeg|gif|png)$ {
proxy_pass http://backend;
#proxy_set_header Host $host:$proxy_port;
proxy_set_header Host $proxy_host:$proxy_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
add_header X-Via $server_addr; $server_addr这是服务器的ip
$server_addr:
客户端win10向代理服务器请求时,代理服务器是server,为添加响应的首部信息:
add_header X-Via $server_addr;
然后访问:http://192.168.223.136:8080/image.jpeg,观察响应报文:
可以清晰的看见刚刚定义的报文信息
继续添加首部,添加后端服务器的首部信息:
location ~* \.(jpg|jpeg|gif|png)$ {
#proxy_pass http://192.168.223.137;
#proxy_cache nginx_zone;
#proxy_cache_valid 200 302 1d;
#proxy_cache_valid 301 1h;
#proxy_cache_valid any 1m;
proxy_pass http://backend;
#proxy_set_header Host $host:$proxy_port;
proxy_set_header Host $proxy_host:$proxy_port;
proxy_set_header X-Real-IP $remote_addr;
#proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
add_header X-Via $server_addr;
add_header X-Upstream $upstream_addr;
}
$upstream_addr:定义的upstream的ip
访问:http://192.168.223.136:8080/image.jpeg,观察响应报文如下:
![nginx之proxy、cache、upstream模块学习 nginx之proxy、cache、upstream模块学习](https://image.shishitao.com:8440/aHR0cHM6Ly9pbWFnZXMyMDE1LmNuYmxvZ3MuY29tL2Jsb2cvMTE2ODg5Ny8yMDE3MDcvMTE2ODg5Ny0yMDE3MDcyNDE0NDcyNzg3MS04MzU0MzMyNzYucG5n.png?w=700)
定义缓存是否命中:$upstream_cache_status
location ~* \.(jpg|jpeg|gif|png)$ {
#proxy_pass http://192.168.223.137;
#proxy_cache nginx_zone;
#proxy_cache_valid 200 302 1d;
#proxy_cache_valid 301 1h;
#proxy_cache_valid any 1m;
proxy_pass http://backend;
proxy_set_header Host $proxy_host:$proxy_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
add_header X-Via $server_addr;
add_header X-Upstream $upstream_addr;
add_header X-Upstream-Cache $upstream_cache_status;
}
访问:http://192.168.223.136:8080/image.jpeg时,发现并没有看见定义的首部,需要开启缓存功能
location ~* \.(jpg|jpeg|gif|png)$ {
proxy_cache nginx_zone;
proxy_cache_valid 200 302 1d;
proxy_cache_valid 301 1h;
proxy_cache_valid any 1m;
proxy_pass http://backend;
proxy_set_header Host $proxy_host:$proxy_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
add_header X-Via $server_addr;
add_header X-Upstream $upstream_addr;
add_header X-Upstream-Cache $upstream_cache_status;
}
启用缓存功能再加上add_header X-Upstream-Cache $upstream_cache_status;
观察报文首部如下:
![nginx之proxy、cache、upstream模块学习 nginx之proxy、cache、upstream模块学习](https://image.shishitao.com:8440/aHR0cHM6Ly9pbWFnZXMyMDE1LmNuYmxvZ3MuY29tL2Jsb2cvMTE2ODg5Ny8yMDE3MDcvMTE2ODg5Ny0yMDE3MDcyNDE0NDgyNjUxMi04NTUwODgxODEucG5n.png?w=700)
可以观察缓存已经命中了