本文介绍了如何基于Nginx中的请求URL重定向到特定的上游服务器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将Nginx用作5个应用服务器的负载平衡器.

I'm using Nginx as a load balancer for my 5 app servers.

我想根据请求URL重定向到特定服务器,例如:

I'd like to redirect to specific servers based on the request URL, for instance:

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不太熟悉-任何人都有一些线索?

I have no idea how to do this, since I'm not very familiar with Nginx - any one has some clues?

推荐答案

阅读文档,进行全面介绍里面有很好的解释.特别是入门指南说明了基础知识.您最终会得到:

Read the documentation, eveything is well explained in it. There's particularly a beginner's guide explaining basics. You would end up with :

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中的请求URL重定向到特定的上游服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 06:09