LimeSurvey Nginx rewrite rules

I just downloaded LimeSurvey, an amazing free open-source project for creating your own online surveys, and was about to install it on my own Nginx-powered server. To my surprise, there are no available Nginx rewrite rules for it on the official project website, and I didn't even find any blog post detailing such rules. So here I go again, filling the voids.

This post details:
- the Nginx rewrite rules translated directly from the provided .htaccess file, the small server configuration file containing the rewrite rules for Apache.
- what to do after installing LimeSurvey

Nginx rewrite rules

The original rewrite rules for LimeSurvey are contained in this simple .htaccess file:

<IfModule mod_rewrite.c>  
RewriteEngine on
    # if a directory or a file exists, use it directly
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    # otherwise forward it to index.php
    RewriteRule . index.php
# General setting to properly handle LimeSurvey paths
# AcceptPathInfo on

Before you copy the Nginx-compatible rules below, you should ideally try to understand what the above does. The first two lines (IfModule... RewriteEngine on) enable the rewrite rules only if the rewrite module is enabled. Normally it should be the case by default on Nginx so you don't need to worry about that.

The following lines (RewriteCond...) specify conditions for the URL to be rewritten. The first line (!-f) ensures that the requested URI doesn't really exist. For example, if the visitor requests /document.txt and that file actually exists on your server, then the URI doesn't need to be rewritten at all; the file can be served as is. The second line (!-d) does the same for folders; if the requested URI corresponds to an existing folder, it will serve it normally, by redirecting the user to its index page, or displaying an automatic index or something. Eventually if both RewriteConditions are met, then Apache will rewrite the URI by internally redirecting the request to index.php (and automatically appending the arguments to the URL).

We could write a similar script for Nginx with if's and rewrite instructions, but the best practice is to use the try_files directive:

location / {
try_files $uri $uri/ /index.php?q=$uri&$args;

What this does is:
1) Nginx attempts to serve $uri (the requested URI)
2) If the above doesn't exist, it attempts to load $uri/ (note the slash for a potential folder)
3) If the above doesn't exist, it loads /index.php?q=$uri&$args (the index.php page, transferring the request arguments too)

An average working (tested) server block for serving LimeSurvey would be:

server {
listen 80;
server_name mywebsite.com;
root /var/www/mywebsitefiles;
index index.php;
location / {
try_files $uri $uri/ /index.php?q=$uri&$args;

location ~* \.php$ {
fastcgi_index index.php;
fastcgi_param PATH_INFO $fastcgi_script_name;
include fastcgi.conf;
Make sure to replace my example values by your own; if you are unsure about the meaning of the directives you should consult the Nginx wiki. Save your configuration file and reload Nginx to apply the changes.

Note: if you allow file uploads from users, you should better secure the "location ~* \.php$" line by specifying from which folders PHP files may be executed. Otherwise your server might be at risk depending on the files you allow your users to upload (ie. if you allow them to upload PHP files).

After installing LimeSurvey

Whether you install LimeSurvey after or before applying the Nginx rewrite rules doesn't matter: the LimeSurvey install script will detect that your server isn't running Apache's mod_rewrite anyway, so it will automatically switch off the "fancy URLs" functionality.

In order to enable it, open /application/config/config.php and change the following values:
'urlFormat' => 'get', 
change this line to:
'urlFormat' => 'path',
And then:
'showScriptName' => true,
to the following line:
'showScriptName' => false,

After doing so, try visiting yourwebsite.com/admin/ and you should be taken to Limesurvey's administration panel. If your browser isn't loading the page properly you might have to clear your browser cache.

I have tested this myself and haven't run into issues so far.


Shanghai Air Quality Index Widget (from US Embassy data)

I live in Shanghai and air pollution is a major concern, especially in institutions such as schools or hospitals. It is important to give proper directives to children and elder: when the air pollution reaches a certain threshold, they should stay inside as much as possible and avoid any form of physical exercise outside.

There are mainly two indices for air quality in Shanghai: the Chinese government's data and the official website of the US Consulate in Shanghai. The latter is based on an RSS feed updated every hour.

In order to facilitate the display and comprehension of the data from that RSS feed, I have designed a widget (running under Windows XP/Vista/7/8) that is meant to display Shanghai's air quality index in real time - again, based on official data from the US Embassy. The widget was designed to be ran on display monitors that stay on 24/7, so it displays at the topmost - it always shows on top regardless of other windows. The data is automatically refreshed every hour with a timer. If you start the widget at 8:05am, it will refresh the data at 8:05am, 9:05am, 10:05am, and so on.

If you are interested in the widget, you can download it from this link. Warning: this application requires the .NET Framework 4.5 to be installed on your machine. It won't run without it!

You may also download the source code here (developped with Visual Studio Express 2012 for Windows Desktop).

Search This Blog