Nginx

How to make nginx works with Geo IP?

NGINX GeoIP Installation:

Follow these steps to install GeoIP with nginx:

wget http://geolite.maxmind.com/download/geoip/api/c/GeoIP.tar.gz

tar -zxvf GeoIP.tar.gz
cd GeoIP-1.4.8/
./configure
make
make install
echo ‘/usr/local/lib’ > /etc/ld.so.conf.d/geoip.conf
yum install gcc pcre-devel.x86_64 openssl-devel.x86_64
yum install readline-devel pcre-devel openssl-develWe are using openresty – nginx here:wget http://openresty.org/download/ngx_openresty-1.7.2.1.tar.gz
tar xzvf ngx_openresty-1.7.2.1.tar.gz
cd ngx_openresty-1.7.2.1
./configure –with-luajit –with-http_geoip_module
gmake
gmake installwget http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz -O /usr/local/share/GeoIP/GeoIP.dat.gz
gunzip /usr/local/share/GeoIP/GeoIP.dat.gz
In nginx.conf  http block – geoip_country /usr/local/share/GeoIP/GeoIP.dat;Reload nginx – nginx -s reload

Read More...

Nginx

Nginx Openresty Get/Set Cookies using LUA.

To set cookies using lua while using nginx_lua module in openresty/nginx server, use this code:

local expires = 3600 * 24  — 1 day
ngx.header[“Set-Cookie”] = “session=demo; Path=/; Expires=” .. ngx.cookie_time(ngx.time() + expires)

This will set cookie session = demo for 1 day from current time on path /.

Read More...

Nginx

Nginx Proxy requests are redirecting to upstream name.

If you are using nginx as a reverse proxy and having the redirection issues to upstream name when proxy request results in 301,302 redirects, e.g.

http://www.mind-geek.net/new.html is proxied by backend upstream i.e.

upstream backend {
server 54.21.42.13:80;
}

location / {
proxy_pass http://backend;
}

and the requests are getting ended eith http://backend/new.html, than you need to sepecify the proxy_headerr host in proxy requests, which is :

proxy_set_header Host $http_host;

so finally it becomes:

location / {
proxy_set_header Host $http_host;
proxy_pass http://backend;
}

Now all you requests and 302,301 redirects will consist valid domain name(host).

Read More...

Nginx

Nginx allow cross domain ajax requests

To enable Nginx for Cross Origin calls add this in nginx configuration for the domain  :

location /{

add_header ‘Access-Control-Allow-Origin’ ‘http://subdomain.mind-geek.net’;
add_header ‘Access-Control-Allow-Credentials’ ‘true’;
add_header ‘Access-Control-Allow-Methods’ ‘GET’;

}

That’s it, it will enable GET requests from http://subdomain.mind-geek.net to the configured domain.

Read More...