http proxy模块参数

nginx功能的代理功能是是通过http proxy模块来实现的。默认在安装Nginx是已经安装了http proxy模块,可以直接使用。

http模块相关参数

说明

proxy_set_header

设置http请求header项传给后端服务节点,例如:可实现让代理后端的服务节点获取访问客户端用户的真实IP地址

client_body_buffer_size

用于指定客户端请求主体缓冲区大小,此处如果了解前面的http请求包的原理就好理解了

proxy_connect_timeout

表示反向代理与后端节点服务器连接的超时时间,即发起握手等候相应超时时间

proxy_send_timeout

表示代理后端服务器的数据回传时间,即在规定时间之内后端服务器必须传完所有的数据,否则Nginx将断开这个连接

proxy_read_timeout

设置Nginx从代理的 后端服务器获取信息的时间,表示连接建立成功后,Nginx等待后端服务器的相应时间,其实是Nginx已经进入后端的排队之中等候处理的时间

proxy_buffer_size

设置缓存区大小,默认该缓存区等于指令proxy_buffer设置的大小

proxy_buffer

设置缓存区的数量和大小,Nginx从代理的后端服务器获取的响应信息,会放置到缓存区

proxy_busy_buffer_size

用于设置系统很忙是可以使用的proxy_buffer大小,官方推荐大小为proxy_buffers*2

proxy_temp_file_write_size

指定proxy缓冲临时文件的大小

相关重要参数

相关重要参数

参数说明

proxy_psss http://server_pools

通过proxy_pass功能把用户的请求转向到反向代理定义的upstream服务器池

proxy_set_header Host $host

在代理后端服务器发送的http请求头中加入host字段信息,用于后端服务器配置有多个虚拟主机主机是可以识别是那个虚拟主机。这是节点服务器多虚拟主机时的关键配置

proxy_set_header X-Forwarded-For $remote_addr;

        

在反向代理服务器发送http请求头加入X-Forwarded-For字段信息,用于后端服务程序、日志等接受记录真实的IP,而不是代理服务器的IP这是反向代理时,节点服务器获取用户真实IP的必要功能配置

后面服务器,记录日志格式,main

根据URL中的目录地址实现代理转发说明

1.1.1 案例背景:通过Nginx实现动静分离,即通过Nginx反向代理规则实现让动态资源和静态资源及其他业务分别由不同的服务器解析,已解决网站性能、安全、用户体验等重要问题。

下面图适合网站前端只使用同一个域名提供服务的场景,例如,用户访问的域名是www.etiantian.org,然后,当用户请求求www.etiantian.org/upload/xx地址时,代理会分配请求到上传服务器池处理数据;当用户请求

www.etiantian.org/static/xx地址时,代理会分配请求到静态服务器池请求数据;当用户请求

www.etiantian.org/xx地址时候,即不包含上述指定的目录地址路径时,代理会分配请求到默认的动态服务器池请求数据(注意:上面的xx表示任意路径)

1.1 案例配置实战

当用户请求www.etiantian.org/upload/xx地址时,实现由upload上传服务器池处理请求。

当用户请求www.etiantian.org/static/xx地址时,实现由静态服务器池处理请求

除此之外,对于其他访问请求,全部默认的动态服务器池处理请求

负载均衡器上实现/配置:

用户请求的URI

负责处理的服务器

主机名

站点目录

功能

/upload

10.0.0.8:80

web01

html/www/upload

upload服务器

/static

10.0.0.7:80

web02

html/www/static

static静态服务器

/

10.0.0.9:80

web03

html/bbs

默认

www.etiantian.org/bingbiang.html

www.etiantian.org/upload/bingbiang.html

www.etiantian.org/static/bingbiang.html

#测试proxy_set_header X_Forwarded_For  记录客户端IP地址

