参考地址:https://www.bilibili.com/video/BV1Bx411Z7Do
http{
server {
listen 80;
server_name localhost;
default_type text/html;
location / {
echo "hello";
}
# 反向代理
# 这个时候访问 localhost/a, 实际访问的是 http://192.168.0.1:80/a
location /a {
proxy_pass http://192.168.0.1:80;
}
# 这个时候访问 localhost/a/, 实际访问的是 http://192.168.0.1:80/
location /a/ {
proxy_pass http://192.168.0.1:80/;
}
}
}
反向代理小结
location /a {
proxy_pass http://ip;
}
location /b/ {
proxy_pass http://ip/;
}
上述配置会导致:
/a/x -> http://ip/a/x;
/b/x -> http://ip/x;
负载均衡小结
http{
upstream group1 {
server 192.168.0.12:80 weight=10;
server 192.168.0.12:81 weight=1;
}
server {
listen 80;
server_name localhost;
default_type text/html;
location / {
echo "hello";
}
# 这个时候,访问 localhost 会跳转到 192.168.0.12:80或者 192.168.0.12:81
# 过程是随机的,可以通过weight参数控制权重
location /a/ {
proxy_pass http://group1/;
}
}
}