nginx反向代理和负载均衡
一、nginx匹配
1、level0,完全匹配,级别最高,使用=”,完全等于这个内容才能进入,如下:localhost:80/a进入index1,其他会进入index
worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; server { keepalive_requests 100; listen 80; #listen 443 ssl; location / { proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://dev.mypro.org:8080/index; proxy_read_timeout 350; } location = /a { proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://dev.mypro.org:8080/index1; proxy_read_timeout 350; } } }
2、level1,模糊匹配,级别次低,使用“^~”,level0匹配不到会选择level1匹配,如下:localhost:80/a会访问index1,localhost:80/a/b会访问index3,localhost:80/a/c会访问index2,localhost:80/b会访问index
worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; server { keepalive_requests 100; listen 80; #listen 443 ssl; location / { proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://dev.mypro.org:8080/index; proxy_read_timeout 350; } location = /a { proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://dev.mypro.org:8080/index1; proxy_read_timeout 350; } location ^~ /a { proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://dev.mypro.org:8080/index2; proxy_read_timeout 350; } location ^~ /a/b { proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://dev.mypro.org:8080/index3; proxy_read_timeout 350; } } }
3、level2,正则匹配,级别较低,使用“~”,level0和level1都匹配不到选择level2匹配,\w+$是匹配任意个字母数字下划线,例如:localhost:80/ab123_1访问index2,localhost:80/b123_访问index4,正则多个匹配成功则按照顺序
worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; server { keepalive_requests 100; listen 80; #listen 443 ssl; location / { proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://dev.mypro.org:8080/index; proxy_read_timeout 350; } location = /a { proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://dev.mypro.org:8080/index1; proxy_read_timeout 350; } location ^~ /a { proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://dev.mypro.org:8080/index2; proxy_read_timeout 350; } location ^~ /a/b { proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://dev.mypro.org:8080/index3; proxy_read_timeout 350; } location ~ ^/\w+$ { proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://dev.mypro.org:8080/index3; proxy_read_timeout 350; } } }
二、反向代理
1、反向代理 proxy_pass,访问localhost:80/a,满足server_name的localhost,监听的80端口,匹配到/a,会转到http://dev.mypro.org:8080/index1
worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; server { keepalive_requests 100; listen 80; server_name localhost; default_type text/html; location / { proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://dev.mypro.org:8080/index; proxy_read_timeout 350; } location = /a { proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://dev.mypro.org:8080/index1; proxy_read_timeout 350; } } }
三、负载均衡
1、负载均衡服务器分配 upstream,假如有三个服务器192.168.100.1,192.168.100.2,192.168.100.3三个服务器,分发到这三个服务器上
worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; upstream dev.mypro.org { server 192.168.100.1:8080/index; server 192.168.100.2:8080/index; server 192.168.100.3:8080/index; } server { keepalive_requests 100; listen 80; server_name localhost; default_type text/html; location / { proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://dev.mypro.org/index; proxy_read_timeout 350; } location = /a { proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://dev.mypro.org/index1; proxy_read_timeout 350; } } }
2、负载均衡权重 weight,按照权重分发请求到服务器,数值越大,权重越大,转发到这个服务器的概率就大
worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; upstream dev.mypro.org { server 192.168.100.1:8080/index weight=1; server 192.168.100.2:8080/index weight=2; server 192.168.100.3:8080/index weight=2; } server { keepalive_requests 100; listen 80; server_name localhost; default_type text/html; location / { proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://dev.mypro.org; proxy_read_timeout 350; } location = /a { proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://dev.mypro.org/index1; proxy_read_timeout 350; } } }