#让web服务器员记录客户端IP

  1. worker_processes 1;
  2. events {
  3. worker_connections 1024;
  4. }
  5. http {
  6. include mime.types;
  7. default_type application/octet-stream;
  8. sendfile on;
  9. keepalive_timeout 65;
  10.  
  11. upstream server_pools {
  12. server 10.0.0.7:80 weight=4 max_fails=3 fail_timeout=30s;
  13. server 10.0.0.8:80 weight=4 max_fails=3 fail_timeout=30s;
  14. server 10.0.0.9:80 weight=4 max_fails=3 fail_timeout=30s;
  15. }
  16.  
  17. server {
  18. listen 80;
  19. server_name www.etiantian.org;
  20. location / {
  21. proxy_pass http://server_pools;
  22. proxy_set_header Host $host;
  23. proxy_set_header X-Forwarded-For $remote_addr;
  24. }
  25. }
  26. server {
  27. listen 80;
  28. server_name bbs.etiantian.org;
  29. location / {
  30. proxy_pass http://server_pools;
  31. proxy_set_header Host $host;
  32. proxy_set_header X-Forwarded-For $remote_addr;
  33. }
  34. }
  35. }
  36.  
  37. #在web服务器服务员监测日志信息
  38. #[root@web01 ~]# tailf /application/nginx/logs/access.log
  39.  
  40. #测试结果
  41. #10.0.0.5 - - [25/May/2017:16:55:14 +0800] "GET /bingbing.html HTTP/1.0" 200 14 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.75 Safari/537.36" "10.0.0.253"
  42.  
  43. #重启Nginx,语法检测。
  44.  
  45. #[root@lb01 conf]# /application/nginx/sbin/nginx -t
  46. #nginx: the configuration file /application/nginx-1.10.2/conf/nginx.conf syntax is ok
  47. #nginx: configuration file /application/nginx-1.10.2/conf/nginx.conf test is successful
  48. #[root@lb01 conf]# /application/nginx/sbin/nginx -s reload
  49.  
  50. ###根据用户请求url目录(URI )进行转发 用户请求
  51.  
  52. 第一个里程碑
  53.  
  54. #配置文件
  55.  
  56. worker_processes 1;
  57. events {
  58. worker_connections 1024;
  59. }
  60. http {
  61. include mime.types;
  62. default_type application/octet-stream;
  63. sendfile on;
  64. keepalive_timeout 65;
  65.  
  66. upstream upload_pools {
  67. server 10.0.0.8:80;
  68. }
  69.  
  70. upstream static_pools {
  71. server 10.0.0.7:80;
  72. }
  73.  
  74. upstream default_pools {
  75. server 10.0.0.9:80;
  76. }
  77.  
  78. server {
  79. listen 80;
  80. server_name www.etiantian.org;
  81. location /upload {
  82. proxy_pass http://upload_pools;
  83. proxy_set_header Host $host;
  84. proxy_set_header X-Forwarded-For $remote_addr;
  85. }
  86.  
  87. location /static {
  88. proxy_pass http://static_pools;
  89. proxy_set_header Host $host;
  90. proxy_set_header X-Forwarded-For $remote_addr;
  91. }
  92.  
  93. location / {
  94. proxy_pass http://default_pools;
  95. proxy_set_header Host $host;
  96. proxy_set_header X-Forwarded-For $remote_addr;
  97. }
  98. }
  99. }
  100.  
  101. ########nginx 给予uri 目录的转发
  102.  
  103. #创建环境
  104.  
  105. ##web01
  106. mkdir -p /application/nginx/html/www/upload
  107. echo "web01 upload" >/application/nginx/html/www/upload/bingbing.html
  108.  
  109. ##web02
  110. mkdir -p /application/nginx/html/www/static
  111. echo "web02 static" >/application/nginx/html/www/static/bingbing.html
  112.  
  113. ##web03
  114. echo "web03 default" >/application/nginx/html/www/bingbing.html
  115.  
  116. ##测试结果
  117. #[root@lb01 conf]# curl 10.0.0.5/bingbing.html
  118. #web03 default
  119. #[root@lb01 conf]# curl 10.0.0.5/static/bingbing.html
  120. #web02 static
  121. #[root@lb01 conf]# curl 10.0.0.5/upload/bingbing.html
  122. #web01 upload

#让web服务器员记录客户端IP

