參考地址: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/;
}
}
}
反向代理小結(jié)
location /a {
proxy_pass http://ip;
}
location /b/ {
proxy_pass http://ip/;
}
上述配置會導(dǎo)致:
/a/x -> http://ip/a/x;
/b/x -> http://ip/x;
負載均衡小結(jié)
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 會跳轉(zhuǎn)到 192.168.0.12:80或者 192.168.0.12:81
# 過程是隨機的县爬,可以通過weight參數(shù)控制權(quán)重
location /a/ {
proxy_pass http://group1/;
}
}
}