Score:0

Nginx robot proxy_pass to another port with change url

cn flag

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

djdomi avatar
za flag
what kind of robot do you want to block? I would redirect /robots.txt to that kind of requests
cn flag
i need redirect robots to another port and url , but not block it
Score:0
us flag

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;
cn flag
Thanks for your response , but got 502 error =(
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.