我使用Nginx作为我的5个应用服务器的负载均衡器。
我想根据请求网址重定向到特定服务器,例如:
acme.com/category/* => Server #1
acme.com/admin/* => Server #2
api.acme.com => Server #3
Fallback for any other URL => Server #4, #5
我的配置如下:
upstream backend {
least_conn;
server 10.128.1.1;
server 10.128.1.2;
server 10.128.1.3;
server 10.128.1.4;
server 10.128.1.5;
}
server {
listen 80;
server_name _;
location / {
proxy_set_header Host $host;
proxy_pass http://backend;
}
}
我不知道怎么做,因为我对Nginx不太熟悉 - 任何人都有一些线索?
答案 0 :(得分:16)
阅读the documentation,其中包含了很好的解释。特别是beginner's guide解释基础知识。你最终会得到:
upstream backend {
least_conn;
server 10.128.1.4;
server 10.128.1.5;
}
server {
server_name _;
location / {
proxy_set_header Host $host;
proxy_pass http://backend;
}
}
server {
server_name acme.com;
location /admin/ {
proxy_set_header Host $host;
proxy_pass http://10.128.1.2;
}
location /category/ {
proxy_set_header Host $host;
proxy_pass http://10.128.1.1;
}
location / {
proxy_set_header Host $host;
proxy_pass http://backend;
}
}
server {
server_name api.acme.com;
location / {
proxy_set_header Host $host;
proxy_pass http://10.128.1.3;
}
}
答案 1 :(得分:3)
您还需要重写URL,否则/将转发到后端服务器
location /admin/ {
rewrite ^/admin^/ /$1 break;
proxy_pass http://10.128.1.2;
}