V
V
vruzin2014-11-13 10:55:45
Nginx
vruzin, 2014-11-13 10:55:45

How to configure Nginx + geoip + gzip Depending on the city, different files with gzip compression were loaded, if they exist?

There is a URL, for example: example.com/contacts/
There is a folder /cache/contacts/
in it files, for example:

msk.html.gz
spb.html.gz
msk-obl.html.gz
len-obl.html.gz
other.html.gz

It is necessary:
​​1. Determine from which region the user is.
2. If the user is from Moscow, then show him the msk.html.gz file
3. Set the GZIP flag
4. If such a file does not exist, then continue execution. (there, in the future, PHP will generate this file, compress it and put it in the desired folder.
I would like NGINX to immediately give a pre-prepared and compressed file, instead of constantly executing the file. (*.php or *.html does not matter, since html processed like php)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dan Ivanov, 2014-11-16
@ptchol

Haven't you tried it like this?

http { 
    ...
    geo $country {
        default        other;
        127.0.0.0/24   msk;
        10.1.0.0/16    spb;
        192.168.1.0/24 other;
    }
    map $country $filename {
        default     other.html.gz;
        msk         msk.html.gz;
        spb         spb.html.gz;
        other       other.html.gz;
    }
    ...
}

server { 
    ...
    location /contacts {
        rewrite .* $filename break;
        alias /var/www/server.com/cache/contacts/;
        gzip on;
        try_files $uri @fallback;
    }
    location @fallback {
        fastcgi_pass 127.0.0.1:9000;
    }
    ...
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question