nginx的proxy_pass在两个模块中都有:ngx_stream_proxy_module,ngx_http_proxy_module
proxy_pass的功能就是做反向代理。
nginx1.9.0版本开始,开始支持ngx_stream_proxy_module模块,支持4层转发,但是nginx默认只安装了ngx_http_proxy_module。要想安装ngx_stream_proxy_module,只需要增加编译 ./configure --with-stream ,这样就可以了,k8s集群中就使用了nginx的4层转发功能,很牛逼。
ngx_stream_proxy_module模块的proxy_pass只能在server层使用,如k8s集群中的配置


cat > /opt/k8s/kube-nginx/conf/kube-nginx.conf <<EOF worker_processes 1; events { worker_connections 1024; } stream { upstream backend { hash $remote_addr consistent; server 172.27.128.150:6443 max_fails=3 fail_timeout=30s; server 172.27.128.149:6443 max_fails=3 fail_timeout=30s; server 172.27.128.148:6443 max_fails=3 fail_timeout=30s; } server { listen 127.0.0.1:8443; proxy_connect_timeout 1s; proxy_pass backend; } } EOF
要点:所以说两个模块是隔离开的,两个模块的指令工作区是不一样的。
参考链接https://my.oschina.net/foreverich/blog/1512304
一般的正向代理如堡垒机的功能就是正向代理。反之,nginx的作用就是反向代理,这样就好理解了。
和upstream搭配使用
如:
#设定负载均衡的服务器列表 #weigth参数表示权值,权值越高被分配到的几率越大 upstream hello{ server 192.168.46.145:8080 weight=1; server 192.168.46.146:8080 weight=1; } server { #侦听的80端口 listen 80; server_name localhost; #匹配以jsp结尾的,tomcat的网页文件是以jsp结尾 location / { index index.jsp; proxy_pass http://hello; #在这里设置一个代理,和upstream的名字一样 } }
反向代理最常用的是ngx_http_proxy_module模块中的proxy_pass
server { listen 80; server_name www.test.com; # 正常代理,不修改后端url的 location /some/path/ { proxy_pass http://127.0.0.1; } # 修改后端url地址的代理(本例后端地址中,最后带了一个斜线) location /testb { proxy_pass http://www.other.com:8801/; } # 使用 if in location location /google { if ( $geoip_country_code ~ (RU|CN) ) { proxy_pass http://www.google.hk; } } location /yongfu/ { # 没有匹配 limit_except 的,代理到 unix:/tmp/backend.socket:/uri/ proxy_pass http://unix:/tmp/backend.socket:/uri/;; # 匹配到请求方法为: PUT or DELETE, 代理到9080 limit_except PUT DELETE { proxy_pass http://127.0.0.1:9080; } } }
proxy_pass
后,后端服务器的url
(request_uri
)情况分析
server { listen 80; server_name www.test.com; # 情形A # 访问 http://www.test.com/testa/aaaa # 后端的request_uri为: /testa/aaaa location ^~ /testa/ { proxy_pass http://127.0.0.1:8801; } # 情形B # 访问 http://www.test.com/testb/bbbb # 后端的request_uri为: /bbbb location ^~ /testb/ { proxy_pass http://127.0.0.1:8801/; } # 情形C # 下面这段location是正确的 location ~ /testc { proxy_pass http://127.0.0.1:8801; } # 情形D # 下面这段location是错误的 # # nginx -t 时,会报如下错误: # # nginx: [emerg] "proxy_pass" cannot have URI part in location given by regular # expression, or inside named location, or inside "if" statement, or inside # "limit_except" block in /opt/app/nginx/conf/vhost/test.conf:17 # # 当location为正则表达式时,proxy_pass 不能包含URI部分。本例中包含了"/" location ~ /testd { proxy_pass http://127.0.0.1:8801/; # 记住,location为正则表达式时,不能这样写!!! } # 情形E # 访问 http://www.test.com/ccc/bbbb # 后端的request_uri为: /aaa/ccc/bbbb location /ccc/ { proxy_pass http://127.0.0.1:8801/aaa$request_uri; } # 情形F # 访问 http://www.test.com/namea/ddd # 后端的request_uri为: /yongfu?namea=ddd location /namea/ { rewrite /namea/([^/]+) /yongfu?namea=$1 break; proxy_pass http://127.0.0.1:8801; } # 情形G # 访问 http://www.test.com/nameb/eee # 后端的request_uri为: /yongfu?nameb=eee location /nameb/ { rewrite /nameb/([^/]+) /yongfu?nameb=$1 break; proxy_pass http://127.0.0.1:8801/; } access_log /data/logs/www/www.test.com.log; } server { listen 8801; server_name www.test.com; root /data/www/test; index index.php index.html; rewrite ^(.*)$ /test.php?u=$1 last; location ~ \.php$ { try_files $uri =404; fastcgi_pass unix:/tmp/php-cgi.sock; fastcgi_index index.php; include fastcgi.conf; } access_log /data/logs/www/www.test.com.8801.log; }