1

I'm very new to nginx and hit the wall configuring simple redirection. Here is my very simple config trying redirection:

server { listen 80 default_server; set $mobile "false"; if ($http_user_agent ~* '(phone|droid)') { set $mobile "true"; } if ($mobile = true) { return 301 http://X.X.X.X/mobile$request_uri; } location /mobile { include uwsgi_params; uwsgi_pass unix:/var/www/video_m/video.sock; } location / { include uwsgi_params; uwsgi_pass unix:/var/www/video/video.sock; } } 

When go to site from desktop everything is ok and my request is going to uwsgi. But from mobile device I get an error from browser ERR_TOO_MANY_REDIRECTS and request url looks like http://X.X.X.X/mobile/mobile/mobile/mobile/mobile/mobile/mobile/mobile

There is obviously something essential and possibly very simple piece that I missing. Please help me out.

2
  • Try 'break;' in the if blocks - just below the set $mobile "true"; Commented Jan 20, 2016 at 14:04
  • It didn’t work for me. I still get redirect loop. Commented Jan 21, 2016 at 8:43

1 Answer 1

1

You have created a loop. The first time it hits the if/return a /mobile prefix is added. Then the URI is presented again and it hits the same statement adding another /mobile prefix, and this continues to be repeated.

To break the loop, you need to protect the if/return within a path that is not taken once the /mobile prefix is added the first time.

Maybe:

server { listen 80 default_server; location /mobile { include uwsgi_params; uwsgi_pass unix:/var/www/video_m/video.sock; } location / { if ($http_user_agent ~* '(phone|droid)') { return 301 $scheme://$host/mobile$request_uri; } include uwsgi_params; uwsgi_pass unix:/var/www/video/video.sock; } } 
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. I did not know that after adding a prefix the request evaluates again from the beginning.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.