# GEOIP context problem

**URL:** <https://www.ruby-forum.com/t/geoip-context-problem/194149>\
**Category:** NGINX\
**Created:** [August 31, 2010, 6:19am UTC](https://www.ruby-forum.com/t/geoip-context-problem/194149 "2010-08-31T06:19:18Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![syle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b2d939/32.png) [@syle](https://www.ruby-forum.com/u/syle)\
**Post date:** [August 31, 2010, 6:19am UTC](https://www.ruby-forum.com/t/geoip-context-problem/194149/1 "2010-08-31T06:19:18Z")

</div>

There is a problem with current 0.8.x release of GEOIP although I assume  
it has always not worked, where these variables:  
fastcgi\_param GEOIP\_CITY\_COUNTRY\_CODE $geoip\_city\_country\_code;  
fastcgi\_param GEOIP\_CITY\_COUNTRY\_CODE3 $geoip\_city\_country\_code3;  
fastcgi\_param GEOIP\_CITY\_COUNTRY\_NAME $geoip\_city\_country\_name;  
fastcgi\_param GEOIP\_REGION $geoip\_region;  
fastcgi\_param GEOIP\_CITY $geoip\_city;  
fastcgi\_param GEOIP\_POSTAL\_CODE $geoip\_postal\_code;  
fastcgi\_param GEOIP\_CITY\_CONTINENT\_CODE $geoip\_city\_continent\_code;  
fastcgi\_param GEOIP\_LATITUDE $geoip\_latitude;  
fastcgi\_param GEOIP\_LONGITUDE $geoip\_longitude;

do not work except inside the fastcgi\_params file. The context is  
suppose to work within http as per module documentation however setting  
these variables  
within http context ends up not setting them at all.

This is currently a major issue especially in freebsd where an upgrade  
of version from port collections can wipe out fastcgi\_params file each  
time, leaving  
any webserver relying on these variables in major trouble if they were  
to forget to replace fastcgi\_params file afterwards.

Posted at Nginx Forum:

> **[GEOIP context problem](https://forum.nginx.org/read.php?2,125449,125449#msg-125449)**
>
> There is a problem with current 0.8.x release of GEOIP although I assume it has always not worked, where these variables: fastcgi\_param GEOIP\_CITY\_COUNTRY\_CODE $geoip\_city\_country\_code; fastcgi\_param GEOIP\_CITY\_COUNTRY\_CODE3...

---

<div class="post-metadata">

**Author:** ![syle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b2d939/32.png) [@syle](https://www.ruby-forum.com/u/syle)\
**Post date:** [August 31, 2010, 9:38am UTC](https://www.ruby-forum.com/t/geoip-context-problem/194149/2 "2010-08-31T09:38:19Z")

</div>

On Tue, Aug 31, 2010 at 12:18:13AM -0400, syle wrote:

> fastcgi\_param GEOIP\_LONGITUDE $geoip\_longitude;  
> to forget to replace fastcgi\_params file afterwards.  
> Could you test following patch against current version of  
> ports/www/nginx-devel ?

Thank you.

---

<div class="post-metadata">

**Author:** ![syle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b2d939/32.png) [@syle](https://www.ruby-forum.com/u/syle)\
**Post date:** [August 31, 2010, 11:38am UTC](https://www.ruby-forum.com/t/geoip-context-problem/194149/3 "2010-08-31T11:38:55Z")

</div>

Hello!

On Tue, Aug 31, 2010 at 12:18:13AM -0400, syle wrote:

> fastcgi\_param GEOIP\_LONGITUDE $geoip\_longitude;
> 
> do not work except inside the fastcgi\_params file. The context is  
> suppose to work within http as per module documentation however setting  
> these variables  
> within http context ends up not setting them at all.

Most likely you attempted to do something like

http {  
fastcgi\_param …  
…  
server {  
…  
location … {  
fastcgi\_param …  
…  
}  
}  
}

This won’t work due to inheritance rules for array-type  
directives, see here:

> **[GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki...](https://github.com/nginxinc/nginx-wiki)**
>
> ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com

“Directives set in current level clear any previously defined  
directives for the current level.”

You have to define all fastcgi\_param directives at the same level,  
i.e.

location … {  
fastcgi\_param …  
fastcgi\_param …  
…  
}

or

http {  
fastcgi\_param …  
fastcgi\_param …  
…  
}

Maxim D.

---

<div class="post-metadata">

**Author:** ![syle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b2d939/32.png) [@syle](https://www.ruby-forum.com/u/syle)\
**Post date:** [August 31, 2010, 5:40pm UTC](https://www.ruby-forum.com/t/geoip-context-problem/194149/4 "2010-08-31T17:40:09Z")

</div>

Actually I’ll post one of my server directives so we can see if there is  
any inheritance problems like  
you suggested, i have about 50 server directives the same as this:  
( I know it seems quite bloated but I didn’t read any way I could set  
php and perl/cgi scripts outside of  
server directivesfor all the vhosts)

server {  
listen 80;#bind to ipv4  
listen [::]:80; #bind to ipv6  
server\_name x.x.x;  
root /xxx/xxx/xxx;  
access\_log /usr/nginx\_logs/xxx\_xxx\_log main;  
error\_log /usr/nginx\_logs/xxx\_xxx\_error;  
index index.php index.pl index.html;  
error\_page 404 /xxx/xxx.pl;  
error\_page 500 502 503 504 /xxx/xxx.pl;  
location ~ .php$ {  
fastcgi\_index index.php;  
fastcgi\_pass unix:/tmp/cgi2.sock;  
fastcgi\_param SCRIPT\_FILENAME  
$document\_root$fastcgi\_script\_name;  
include fastcgi\_params;  
}  
location ~ .pl$ {  
fastcgi\_pass unix:/tmp/cgi.sock;  
fastcgi\_param SCRIPT\_FILENAME  
$document\_root$fastcgi\_script\_name;  
include fastcgi\_params;  
}  
}

---- i do see params being set in location as you said would not work,  
So are you saying i would have to add all those GEO\_IP directives to all  
50 server directives ☹ perhaps maybe can suggest a way to move these  
perl and php directives outside server directives into http context to  
work with GEOIP? or I have to hope for a freebsd commit to stop removing  
that file I suppose.

Posted at Nginx Forum:

> **[Re: GEOIP context problem](https://forum.nginx.org/read.php?2,125449,125687#msg-125687)**
>
> There is a problem with current 0.8.x release of GEOIP although I assume it has always not worked, where these variables: fastcgi\_param GEOIP\_CITY\_COUNTRY\_CODE $geoip\_city\_country\_code; fastcgi\_param GEOIP\_CITY\_COUNTRY\_CODE3...

---

<div class="post-metadata">

**Author:** ![syle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b2d939/32.png) [@syle](https://www.ruby-forum.com/u/syle)\
**Post date:** [August 31, 2010, 5:43pm UTC](https://www.ruby-forum.com/t/geoip-context-problem/194149/5 "2010-08-31T17:43:40Z")

</div>

Hello!

On Tue, Aug 31, 2010 at 10:58:23AM -0400, syle wrote:

> fastcgi\_param GEOIP\_CITY\_COUNTRY\_CODE3 $geoip\_city\_country\_code3;  
> }  
> And you likely have “include fastcg\_params;” in your locations  
> blocks. This is exactly what I was talked about. You have to  
> set all fastcgi\_param’s at the same level.

Maxim D.

---

<div class="post-metadata">

**Author:** ![syle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b2d939/32.png) [@syle](https://www.ruby-forum.com/u/syle)\
**Post date:** [August 31, 2010, 5:00pm UTC](https://www.ruby-forum.com/t/geoip-context-problem/194149/6 "2010-08-31T17:00:55Z")

</div>

Thankyou for repliies, as far as freebsd guy, reading your patch just  
suggests not removing fast fastcgi\_params file which would be a nice  
commit  
till this is resolved.

Maxim:

I was doing strictly in http context as module was documented nothing  
else and it was not recogised at all ie:

http {  
#GEOIP, can parse ENV headers now for where they are located 🙂  
geoip\_city /usr/local/share/GeoIP/GeoLiteCity.dat;

fastcgi\_param GEOIP\_CITY\_COUNTRY\_CODE $geoip\_city\_country\_code;  
fastcgi\_param GEOIP\_CITY\_COUNTRY\_CODE3 $geoip\_city\_country\_code3;  
fastcgi\_param GEOIP\_CITY\_COUNTRY\_NAME $geoip\_city\_country\_name;  
fastcgi\_param GEOIP\_REGION $geoip\_region;  
fastcgi\_param GEOIP\_CITY $geoip\_city;  
fastcgi\_param GEOIP\_POSTAL\_CODE $geoip\_postal\_code;  
fastcgi\_param GEOIP\_CITY\_CONTINENT\_CODE $geoip\_city\_continent\_code;  
fastcgi\_param GEOIP\_LATITUDE $geoip\_latitude;  
fastcgi\_param GEOIP\_LONGITUDE $geoip\_longitude;

```
#virtual hosting---all server {} directives
include /usr/local/etc/nginx/vhosts/*;

```

}

Posted at Nginx Forum:

> **[Re: GEOIP context problem](https://forum.nginx.org/read.php?2,125449,125670#msg-125670)**
>
> There is a problem with current 0.8.x release of GEOIP although I assume it has always not worked, where these variables: fastcgi\_param GEOIP\_CITY\_COUNTRY\_CODE $geoip\_city\_country\_code; fastcgi\_param GEOIP\_CITY\_COUNTRY\_CODE3...

---

<div class="post-metadata">

**Author:** ![syle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b2d939/32.png) [@syle](https://www.ruby-forum.com/u/syle)\
**Post date:** [August 31, 2010, 5:51pm UTC](https://www.ruby-forum.com/t/geoip-context-problem/194149/7 "2010-08-31T17:51:52Z")

</div>

Hello!

On Tue, Aug 31, 2010 at 11:38:38AM -0400, syle wrote:

> ```
> listen [::]:80; #bind to ipv6
> fastcgi_param SCRIPT_FILENAME
> 
> ```
> 
> ---- i do see params being set in location as you said would not work,  
> So are you saying i would have to add all those GEO\_IP directives to all  
> 50 server directives ☹ perhaps maybe can suggest a way to move these  
> perl and php directives outside server directives into http context to  
> work with GEOIP? or I have to hope for a freebsd commit to stop removing  
> that file I suppose.

As long as all fastcgi\_param’s are identical in all locations -  
you are free to move all of them to http level.

Alternatively - nothing stops you from using another include file  
for these params.

Maxim D.

---

<div class="post-metadata">

**Author:** ![syle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b2d939/32.png) [@syle](https://www.ruby-forum.com/u/syle)\
**Post date:** [September 1, 2010, 4:44am UTC](https://www.ruby-forum.com/t/geoip-context-problem/194149/8 "2010-09-01T04:44:36Z")

</div>

Sergey:

I will just do following for now, so maybe overwriting fastcgi\_params  
might not be such a bad thing on freebsd since we will always get  
a new copy of anything that changes automatically, i’ve just thrown the  
geoip values in another include as follows:

```
location ~ \.php$ {
    #fastcgi_pass 127.0.0.1:9000;
    fastcgi_pass unix:/tmp/cgi2.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME

```

$document\_root$fastcgi\_script\_name;  
include fastcgi\_params;  
include fastcgi\_params\_geoip;  
}  
location ~ .pl$ {  
fastcgi\_pass unix:/tmp/cgi.sock;  
fastcgi\_param SCRIPT\_FILENAME  
$document\_root$fastcgi\_script\_name;  
include fastcgi\_params;  
include fastcgi\_params\_geoip;  
}

Posted at Nginx Forum:

> **[Re: GEOIP context problem](https://forum.nginx.org/read.php?2,125449,125883#msg-125883)**
>
> There is a problem with current 0.8.x release of GEOIP although I assume it has always not worked, where these variables: fastcgi\_param GEOIP\_CITY\_COUNTRY\_CODE $geoip\_city\_country\_code; fastcgi\_param GEOIP\_CITY\_COUNTRY\_CODE3...

---

<div class="post-metadata">

**Author:** ![syle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b2d939/32.png) [@syle](https://www.ruby-forum.com/u/syle)\
**Post date:** [September 1, 2010, 4:26am UTC](https://www.ruby-forum.com/t/geoip-context-problem/194149/9 "2010-09-01T04:26:27Z")

</div>

Unfortunately the location directive only works in server context, so  
fastcgi\_param SCRIPT\_FILENAME is always going to loose  
any http context defined vars for executing php and perl scripts,  
basically where they are needed.

Would be nice to be able to set location in http context for those perl  
and php scripts as I have them, making them server wide for vhosts.

I’ll take your other suggestion for now and just use another include  
file so I stop having this issue with freebsd overwriting that file.

Thank-you.

Posted at Nginx Forum:

> **[Re: GEOIP context problem](https://forum.nginx.org/read.php?2,125449,125881#msg-125881)**
>
> There is a problem with current 0.8.x release of GEOIP although I assume it has always not worked, where these variables: fastcgi\_param GEOIP\_CITY\_COUNTRY\_CODE $geoip\_city\_country\_code; fastcgi\_param GEOIP\_CITY\_COUNTRY\_CODE3...

---

<div class="post-metadata">

**Author:** ![syle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b2d939/32.png) [@syle](https://www.ruby-forum.com/u/syle)\
**Post date:** [September 1, 2010, 8:03am UTC](https://www.ruby-forum.com/t/geoip-context-problem/194149/10 "2010-09-01T08:03:36Z")

</div>

Hi, syle.

Did you test my changes for www/nginx-devel port?  
Does everything works fine for you?

–  
Sergey A. Osokin  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

On Tue, Aug 31, 2010 at 10:43:38PM -0400, syle wrote:

> ```
> fastcgi_index index.php;
> include fastcgi_params_geoip;
> }
> 
> ```
> 
> Posted at Nginx Forum: [http://forum.nginx.org/read.php?2,125449,125883#msg-125883](http://forum.nginx.org/read.php?2,125449,125883#msg-125883)
> 
> * * *
> 
> nginx mailing list  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [mailman.nginx.org archive](http://nginx.org/mailman/listinfo/nginx)

–  
Sergey A. Osokin,  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![syle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b2d939/32.png) [@syle](https://www.ruby-forum.com/u/syle)\
**Post date:** [October 21, 2010, 11:51pm UTC](https://www.ruby-forum.com/t/geoip-context-problem/194149/11 "2010-10-21T23:51:32Z")

</div>

This really sucks. It’d be MUCH better if you could simply inherit the  
fastcgi\_param. I’ve now come across this a bunch of times with  
different projects. It’d be nice for example to pass DEBUG 1 or DEBUG 0  
to the fastcgi handler.

The documentation makes sense after you read it a bunch of times, but at  
first read, it makes it seem like you can inherit portions of  
fastcgi\_param.

Posted at Nginx Forum:

> **[Re: GEOIP context problem](https://forum.nginx.org/read.php?2,125449,143261#msg-143261)**
>
> There is a problem with current 0.8.x release of GEOIP although I assume it has always not worked, where these variables: fastcgi\_param GEOIP\_CITY\_COUNTRY\_CODE $geoip\_city\_country\_code; fastcgi\_param GEOIP\_CITY\_COUNTRY\_CODE3...
