0

Scenario :

Web requests are coming to nginx on port 80. I need to forward requests on basis of URL parameter.

If URL contains userId=foo anywhere in URL then it must got to Server A

and If URL contains userId=bar anywhere in URL then it must got to Server B

What configuration option can I use to achieve this?

Izzy
  • 786
  • 2
  • 8
  • 29
Abhishek
  • 3
  • 3

1 Answers1

0

/etc/nginx/sites-available/default

 server {
    listen      80;
    server_name example.com;

    location ~ userId=foo {
         return     301 http://domainOfServerA$request_uri;
    }

    location  ~ userId=bar {
        return      301 http://domainOfServerB$request_uri;
    }
 }

Anyway if bar/foo is a variable, you should write regexp for that.

pinGu
  • 140
  • 1
  • 1
  • 7
  • If any "?" comes in between the URL then this configuration failed. Sample URL http://1.1.1.1:8080/servlet/com.test?pass=test&userId=bar. Please suggest a config for it – Abhishek Sep 21 '15 at 06:42
  • Whatever after "?" isn't part of the URL, so you can't handle with nginx. I think you should do redirect with php. – pinGu Sep 23 '15 at 22:54