Too many redirects

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Too many redirects

Tseveendorj Ochirlantuu
Hi,
I configured http (www, non-www) to https (non-www) and https (www) to
https (non-www). It is working fine. But I need to add geoip redirection
in location / but I got Too many redirect.

This server is behind load balancer. LB is redirecting 80 to 81 and 443
to 80.
server {
         listen  81;
         server_name www.example.com example.com;
         return  301 https://example.com$request_uri;
}
server {
         listen  80;
         server_name www.example.com;
         return  301 https://example.com$request_uri;
}
server {
         listen   80; ## listen for ipv4; this line is default and implied
         server_name example.com;

         ...
}

I added geoip redirection in

nginx.conf
         geoip_country /usr/share/GeoIP/GeoIP.dat;

sites-enabled/example.com

         location / {

                 # AWS load balancer access log off
                 if ($ignore_ua) {
                         access_log off;
                         return 200;
                 }

                 index index.html;
                 if ($geoip_country_code != "JP") { return 301
https://example.com/en/; }
         }
         location = /en/ {
                 index index.html;
                 try_files $uri $uri/ =404;
         }

I'm trying to if request from other than JP request go to /en/ if not
/index.html

BR,
Tseveen





_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx
Reply | Threaded
Open this post in threaded view
|

Re: Too many redirects

Francis Daly
On Thu, Sep 14, 2017 at 11:54:23PM +0800, tseveendorj wrote:

Hi there,

>         location / {
>                 index index.html;
>                 if ($geoip_country_code != "JP") { return 301
> https://example.com/en/; }
>         }

>         location = /en/ {

Change that to "location /en/ {" or "location ^~ /en/ {".

>                 index index.html;
>                 try_files $uri $uri/ =404;
>         }
>
> I'm trying to if request from other than JP request go to /en/ if
> not /index.html

You request /en/, which is processed in the second location. That does
an internal redirect to /en/index.html, which is processed in the first
location and returns a 301 redirect to /en/, so you have a loop.

Change "location =" to break the loop, so that the request to
/en/index.html is not handled in the first location.

        f
--
Francis Daly        [hidden email]
_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx
Reply | Threaded
Open this post in threaded view
|

Re: Too many redirects

Tseveendorj Ochirlantuu
Thank you Francis Daly. It works.

On 15 Sep 2017 5:45 am, "Francis Daly" <[hidden email]> wrote:
On Thu, Sep 14, 2017 at 11:54:23PM +0800, tseveendorj wrote:

Hi there,

>         location / {
>                 index index.html;
>                 if ($geoip_country_code != "JP") { return 301
> https://example.com/en/; }
>         }

>         location = /en/ {

Change that to "location /en/ {" or "location ^~ /en/ {".

>                 index index.html;
>                 try_files $uri $uri/ =404;
>         }
>
> I'm trying to if request from other than JP request go to /en/ if
> not /index.html

You request /en/, which is processed in the second location. That does
an internal redirect to /en/index.html, which is processed in the first
location and returns a 301 redirect to /en/, so you have a loop.

Change "location =" to break the loop, so that the request to
/en/index.html is not handled in the first location.

        f
--
Francis Daly        [hidden email]
_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx


_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx