Nginx: ngx_http_proxy_module
Module ngx_http_upstream_module (nginx.org)
大坑:
proxy_pass & location:
proxy_pass后有URI时分两种情况 location为RegExp location为Prefix:
location为RegExp类型时, proxy_pass必须使用分组捕获, 否则报错
location 为prefix类型时:
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 4096;
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format vacate 'upstream_addr: $upstream_addr, upstream_connect_time: $upstream_connect_time '
'upstream_header_time: $upstream_header_time, upstream_response_time: $upstream_response_time '
'upstream_response_length: $upstream_response_length, upstream_bytes_received: $upstream_bytes_received '
'upstream_status: $upstream_status, upstream_http_server: $upstream_http_server upstream_cache_status: $upstream_cache_status';
upstream vacate {
#ip_hash;
#hash user_$arg_username consistent; # 对$args中username进行后hash调度, 没有则轮询
hash $custom_key;
server 127.0.0.1:2020 weight=2 max_conns=2 max_fails=2 fail_timeout=5;
server 127.0.0.1:3030 weight=1;
}
upstream nextupstream {
server 127.0.0.1:5051;
server 127.0.0.1:5052;
}
server {
listen 80;
listen [::]:80;
server_name vend.intrinsic.io *.intrinsic.io;
root /usr/share/nginx/html;
error_log /var/log/nginx/error.log debug;
access_log /var/log/nginx/a.log vacate;
rewrite_log on;
if ($uri ~* ^/(?[^/]+)/?.*$) {
set $custom_key $path; # 对URI第一段路径进行hash后调度
}
location ^~ /prefix {
proxy_pass http://vacate/abc/;
proxy_http_version 1.0;
proxy_method PUT; # Must be uppercase
proxy_set_header Connection ''; # keep-alive | close, '' 空串不向后端传递
proxy_set_header Host $proxy_host;
proxy_set_header Custom-Header ${http_custom_header}bb;
proxy_pass_request_headers on;
proxy_pass_request_body on;
#proxy_set_body 'proxy body'; # 会覆盖proxy_pass_request_body 抓包tcpdump -i lo -s0 -A port 2020
}
location ^~ / {
root /node; # upstream返回文件存储位置
proxy_pass http://localhost:4040;
proxy_store on;
proxy_temp_path /tmp; # upstream返回文件临时存储位置
proxy_set_header Accept-Encoding ''; # 禁止upstream压缩
proxy_store_access user:rw group:rw all:r; # worker进程的user,group, 比如nginx
proxy_pass_header server; # case-insensitive 把upstream的Server传给client
proxy_ignore_headers X-Accel-Limit-Rate; # 忽略upstream的指定header, 解除限速
#proxy_hide_header custom-header; # 隐藏upstream header, case-insensitive
}
location ^~ /nextupstream {
proxy_pass http://nextupstream;
proxy_connect_timeout 1s;
#proxy_next_upstream error; # upstream 502时, 此upstream被排除
#proxy_next_upstream http_500; # upstream 返回500时, 此upstream被排除
}
location ^~ /intercept {
proxy_pass http://localhost:6060;
proxy_intercept_errors on; # proxied responses with codes greater than or equal to 300 should be passed to a client or be intercepted and redirected to nginx for processing with the error_page directive.
}
error_page 404 /404.html;
location = /404.html {
}
error_page 300 500 502 503 504 /50x.html;
location = /50x.html {
}
}
server {
listen 2020 default_server;
server_name _;
server_name_in_redirect off;
error_log /var/log/nginx/2020.log debug;
resolver 192.168.8.1;
root /opt;
return 205 '
connection=$http_connection
uri=$uri
request_method=$request_method
server_protocol=$server_protocol
request=$request
request_uri=$request_uri
server_port=$server_port
http_custom_header=$http_custom_header\n';
}
server {
listen 3030 default_server;
server_name _;
server_name_in_redirect off;
error_log /var/log/nginx/3030.log debug;
resolver 192.168.8.1;
return 205 '
connection=$http_connection
uri=$uri
request_method=$request_method
server_protocol=$server_protocol
request=$request
request_uri=$request_uri
server_port=$server_port
http_custom_header=$http_custom_header\n';
}
server {
server_tokens off;
listen 4040 default_server;
server_name _;
add_header Custom-Header 'Custom-Header Value'; # 403时不会添加
add_header X-Accel-Limit-Rate 10; # upstream返给Nginx, Nginx会对client限速10bytes/s, client不会看到此header, Nginx专用
root /opt;
}
server {
listen 5051 default_server;
server_name _;
return 205 '
connection=$http_connection
uri=$uri
request_method=$request_method
server_protocol=$server_protocol
request=$request
request_uri=$request_uri
server_port=$server_port
http_custom_header=$http_custom_header\n';
}
server {
listen 5052 default_server;
server_name _;
return 500 '
connection=$http_connection
uri=$uri
request_method=$request_method
server_protocol=$server_protocol
request=$request
request_uri=$request_uri
server_port=$server_port
http_custom_header=$http_custom_header\n';
}
server {
listen 6060 default_server;
server_name _;
return 300 '$server_port';
}
}