0

I Have a problem with nginx configuration. If user agent is "robot" - then proxy pass to another port with setting corrent uri as argument

for example : have two services localhost:5000 and localhost:6000 port all non robots pass to 5000 and robots pass to 6000 with url like :

normal https://test.page/test/test -> http://localhost:5000/test/test
robot  https://test.page/test/test -> http://localhost:6000/Page/Get?url=https://test.page/test/test

i try like this :

if ( $http_user_agent ~ 'robot' ) {
                set $request_uri "/Page/Get?url=https://test.page$request_uri";
                proxy_pass http://localhost:6000;
            }
            if ( $http_user_agent !~ 'robot' ) {
                proxy_pass http://localhost:5000;
            }

but for robot got 404

1 Answers1

0

map is a better way to perform these kinds of conditional things. Add the following to http level in nginx config:

map $http_user_agent $upstream {
    ~robot "localhost:6000/Page/Get?url=https://test.page$request_uri";
    default localhost:5000$request_uri;
}

And then use $upstream as target:

proxy_pass http://$upstream;
Tero Kilkanen
  • 34,499
  • 3
  • 38
  • 58