##根据用户请求的url目录(URI)进行转发 用户的请求总的

  1. ####第一个里程碑-规划 分类
  2. /upload 10.0.0.8:80 upload服务器
  3. /static 10.0.0.7:80 static静态服务器
  4. / 10.0.0.9:80 默认
  5.  
  6. ####第二个里程碑-创建澡堂子
  7.  
  8. upstream upload_pools {
  9. server 10.0.0.8:80;
  10. }
  11.  
  12. upstream static_pools {
  13. server 10.0.0.7:80;
  14. }
  15.  
  16. upstream default_pools {
  17. server 10.0.0.9:80;
  18. }
  19.  
  20. ##第三个里程碑-什么时候去某一个澡堂子(条件)
  21. location ====== 专门用来匹配 判断 uri if ($uri ~ xxxx)
  22.  
  23. location /static/ {
  24. proxy_pass http://static_pools;
  25. proxy_set_header Host $host;
  26. proxy_set_header X-Forwarded-For $remote_addr;
  27. }
  28.  
  29. #将符合upload的请求交给上传服务器池upload_pools,配置如下:
  30. location /upload/ {
  31. proxy_pass http://upload_pools;
  32. proxy_set_header Host $host;
  33. proxy_set_header X-Forwarded-For $remote_addr;
  34. }
  35.  
  36. #不符合上述规则的请求,默认全部交给动态服务器池default_pools,配置如下:
  37. location / {
  38. proxy_pass http://default_pools;
  39. proxy_set_header Host $host;
  40. proxy_set_header X-Forwarded-For $remote_addr;
  41. }
  42.  
  43. ###第四个里程碑-配置lb负载均衡
  44.  
  45. worker_processes 1;
  46. events {
  47. worker_connections 1024;
  48. }
  49. http {
  50. include mime.types;
  51. default_type application/octet-stream;
  52. sendfile on;
  53. keepalive_timeout 65;
  54. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  55. '$status $body_bytes_sent "$http_referer" '
  56. '"$http_user_agent" "$http_x_forwarded_for"';
  57.  
  58. upstream upload_pools {
  59. server 10.0.0.8:80;
  60. }
  61.  
  62. upstream static_pools {
  63. server 10.0.0.7:80;
  64. }
  65.  
  66. upstream default_pools {
  67. server 10.0.0.9:80;
  68. }
  69.  
  70. server {
  71. listen 80;
  72. server_name www.etiantian.org;
  73. location /static/ {
  74. proxy_pass http://static_pools;
  75. proxy_set_header Host $host;
  76. proxy_set_header X-Forwarded-For $remote_addr;
  77. }
  78.  
  79. location /upload/ {
  80. proxy_pass http://upload_pools;
  81. proxy_set_header Host $host;
  82. proxy_set_header X-Forwarded-For $remote_addr;
  83. }
  84.  
  85. location / {
  86. proxy_pass http://default_pools;
  87. proxy_set_header Host $host;
  88. proxy_set_header X-Forwarded-For $remote_addr;
  89. }
  90. access_log logs/access_www.log main;
  91. }
  92. }
  93.  
  94. ###第五个里程碑-创建环境
  95. www.etiantian.org/bingbing.html
  96. www.etiantian.org/upload/bingbing.html
  97. www.etiantian.org/static/bingbing.html
  98.  
  99. ##web01
  100. mkdir -p /application/nginx/html/www/upload
  101. echo "web01 upload" >/application/nginx/html/www/upload/bingbing.html
  102.  
  103. ##web02
  104. mkdir -p /application/nginx/html/www/static
  105. echo "web02 static" >/application/nginx/html/www/static/bingbing.html
  106.  
  107. ##web03
  108. echo "web03 default" >/application/nginx/html/www/bingbing.html
  109.  
  110. ###第五个里程碑-进行测试
  111.  
  112. #[root@lb01 conf]# curl www.etiantian.org/bingbing.html
  113. #web03 default
  114. #[root@lb01 conf]# curl www.etiantian.org/static/bingbing.html
  115. #web02 static
  116. #[root@lb01 conf]# curl www.etiantian.org/upload/bingbing.html
  117. #web01 upload
  118.  
  119. ####下面三条要解析否则会报404
  120. curl www.etiantian.org/bingbing.html
  121. curl www.etiantian.org/static/bingbing.html
  122. curl www.etiantian.org/upload/bingbing.html
  123.  
  124. curl 10.0.0.5/bingbing.html
  125. curl 10.0.0.5/static/bingbing.html
  126. curl 10.0.0.5/upload/bingbing.html

##根据用户请求的url目录(URI)进行转发 用户的请求总的

根据用户客户端的设备(user_agent)转发实践

###nginx.conf lb01 基于 用户的客户端浏览器

  1. worker_processes 1;
  2. events {
  3. worker_connections 1024;
  4. }
  5. http {
  6. include mime.types;
  7. default_type application/octet-stream;
  8. sendfile on;
  9. keepalive_timeout 65;
  10.  
  11. upstream upload_pools {
  12. server 10.0.0.8:80;
  13. }
  14.  
  15. upstream static_pools {
  16. server 10.0.0.7:80;
  17. }
  18.  
  19. upstream default_pools {
  20. server 10.0.0.9:80;
  21. }
  22.  
  23. server {
  24. listen 80;
  25. server_name www.etiantian.org;
  26. location / {
  27. if ($http_user_agent ~* "MSIE")
  28.  
  29. {
  30. proxy_pass http://static_pools;
  31. }
  32. if ($http_user_agent ~* "Chrome")
  33.  
  34. {
  35. proxy_pass http://upload_pools;
  36. }
  37. proxy_pass http://default_pools;
  38. proxy_set_header Host $host;
  39. proxy_set_header X-Forwarded-For $remote_addr;
  40.  
  41. }
  42. }
  43. }
  44.  
  45. ##因为10.0.0.5下没有动态或静态的东西
  46.  
  47. [root@lb01 conf]# curl 10.0.0.5/bingbing.html
  48. web03 default
  49. [root@lb01 conf]# curl 10.0.0.5/static/bingbing.html
  50. <html>
  51. <head><title>404 Not Found</title></head>
  52. <body bgcolor="white">
  53. <center><h1>404 Not Found</h1></center>
  54. <hr><center>nginx/1.10.2</center>
  55. </body>
  56. </html>
  57. [root@lb01 conf]# curl 10.0.0.5/upload/bingbing.html
  58. <html>
  59. <head><title>404 Not Found</title></head>
  60. <body bgcolor="white">
  61. <center><h1>404 Not Found</h1></center>
  62. <hr><center>nginx/1.10.2</center>
  63. </body>
  64. </html>
  65.  
  66. #加A + 参数指定浏览器名字
  67.  
  68. [root@lb01 conf]# curl -A chrome 10.0.0.5/upload/bingbing.html
  69. web01 upload
  70. [root@lb01 conf]# curl 10.0.0.5/bingbing.html
  71. web03 default
  72. [root@lb01 conf]# curl -A msie 10.0.0.5/static/bingbing.html
  73. web02 static
  74.  
  75. [root@lb01 conf]# ####访问一个目录 nginx 默认找的文件是 index.html index.htm
  76. [root@lb01 conf]# #####如果这些文件不存在 nginx报错 403
  77. [root@lb01 conf]# curl -A chrome 10.0.0.5/upload/oldboy.txt
  78. <html>
  79. <head><title>404 Not Found</title></head>
  80. <body bgcolor="white">
  81. <center><h1>404 Not Found</h1></center>
  82. <hr><center>nginx/1.10.2</center>
  83. </body>
  84. </html>
  85.  
  86. ##################
  87. ###报404
  88.  
  89. [root@lb01 conf]# ####1.10.0.0.5 访问的反向代理 lb01
  90. [root@lb01 conf]# ####2.10.0.0.5 默认访问第一个虚拟主机 server
  91. [root@lb01 conf]# ####3.查看对应的条件 uri 目录
  92. [root@lb01 conf]# # if ($http_user_agent ~* "MSIE")
  93. [root@lb01 conf]# #
  94. [root@lb01 conf]# # {
  95. [root@lb01 conf]# # proxy_pass http://static_pools;
  96. [root@lb01 conf]# # }
  97. [root@lb01 conf]# # if ($http_user_agent ~* "Chrome")
  98. [root@lb01 conf]# #
  99. [root@lb01 conf]# # {
  100. [root@lb01 conf]# # proxy_pass http://upload_pools;
  101. [root@lb01 conf]# # }
  102. [root@lb01 conf]# #proxy_pass http://default_pools;
  103. [root@lb01 conf]# ####4.最后找到的是 默认的池塘 里面没有 upload 目录
  104. [root@lb01 conf]# ####5. 没有这个目录 404 找不到
  105. [root@lb01 conf]# curl 10.0.0.5/upload/
  106. <html>
  107. <head><title>404 Not Found</title></head>
  108. <body bgcolor="white">
  109. <center><h1>404 Not Found</h1></center>
  110. <hr><center>nginx/1.10.2</center>
  111. </body>
  112. </html>
  113.  
  114. ############403错误
  115. [root@lb01 conf]# curl -A msie 10.0.0.5/static/
  116. <html>
  117. <head><title>403 Forbidden</title></head>
  118. <body bgcolor="white">
  119. <center><h1>403 Forbidden</h1></center>
  120. <hr><center>nginx/1.10.2</center>
  121. </body>
  122. </html>
  123. [root@lb01 conf]# ####1.lb01
  124. [root@lb01 conf]# ####2.匹配的是第一个虚拟主机 10.0.0.5 www.etiantian.org
  125. [root@lb01 conf]# ####3.进入location
  126.  
  127. [root@lb01 conf]# ####4.进入location /
  128. [root@lb01 conf]# ####5.判断
  129. [root@lb01 conf]# ####-A 装作是msie
  130. [root@lb01 conf]# ####7.10.0.0.7 这个机器上面 有/static 目录
  131. [root@lb01 conf]# ####8.10.0.0.5/static/ ===== 10.0.0.5/static/index.html
  132. [root@lb01 conf]# ####9.找首页文件 但是 首页文件不存在就显示403 默认去找index.html
  133. [root@lb01 conf]#
  134. [root@lb01 conf]# ####10.找不到就汇报403 错误 。
  135.  
  136. ##1.浏览器缓存 ctrl+F5
  137. ##2.域名没有解析
  138. ##3.修改了配置文件,没重启 配置文件没生效
  139.  
  140. [root@lb01 conf]# cat nginx.conf
  141. ####lb01负载均衡
  142. worker_processes 1;
  143. events {
  144. worker_connections 1024;
  145. }
  146. http {
  147. include mime.types;
  148. sendfile on;
  149. keepalive_timeout 65;
  150. default_type application/octet-stream;
  151. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  152. '$status $body_bytes_sent "$http_referer" '
  153. '"$http_user_agent" "$http_x_forwarded_for"';
  154. upstream uplaod_pools {
  155. server 10.0.0.8:80;
  156. }
  157. upstream static_pools {
  158. server 10.0.0.7:80;
  159. }
  160. upstream default_pools {
  161. server 10.0.0.9:80;
  162. }
  163.  
  164. server {
  165. listen 80;
  166. server_name www.etiantian.org;
  167. location / {
  168. if ($http_user_agent ~* "MSIE")
  169. {
  170. proxy_pass http://static_pools;
  171. }
  172. if ($http_user_agent ~* "Chrome")
  173. {
  174. proxy_pass http://uplaod_pools;
  175. }
  176. proxy_pass http://default_pools;
  177. }
  178. access_log logs/access_www.log main;
  179. }
  180. }

###nginx.conf lb01 基于 用户的客户端浏览器

http proxy模块参数的更多相关文章

  1. nginx反向代理proxy模块相关参数

    http_proxy_module Proxy_pass proxy_pass指令属于ngx_http_proxy_module模块,此模块可以将请求转发到另一台服务器:官方说明:http://ngi ...

  2. nginx的proxy模块详解以及参数

    文章来源 运维公会:nginx的proxy模块详解以及参数 使用nginx配置代理的时候,肯定是要用到http_proxy模块.这个模块也是在安装nginx的时候默认安装.它的作用就是将请求转发到相应 ...

  3. 【web安全】第五弹:burpsuite proxy模块的一些理解

    作为一只小小小白的安全新手,只会简单的用sqlmap扫扫网站,用burpsuite的proxy模块拦截一些请求.最近又对proxy有点儿小理解,记录之. 1. 查看sqlmap注入的语句以及HTTP ...

  4. Nginx配置之location模块和proxy模块

    1.location指令的用法介绍 Location主要用来匹配url,如:http://www.beyond.com/nice,在这里对于location来说www.beyond.com是域名,/n ...

  5. insmod module_param 模块参数

    模块参数 引导模块时,可以向它传递参数.要使用模块参数加载模块,这样写: insmod module.ko [param1=value param2=value ...] 为了使用这些参数的值,要在模 ...

  6. Verilog 模块参数重定义(转)

    Verilog重载模块参数: 当一个模块引用另外一个模块时,高层模块可以改变低层模块用parameter定义的参数值,改变低层模块的参数值可采用以下两种方式: 1)defparam 重定义参数语法:d ...

  7. 模块参数,系统调用,字符设备编程重要数据结构,设备号的申请与注册,关于cdev的API

    1.模块参数  应用编程:      int main(int argc, char *argv[])      {               }      ./a.out xxx yyy zzz  ...

  8. Linux设备驱动程序 之 模块参数

    模块支持参数的方法 内核允许驱动程序指定参数,这些参数可在运行insmod或者modprobe命令装载模块时赋值,modprobe还可以从它的配置文件(/etc/modporb.conf)中读取参数值 ...

  9. linux模块参数

    驱动需要知道的几个参数因不同的系统而不同. 从使用的设备号( 如我们在下一章见到的 ) 到驱动应当任何操作的几个方面. 例如, SCSI 适配器的驱动常常有选项控制标记命令队列 的使用, IDE 驱动 ...

