To block visitors from a specific country (or countries) using the IP
addresses, Nginx needs to be compiled with the GeoIP module. This module
was introduced in v0.7.63 and 0.8.6. In addition to that you need a Geo
database. Install these packages under Debian :
apt-get install geoip-database libgeoip1
We now need to edit the configuration files. Put this code in the http block
in /etc/nginx/nginx.conf. It can be also put in your vhosts config file
outside of the server block.
This code sets the variable $allow_visit to yes or no, depending on the
country of the visitor. In this example, we have set $allow_visit to no for
Egypt, France and Finland, and yes for all other countries. Alternately, if
you want to block all countries except for a few, set the default to no and
set the variable to yes for a select few. A list of country codes can be found
here.
Now we need to use the $allow_visit variable to block visitors. Inside your
server block, add this :
if ($allow_visit = no) {
return 403;
}
This will return the “403 Forbidden” page for all visitors from countries
whose country code is set to no. You can also define a custom 403 page which
is a bit more helpful than the default one.
error_page 403 /custom_403.html;
It is also possible to use this for redirecting users based on their country,
but that’ll be explored in another post.
Nginx cannot run CGI scripts natively. You have to either use the Perl-FastCGI daemon to run your Perl scripts, or for a more “native” CGI
environment, proxy the CGI scripts to a web-server that has the ability to
execute them natively. Apache, surely, will be overkill for this purpose.
Keeping in mind the low-memory requirements of most users who run Nginx, we’ll
use thttpd. thttpd is a tiny, lightweight server (only around 800 kB RSS)
which has the ability to execute CGI scripts natively.
First, let us install thttpd. thttpd hasn’t been in active development since
late 2003 (v2.25b), but there isn’t any security issue with it. The only issue
is that it doesn’t respect (or pass on) the X-Forwarded-For header. So,
if you are using Debian 5.0.X Lenny, please build from source after applying
this patch or install from the Squeeze repositories using apt-pinning (This issue was fixed in v2.25b-10) .
Assuming you are using Debian Squeeze,
apt-get install thttpd
Replace the thttpd configuration file with our own :
mv /etc/thttpd/thttpd.conf /etc/thttpd/thttpd.conf.orig
vi /etc/thttpd/thttpd.conf
Put this in the file :
host=127.0.0.1
port=8000
dir=/var/www #change this to the $document_root from nginx config file
user=www-data
cgipat=**.cgi|**.pl
logfile=/var/log/thttpd.log
pidfile=/var/run/thttpd.pid
Now let us start thttpd.
invoke-rc.d thttpd start
We also need to configure nginx to pass on the requests for CGI scripts to
thttpd. Add this to your configuration file :
location ~ \.cgi|pl$ {
proxy_pass http://127.0.0.1:8000;
include proxy.conf;
}
We also need to create a file with some configuration parameters for nginx :
The Nginx release on the Ubuntu repositories is often outdated. Igor pushes
out new versions of Nginx quite frequently and it’s understandable that the
Ubuntu package maintainers do not keep up. Also, Ubuntu only includes major
version updates with new OS releases. So, if the Nginx development branch
turns stable, and you plan on staying on your LTS (Long Term Support) version,
you might not be able to get the newer version of Nginx unless you upgrade
your OS.
So, in view of all this, Michael Lustfield and a bunch of other cool
folks maintain a Launchpad PPA where they provide the latest versions of
Nginx from both the stable and development branch. To get the latest version
of Nginx from the PPA, follow these steps :
Next, we should download and configure a FastCGI wrapper script (credit: Denis
S. Filimonov), a Debian init script to start/stop the FastCGI process, give
them the necessary permissions to be executed and set the init script to start
up the FastCGI daemon automatically.
Unlike Apache, nginx does not have any .htaccess file. Password protection is
achieved by using the Nginx HttpAuthBasic module directives in the
configuration file.
To password protect a directory called secret_folder, use the following
directives inside the server block in the configuration file for your website.
This will password protect the directory, it’s sub folders and the files
inside it. Change auth_basic_user_file directive to point to your htpasswd
file. If your are using nginx version 0.6.7 or higher, note that the filename
path is relative to directory of nginx configuration file, nginx.conf,
rather than nginx prefix directory. So, if your nginx.conf is in /etc/nginx
folder, the above code will use the htpasswd file in /etc/nginx/conf folder.
Here’s the format of the htpasswd file :
user:pass
user2:pass2:comment
user3:pass3
Passwords must be encoded by the crypt(3) function if Apache is not installed.
If Apache is installed, you can use the htpasswd program to generate the
htpasswd file:
htpasswd -b htpasswd NewUser NewPassword
If you want to use a web based utility to generate your htpasswd file, I’d
recommend the excellent .htaccess Password Generator utility from Dynamic
Drive.
In order to password protect the whole site, use the same code within location
/ block.
Enabling directory listing in a folder in nginx is
simple enough with just an autoindex on; directive inside the location
directive. You can also enable sitewide directory listing by putting it in the
server block or even enable directory access for all sites by putting it in the http block.