http proxy模块参数

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

worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    
    
    upstream server_pools { 
         server 10.0.0.7:80 weight=4 max_fails=3 fail_timeout=30s;
         server 10.0.0.8:80 weight=4 max_fails=3 fail_timeout=30s;
         server 10.0.0.9:80 weight=4 max_fails=3 fail_timeout=30s;
    }       
    
    server { 
       listen       80;
       server_name  www.etiantian.org;
       location / {
        proxy_pass http://server_pools; 
        proxy_set_header  Host $host;
        proxy_set_header  X-Forwarded-For $remote_addr; 
       }
    }
    server { 
       listen       80;
       server_name  bbs.etiantian.org;
       location / {
        proxy_pass http://server_pools; 
        proxy_set_header  Host $host;
        proxy_set_header  X-Forwarded-For $remote_addr; 
       }
    }
}



#在web服务器服务员监测日志信息
#[root@web01 ~]# tailf /application/nginx/logs/access.log 

#测试结果
#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"

#重启Nginx,语法检测。

#[root@lb01 conf]# /application/nginx/sbin/nginx -t
#nginx: the configuration file /application/nginx-1.10.2/conf/nginx.conf syntax is ok
#nginx: configuration file /application/nginx-1.10.2/conf/nginx.conf test is successful
#[root@lb01 conf]# /application/nginx/sbin/nginx -s reload






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

第一个里程碑



#配置文件

worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    
    upstream upload_pools {
      server 10.0.0.8:80;
    }
    
    upstream static_pools {
      server 10.0.0.7:80;
    }
    
    upstream default_pools {
      server 10.0.0.9:80;
    } 
    
    server { 
       listen       80;
       server_name  www.etiantian.org;
         location /upload {
             proxy_pass http://upload_pools;  
             proxy_set_header Host $host;
             proxy_set_header X-Forwarded-For $remote_addr;
         }
         
         location /static {
             proxy_pass http://static_pools;  
             proxy_set_header Host $host;
             proxy_set_header X-Forwarded-For $remote_addr;
         }
         
         
         location / {
             proxy_pass http://default_pools;  
             proxy_set_header Host $host;
             proxy_set_header X-Forwarded-For $remote_addr;
         }
    }
}

########nginx  给予uri 目录的转发




#创建环境 


##web01 
mkdir -p /application/nginx/html/www/upload
echo  "web01 upload" >/application/nginx/html/www/upload/bingbing.html 

##web02 
mkdir -p /application/nginx/html/www/static
echo  "web02 static" >/application/nginx/html/www/static/bingbing.html 

##web03 
echo  "web03 default" >/application/nginx/html/www/bingbing.html



##测试结果
#[root@lb01 conf]# curl 10.0.0.5/bingbing.html
#web03 default
#[root@lb01 conf]# curl 10.0.0.5/static/bingbing.html
#web02 static
#[root@lb01 conf]# curl 10.0.0.5/upload/bingbing.html
#web01 upload
#让web服务器员记录客户端IP

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

####第一个里程碑-规划 分类
/upload      10.0.0.8:80       upload服务器
/static         10.0.0.7:80       static静态服务器
/            10.0.0.9:80       默认


####第二个里程碑-创建澡堂子


upstream upload_pools {
  server 10.0.0.8:80;
}

upstream static_pools {
  server 10.0.0.7:80;
}

upstream default_pools {
  server 10.0.0.9:80;
}


##第三个里程碑-什么时候去某一个澡堂子(条件)
location  ====== 专门用来匹配 判断 uri  if ($uri ~ xxxx)





location /static/ { 
    proxy_pass http://static_pools;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $remote_addr;
}

#将符合upload的请求交给上传服务器池upload_pools,配置如下:
location /upload/ { 
    proxy_pass http://upload_pools;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $remote_addr;
}

#不符合上述规则的请求,默认全部交给动态服务器池default_pools,配置如下:
location / { 
    proxy_pass http://default_pools;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $remote_addr;
}

###第四个里程碑-配置lb负载均衡 

worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
                      
                      
    upstream upload_pools {
      server 10.0.0.8:80;
    }

    upstream static_pools {
      server 10.0.0.7:80;
    }

    upstream default_pools {
      server 10.0.0.9:80;
    }

    server {
        listen 80;
        server_name www.etiantian.org;
    location /static/ { 
        proxy_pass http://static_pools;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $remote_addr;
    }

        location /upload/ { 
        proxy_pass http://upload_pools;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $remote_addr;
    }

     location / { 
        proxy_pass http://default_pools;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $remote_addr;
    }
         access_log  logs/access_www.log  main;
    }
}

###第五个里程碑-创建环境
www.etiantian.org/bingbing.html
www.etiantian.org/upload/bingbing.html
www.etiantian.org/static/bingbing.html 

##web01 
mkdir -p /application/nginx/html/www/upload
echo  "web01 upload" >/application/nginx/html/www/upload/bingbing.html 

##web02 
mkdir -p /application/nginx/html/www/static
echo  "web02 static" >/application/nginx/html/www/static/bingbing.html 

##web03 
echo  "web03 default" >/application/nginx/html/www/bingbing.html

###第五个里程碑-进行测试

#[root@lb01 conf]# curl www.etiantian.org/bingbing.html 
#web03 default
#[root@lb01 conf]# curl www.etiantian.org/static/bingbing.html 
#web02 static
#[root@lb01 conf]# curl www.etiantian.org/upload/bingbing.html 
#web01 upload

####下面三条要解析否则会报404
curl www.etiantian.org/bingbing.html 
curl www.etiantian.org/static/bingbing.html
curl www.etiantian.org/upload/bingbing.html 

curl 10.0.0.5/bingbing.html
curl 10.0.0.5/static/bingbing.html
curl 10.0.0.5/upload/bingbing.html
##根据用户请求的url目录(URI)进行转发 用户的请求总的

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

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

worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    
    upstream upload_pools {
      server 10.0.0.8:80;
    }
    
    upstream static_pools {
      server 10.0.0.7:80;
    }
    
    upstream default_pools {
      server 10.0.0.9:80;
    } 
    
    server { 
       listen       80;
       server_name  www.etiantian.org;
        location / {
         if ($http_user_agent ~* "MSIE")

          {
            proxy_pass http://static_pools;
          }
         if ($http_user_agent ~* "Chrome")

          {
            proxy_pass http://upload_pools;
          }
        proxy_pass http://default_pools;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $remote_addr;
        
       }
    }
}

##因为10.0.0.5下没有动态或静态的东西

[root@lb01 conf]# curl 10.0.0.5/bingbing.html 
web03 default
[root@lb01 conf]# curl 10.0.0.5/static/bingbing.html 
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.10.2</center>
</body>
</html>
[root@lb01 conf]# curl 10.0.0.5/upload/bingbing.html 
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.10.2</center>
</body>
</html>


#加A + 参数指定浏览器名字

[root@lb01 conf]# curl -A chrome 10.0.0.5/upload/bingbing.html 
web01 upload
[root@lb01 conf]# curl 10.0.0.5/bingbing.html 
web03 default
[root@lb01 conf]# curl -A msie 10.0.0.5/static/bingbing.html
web02 static





[root@lb01 conf]# ####访问一个目录  nginx 默认找的文件是 index.html index.htm 
[root@lb01 conf]# #####如果这些文件不存在  nginx报错 403 
[root@lb01 conf]# curl -A chrome 10.0.0.5/upload/oldboy.txt
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.10.2</center>
</body>
</html>




##################
###报404

[root@lb01 conf]# ####1.10.0.0.5  访问的反向代理 lb01 
[root@lb01 conf]# ####2.10.0.0.5 默认访问第一个虚拟主机 server  
[root@lb01 conf]# ####3.查看对应的条件 uri 目录 
[root@lb01 conf]#         # if ($http_user_agent ~* "MSIE")
[root@lb01 conf]#         #
[root@lb01 conf]#         #  {
[root@lb01 conf]#         #    proxy_pass http://static_pools;
[root@lb01 conf]#         #  }
[root@lb01 conf]#         # if ($http_user_agent ~* "Chrome")
[root@lb01 conf]#         #
[root@lb01 conf]#         #  {
[root@lb01 conf]#         #    proxy_pass http://upload_pools;
[root@lb01 conf]#         #  }
[root@lb01 conf]#         #proxy_pass http://default_pools;
[root@lb01 conf]# ####4.最后找到的是 默认的池塘 里面没有 upload 目录 
[root@lb01 conf]# ####5. 没有这个目录 404 找不到
[root@lb01 conf]# curl  10.0.0.5/upload/
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.10.2</center>
</body>
</html>



############403错误
[root@lb01 conf]# curl -A msie  10.0.0.5/static/
<html>
<head><title>403 Forbidden</title></head>
<body bgcolor="white">
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.10.2</center>
</body>
</html>
[root@lb01 conf]# ####1.lb01 
[root@lb01 conf]# ####2.匹配的是第一个虚拟主机 10.0.0.5  www.etiantian.org 
[root@lb01 conf]# ####3.进入location 

[root@lb01 conf]# ####4.进入location /
[root@lb01 conf]# ####5.判断 
[root@lb01 conf]# ####-A 装作是msie 
[root@lb01 conf]# ####7.10.0.0.7 这个机器上面  有/static 目录 
[root@lb01 conf]# ####8.10.0.0.5/static/  ===== 10.0.0.5/static/index.html 
[root@lb01 conf]# ####9.找首页文件 但是 首页文件不存在就显示403     默认去找index.html 
[root@lb01 conf]# 
[root@lb01 conf]# ####10.找不到就汇报403 错误 。



##1.浏览器缓存 ctrl+F5 
##2.域名没有解析 
##3.修改了配置文件,没重启  配置文件没生效






[root@lb01 conf]# cat nginx.conf
####lb01负载均衡
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    sendfile        on;
    keepalive_timeout  65;
    default_type  application/octet-stream;
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    upstream uplaod_pools {
        server 10.0.0.8:80;
    }
    upstream static_pools {
        server 10.0.0.7:80;
    }
    upstream default_pools {
        server 10.0.0.9:80;
    }

    server {
        listen 80;
         server_name www.etiantian.org;
        location / {
          if ($http_user_agent ~* "MSIE")
          {
             proxy_pass http://static_pools;
          }
           if ($http_user_agent ~* "Chrome")
          {
             proxy_pass http://uplaod_pools;
          }
             proxy_pass http://default_pools;
         }
                access_log  logs/access_www.log  main; 
    }
}
###nginx.conf lb01 基于 用户的客户端浏览器

猜你喜欢

转载自www.cnblogs.com/zhaojingyu/p/9073469.html
今日推荐