随机推荐

  1. bootstrap-实现loading效果

    可以使用bootstrap的模态框(modal.js),使用它我们可以做出loading效果. html <!-- loading --> <div class="moda ...

  2. HTTPS之acme.sh申请证书

    1.关于let's encrypt和acme.sh的简介 1.1 let's encrypt Let's Encrypt是一个于2015年三季度推出的数字证书认证机构,旨在以自动化流程消除手动创建和安 ...

  3. [Java] Windows/Linux路径不同时,统一war的最简办法

    作者: zyl910 一.缘由 在项目开发时,因为运行环境的不同,导致有时得分别为不同的环境,切换配置参数打不同war包.但手工切换配置文件的话,不仅费时费力,而且容易出错. 有些打包工具支持配置切换 ...

  4. Effective Java 第三版—— 85. 其他替代方式优于Java本身序列化

    Tips 书中的源代码地址:https://github.com/jbloch/effective-java-3e-source-code 注意,书中的有些代码里方法是基于Java 9 API中的,所 ...

  5. 【C语言天天练(三)】typedef具体解释

    引言: typedef能够看作type define的缩写,顾名思义就是类型定义,也就是说它仅仅是给已有的类型又一次定义了一个方便使用的别名.并没有产生新的数据类型. typedef与define的不 ...

  6. Xshell设置密钥登录CentOS6.5_64位(文字命令版)

    1.新建/root/.ssh目录 mkdir /root/.ssh 2.创建authorized_keys文件 vi /root/.ssh/authorized_keys 3.复制公钥内容保存 :wq ...

  7. The thumbprint of same asymmetric key is not same in 'SQL Server Connector for Microsoft Azure Key Vault' 1.0.4.0 and 'SQL Server Connector for Microsoft Azure Key

    https://support.microsoft.com/en-us/help/4470999/db-backup-problems-to-sql-server-connector-for-azur ...

  8. Django Rest Framework(认证、权限、限制访问频率)

    阅读原文Django Rest Framework(认证.权限.限制访问频率) django_rest_framework doc django_redis cache doc

  9. Mongodb常用增删改查语法

    1,新增 新增有两种方式 var Tank = mongoose.model('Tank', yourSchema); var small = new Tank({ size: 'small' }); ...

  10. vim简单的移动光标

    vim学习简单的光标移动 ==> 移动到行首 $ ==> 移动到行尾 ^ ==> 移动到本行第一个不是blank的位置 g_ ==> 移动到本行最后一个不是blank的位置^Z ...