Report this

What is the reason for this report?

Nginx - Having probelem for indexing static folder from multiple subdirectories, rewrite for GeoIp

Posted on March 26, 2021

I am building a static site where the user will see a different index.html file based on their country.

My plan is to use GeoIp module in nginx where, if a person from country 1 visits my domain www.example.com his URL will be rewritten to www.example.com/country1 and its static site data will be served the from folder /var/www/site/country1/index.html . Similar logic will be applied to country2 and country3. If the person does not belong to these 3 countries, he will see the default page located in /var/www/site/index.html

But the problem is I am not able to find a solution for this. Sometimes I see 404 error, sometimes it shows 500 error indicating multiple internal redirections and sometimes it only shows the /var/www/site/country1/index.html file without any styling i.e. Not able to access any asset files located in /var/www/site/country1/assets.

Here is how my nginx.conf looks like ,

user                 www-data;
pid                  /run/nginx.pid;
worker_processes     auto;
worker_rlimit_nofile 65535;
error_log /var/log/nginx/error.log;
include /usr/share/nginx/modules/*.conf;
load_module modules/ngx_http_geoip2_module.so;
load_module modules/ngx_pagespeed.so;
load_module modules/ngx_http_brotli_filter_module.so;
load_module modules/ngx_http_brotli_static_module.so;
events {
    multi_accept       on;
    worker_connections 65535;
}
http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for" "$http_x-webp-convert-log" ';
    charset              utf-8;
    sendfile             on;
    tcp_nopush           on;
    tcp_nodelay          on;
    server_tokens        off;
    log_not_found        off;
    types_hash_max_size  2048;
    client_max_body_size 16M;
    include              mime.types;
    default_type         application/octet-stream;
    access_log           /var/log/nginx/access.log;
    error_log            /var/log/nginx/error.log warn;
    include              /etc/nginx/conf.d/*.conf;
    include              /etc/nginx/sites-enabled/*;
     server {
                listen 80 ;
                server_name _;
                deny all;
                return 444;

                location / { 
                }
                error_page 404 /404.html;
                    location = /40x.html {
                }
                error_page 500 502 503 504 /50x.html;
                    location = /50x.html {
                }
            }
}

And Here is my Configuration file for the website ,

geoip2 /usr/share/GeoIP/GeoLite2-Country.mmdb {
       $geoip2_data_country_iso_code country iso_code;
    }
    map $geoip2_data_country_iso_code $redirect_country  {
            default other;
            GB      country1 ;
            US      country2 ;
            IN      country3 ;
    }

server {

    listen 80;
            server_name www.example.com;
            index index.html;
            root /var/www/static/www.example.com;
            client_max_body_size 50M;   
            include /etc/nginx/pagespeed.conf;
            include /etc/nginx/conf.d/security.conf ;
         
                        
                        if ($redirect_country = country1) 
                        {          
                                    #This  is where i am making the changes
                        
                                    #rewrite "^.*$" /country1 break ;
                                    #rewrite ^ https://www.example.com/country1$uri  break;
                                    #rewrite ^ https://www.example.com/country1 break ;
                                    #return 444;
                                    #rewrite ^ $scheme://$server_name/country1$request_uri break; 
                                    #rewrite ^/(.*)$ $scheme://www.example.com$uri break;
                                    #return 301  https://www.example.com/country1/index.html break ; 
                                    #rewrite ^ https://www.example.com/(.*) https://www.example.com/country1 $1  break; 
                                    #rewrite ^/(.*)$ https://www.example.com/country1/$1 redirect;
                                    # rewrite "^.*$" /country1/country1.html$uri break; 

                                    rewrite "^.*$" /country1$request_uri break; 
                        }

                        if ($redirect_country = country3) 
                        {
                                    rewrite "^.*$" /country3$uri break ;

                        }

                        if ($redirect_country = country2) 
                        {                          
                                    rewrite "^.*$" /country2$uri break ; 
                        }
                        
                        if ($redirect_country = other) 
                        {
                                    rewrite "^.*$"  $request_uri break ;
                             
                        }
                        
        access_log /var/log/nginx/sg.foodlisa.tk.access.log;
        error_log /var/log/nginx/sg.foodlisa.tk.error.log;
                    

        location  / 
                        {   
                            #root /var/www/static/www.example.com;
                            # try_files $uri  /index.html;
                            #try_files $uri = return 404;
                            
                            try_files $uri $uri/ /index.html =404;           
                        }

        location /country1
                        {   
                            #This  is where i am making the changes
                        
                            index country1.html ;
                            root /var/www/static/www.example.com/country1/;
                            #try_files $uri  /country1/index.html;
                            #try_files $uri /index.html;
                            #break;
                            autoindex off;
                            # try_files $uri $uri/ /var/www/static/www.example.com/country1/index.html?q=$uri&$args;
                            try_files $uri $uri/ /index.html =404;
                        }


        location /country2
                        {
                        root /var/www/static/www.example.com/country2;
                            index index.html ;

                        }



        location /country3
                        {
                        root /var/www/static/www.example.com/country3;
                            index index.html ;
                            try_files $uri /index.html;

                        }


        location /sw.js 
                        {
                            add_header Cache-Control "no-cache";
                            proxy_cache_bypass $http_pragma;
                            proxy_cache_revalidate on;
                            expires off;
                            access_log off;
                        }

        location ~ \.css 
                        {
                            add_header  Content-Type    text/css;
                        }

        location ~ \.js 
                        {
                            add_header  Content-Type    application/x-javascript;
                        }
        
}



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

To solve your problem, you can try the following example of an Nginx configuration:

  1. server {
  2. listen 80;
  3. server_name www.example.com;
  4. root /var/www/site;
  5. index index.html;
  6. location / {
  7. try_files $uri $uri/ =404;
  8. }
  9. location ~ ^/(country1|country2|country3)$ {
  10. try_files /$1/index.html$1/ =404;
  11. }
  12. location ~ ^/(country1|country2|country3)/assets {
  13. try_files /$1/$uri =404;
  14. }
  15. }

In the example above, the server block listens to requests targeting the www.example.com domain on port 80. The root directive is set to the /var/www/site directory, and the index directive is set to `index.html.

Hope that this helps!

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.