can t make nginx try files to work with default index html

+2 votes

I want to redirect users based on their IP with %geoip_country_code module of nginx.

Configuration:

server {
      listen 80;
      gzip on;
      server_name example.com;
      root html/example;
      location / {
        index index.html;
        try_files /$geoip_country_code/index.html /index.html;
      }
      location ~* \.(gif|jpg|jpeg|png|js|css)$ { }
  }

I just want to redirect the users based on their country and it is working perfectly fine for my own country but if I try and open from a different country it shows internal server error. It's not going to the default index page. Am I doing soemthing wrong here?

Apr 6, 2018 in DevOps on Cloud by shubham
• 7,340 points
4,496 views

3 answers to this question.

0 votes

You can set a default action in try_files statement. It's the lst element in the try_files which could be either set to a response code or a URI. Here the /index.html starts looking for a new location to process the request and returns to the start, creating a redirection loop.

You can try making /index.html a file term:

try_files /$geoip_country_code/index.html /index.html =404;

Here the =404 is never tried because /index.html is always there.

Or you can try the generic sol.

try_files /$geoip_country_code$uri $uri /index.html;

Read through this document

answered Apr 6, 2018 by DareDev
• 6,890 points
0 votes

try_files tries the literal path you specify in relation to the defined root directive and sets the internal file pointer. If you use for instance try_files /app/cache/ $uri @fallback; with index index.php index.html; then it will test the paths in this order:

  1. $document_root/app/cache/index.php
  2. $document_root/app/cache/index.html
  3. $document_root$uri
answered Oct 18, 2018 by Kalgi
• 52,360 points
0 votes

Here's another convenient use of try_files, as unconditional redirects to named locations. The named locations are effectively acting as subroutines, saving duplication of code. When the first argument to try_files is "_" the fallback redirect is always taken.

    location =/wp-login.php { try_files _ @adminlock; }
    location ^~ /wp-admin/  { try_files _ @adminlock; }
    location @adminlock  {
            allow 544.23.310.198;
            deny all;
            try_files _ @backend;
            # wp-admin traffic is tiny so ok to send all reqs to backend 
    }
    location ~ \.php {  try_files _ @backend; }
    location / { try_files $uri $uri/ =403; }
    location @backend {
            fastcgi_pass 127.0.0.1:9000;
            include snippets/fastcgi-php.conf;
    }
answered Oct 18, 2018 by Hannah
• 18,570 points

Related Questions In DevOps on Cloud

0 votes
1 answer
0 votes
1 answer

How to deploy compiled elements Azure Web App with Appveyor?

Add the following BeforeBuild target to Web ...READ MORE

answered Jul 12, 2018 in DevOps on Cloud by Kalgi
• 2,680 points
610 views
0 votes
2 answers

Nginx Request redirection one container to another

try adding a / at the end ...READ MORE

answered Oct 25, 2018 in DevOps on Cloud by Haseeb
2,481 views
+1 vote
1 answer
+15 votes
2 answers

Git management technique when there are multiple customers and need multiple customization?

Consider this - In 'extended' Git-Flow, (Git-Multi-Flow, ...READ MORE

answered Mar 27, 2018 in DevOps & Agile by DragonLord999
• 8,450 points
3,460 views
+4 votes
2 answers

unable to get subdomain of URL in NGINX

You just need to add .+ after ...READ MORE

answered Oct 11, 2018 in DevOps on Cloud by lina
• 8,220 points
4,434 views
+2 votes
2 answers
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP