JDB nginx Modesty makes one progress pride makes
JDB nginx学习分享 Modesty makes one progress, pride makes one lag behind.
nginx实现反向代理 nginx是高度模块化的,各个功能都封装在模块中,而各个模块的初始化则是根据配置文件来进行的。 实现反向代理的模块 模块 :ngx_http_proxy_module 指令: proxy_pass :指定上游服务器的ip和端口 proxy_set_header :添加一个新的首部 演示配置 反向代理的例子 location / { proxy_pass http: //10. 220. 5. 180; proxy_set_header X-Real-IP $remote_addr proxy_set_header Host $proxy_host; } 指令语法 Syntax: proxy_pass URL; Default: — Context: location, if in location, limit_except 例子:proxy_pass http: //10. 220. 5. 196: 8000; Syntax: proxy_set_header field value; Default: proxy_set_header Host $proxy_host; Context: http, server, location nginx的配置中,可以使用变量 内置变量:nginx自带的 自定义变量:用户定义的,定义方法: set 变量名 变量值
nginx实现反向代理 补充1 location /admin { proxy_pass http: //www. ydy. com/; proxy_pass http: //www. ydy. com; } 请求的url 是http: //www. jdb. com/admin/a. html 如果代理方式是 proxy_pass http: //www. ydy. com/; 那么去www. ydy. com的根目录下找a. html 如果代理方式是 proxy_pass http: //www. ydy. com; 那么去www. zxhk. com的跟目录下的admin找 a. html 补充2 如果location中使用了模式匹配(正则),那么,location中的url会 直接补充到代理节点的后面,此时,上游服务器的的后面不能有任何 内容,包括 / location ~ . php$ { proxy_pass http: //www. jdb. com; <<< 正确写法 proxy_pass http: //www. jdb. com: 80; <<< 正确写法 proxy_pass http: //www. jdb. com/; <<< 错误写法 proxy_pass http: //www. jdb. com/img; <<< 错误写法 } 此时,如果请求的url是 http: //www. baidu. com/book/stu/a. php , 就会代理成 http: //www. jdb. com/book/stu/a. php
N g i点 n x击实添现加页 标面 题缓 文存 字 演示配置 http段: proxy_cache_path /cache/nginx levels=1: 1 keys_zone=mycache: 100 m; Server段: proxy_cache mycache; proxy_cache_valid 200 302 10 m; proxy_cache_valid 301 1 h; proxy_cache_valid any 1 m; proxy_cache_use_stale error timeout invalid_header updating http_500; } 扩展: 如何检查我所请求的数据是哪个主机发送给我的? 添加首部 add_header server. Host $server_addr; 如果检查我所请求的数据在缓存中是否存在? 添加首部 add_header cache. Status $upstream_cache_status;
Ngi点 n x击实添现加负 标载 题均 文衡 字 指令:upstream 作用:定义一个上游服务器组 格式 upstream name { server 上游服务器 1 参数 参数; } 注意: 定义服务器组的时候,不能写http: // Example: upstream backend { server www. jdb. com weight=5; server 127. 0. 0. 1: 8080 max_fails=3 fail_timeout=30 s; server www. ydy. com down ; server 10. 220. 5. 180 backup; } fail_timeout: 在经历了max_fails次失败后, 暂停服务的时间。 max_fails: 和fail_timeout一起使用, 进行对后端服务器的健康状态检查;
Ngi点 n x击实添现加负 标载 题均 文衡 字 演示配置 http段: upstream myservers { server 10. 220. 5. 180 weigjt=1; server 10. 220. 5. 181 weight=2; } Server段: location /test{ proxy_pass http: //myservers; } 演示配置 http段: upstream myservers { ip_hash; server 10. 220. 5. 180 weigjt=1; server 10. 220. 5. 181 weight=2; } Server段: location /test{ proxy_pass http: //myservers; }
JDB 感谢您的观看! 串讲人:刘豪伟 2019年 3月15日 Do one thing at a time, and do well.
- Slides: 22