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,后端没有这个文件,所以报错:

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;
}

所以,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

因为在后端服务器有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天

配置如下:
[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
然后再次进行访问:

发现缓存效果已经成功了
 
这就是实际缓存的内容:
[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,观察响应报文如下:

定义缓存是否命中:$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模块学习的更多相关文章

  1. Nginx缓存[proxy cache、memcache]

    nginx自带缓存 nginx自己有单独的进程来对磁盘上的缓存文件进行扫描,在内存中建立缓存索引.并且有管理进程来对缓存进行过期判断,更新等操作 定义:只能在http段中使用 proxy_cache_ ...

  2. nginx upstream模块

    upstream模块 upstream模块 (100%) nginx模块一般被分成三大类:handler.filter和upstream.前面的章节中,读者已经了解了handler.filter. 利 ...

  3. upstream模块实现反向代理的功能

    nginx平台初探(100%) — Nginx开发从入门到精通 http://tengine.taobao.org/book/chapter_02.html#id6 nginx的模块化体系结构¶ ng ...

  4. Nginx学习总结:proxy与rewrite模块(三)

    斜体下划线,表示建议采用默认配置,无需显式的配置 一.ngx_http_upstream_module 此模块中可配置的指令并不是很多.nginx的负载均衡算法包括: 1)round-robin:轮询 ...

  5. nginx upstream模块--负载均衡

    Module ngx_http_upstream_module英文文档 upstream模块相关说明1.upstream模块应放于nginx.conf配置的http{}标签内2.upstream模块默 ...

  6. [转帖]nginx upstream模块--负载均衡

    nginx upstream模块--负载均衡 https://www.cnblogs.com/linjiqin/p/5494783.html Module ngx_http_upstream_modu ...

  7. Nginx - upstream 模块及参数测试

    目录 - 1. 前言- 2. 配置示例及指令说明    - 2.1 配置示例    - 2.2 指令    - 2.3 upstream相关变量- 3. 参数配置及测试    - 3.1 max_fa ...

  8. nginx proxy cache配置和清理

    1.nginx需要编译Purge模块 2.nginx.conf 配置cache: proxy_cache_path  /home/cache/xxx levels=1:2  keys_zone=cac ...

  9. Nginx Upstream模块源码分析(上)

    Upstream模块是一个很重要的模块,很多其他模块都会使用它来完成对后端服务器的访问, 达到反向代理和负载均衡的效果.例如Fastcgi.Memcached.SessionSticky等. 如果自己 ...

随机推荐

  1. android EditText自动弹出和自动关闭软键盘

    程序进入某个activity直接弹出软键盘,不能直接在OnCreate中设置,必须等View绘制事件完毕才可以弹出,需要用到Timer辅助实现,如果要实现输入的功能,必须让EditText获得焦点. ...

  2. Python--进阶处理4

    #================第四章:迭代器和生成器=================== # 函数 itertools.islice()适用于在迭代器和生成器上做切片操作. import ite ...

  3. NoSQL 常用资源

    Hadoop:http://www.apache.org/dyn/closer.cgi/hadoop/common/ easyhadoop:https://github.com/xianglei/ea ...

  4. datasnap 授权验证DSAuthenticationManager方法应用

    服务端 1.服务端只需要增加DSAuthenticationManager1并且把dshttpservice的AuthenticationManager属性设置为DSAuthenticationMan ...

  5. 在setting中实现可拔插的插件功能实现

    1.setting配置信息 在配置文件中通过对插件进行注释来实现可拔插,例如在配置中将其注释,则在使用此功能不起作用,注释取消后则可正常使用 # 可拔插的插件(plugins) PLUGINS_DIC ...

  6. 【我的Android进阶之旅】Jenkins挂载slave节点,增强分布式编译的效率

    由于公司的Jenkins任务越来越多,而且所有的Android Jenkins任务都在同一台服务器上进行编译,而且该服务器配置Jenkins任务最多3个任务同时运行,所以有时候大家一起编译的时候,只能 ...

  7. Keras + Ubuntu环境搭建

    安装Theano (环境参数:Ubuntu 16.04.2  Python 2.7) 安装 numpy 和 scipy 1.sudo apt-get install python-numpy pyth ...

  8. 技术架构标杆(Certicom Security Architecture)对比思考——By Me at 20140408

    看到一家国外网络安全企业Certicom,官网链接:http://www.certicom.com/,可以作为很好的企业安全技术建构以及产品规划的标杆,下面我绘制了该公司的产品组合以及技术架构框图:

  9. Elasticsearch.js 发布 —— 在Node.js和浏览器中调用Elasticsearch

    继PHP.Ruby.Python和Perl之后,Elasticsearch最近发布了Elasticsearch.js,Elasticsearch的JavaScript客户端库.可以在Node.js和浏 ...

  10. POJ3176:Cow Bowling(数字三角形问题)

    地址:http://poj.org/problem?id=3176 题目解析:没什么好说的,之前上课时老师讲过.从下往上找,每一个三角形的顶点可由两个角加上顶点的值 两种方式得到 ,用dp数组保存下最 ...