Nginx - architecture articles proxy Agent process flow
Development of upstream services and address upstream server command
Function: Specifies have the server address on a set, which address can be domain name, IP address or unix socket address. Can be added to the port after the domain name or IP address, if not a port, use port 80 by default.
upstream backend {
server backend1.example.com weight=5;
server backend2.example.com:8080;
server unix:/tmp/backend3;
}
server {
location / {
proxy_pass http://backend;
}
}
General parameters:
- backup: Specifies the backup server service, only when the non-backup server is not available, the request will be forwarded on to the server.
- It indicates that a service station has been off the assembly line, not in service.
Weighted Round-Robin load balancing algorithm
Function: access server instructions specify a weighted polling upstream service.
ip_hash and hash module
Function: The IP address of the client is the key hash algorithm mapped to specific upstream server.
upstream backend {
ip_hash;
server backend1.example.com;
server backend2.example.com;
server backend3.example.com down;
server backend4.example.com;
}
Variable upstream module provides
The packet sent to the command generator upstream thereof:
proxy_method proxy #请求的方法
proxy_set_header #根据指令修改发往上游的请求,若vaule的值为空字符串,则整个header都不会向上游发送
proxy_pass_request_headers on | off 是否放向上游发送请求
proxy_pass_request_body on | off 是否向上游发送body内容
Receiving a user request packet embodiment thereof:
语法: proxy_buffering on|off
默认值: proxy_buffering on
上下文: http, server, location
该指令开启从后端被代理服务器的响应内容缓冲.
如果缓冲区开启,nginx假定被代理的后端服务器会以最快速度响应,并把内容保存在由指令
client_body_buffer_size
Establish a connection with the upstream server
proxy_connect_timeout 设置超时时间,响应码为502
proxy_next_upstream 上游连接失败,更换一台服务器
proxy_socket_keepalive 上游连接启用Tcp keepalive
http response to receiving the upstream head
语法: proxy_buffer_size the_size
默认值: proxy_buffer_size 4k/8k
上下文: http, server, location
The instructions set the buffer size, the contents of the response obtained from the back-end server agent, will first read is placed here in response to a small part of this header is usually located inside the response content.
Function is disabled in response to the upstream head
proxy_ignore_headers 某些响应头部可以改变nginx的行为,以禁止它们生效
Back upstream failure processing method:
proxy_next_upstream
Premise: does not send anything to the client
Configuration
- error
- timeout
- invalid header
- http_
- non_idempotent
- off
–
location / {
proxy_pass http://app-proxy;
proxy_next_upstream error timeout http_500 http_502 http_503 http_504;
proxy_next_upstream_tries 3;
proxy_connect_timeout 60s;
proxy_read_timeout 60s;
proxy_send_timeout 60s;
proxy_pass_request_headers on;
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
set $domain default;
}