我将Nginx用作5个应用服务器的负载平衡器。
我想根据请求URL重定向到特定服务器,例如:
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不太熟悉-任何人都有一些线索?
最佳答案
阅读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;
}
}
关于nginx - 如何基于Nginx中的请求URL重定向到特定的上游服务器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27220678/