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
</IfModule>
# 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_pass 127.0.0.1:9000;
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.

Comments

Alfonso said…
Very interesting article. Gives us information on how we can write our own rules in case the software doesn't provide it.

Thanks
harnas said…
This article just saved my day!

I was migrating my installation from Apache to Nginx due to performance issues...

Many thanks!
Anonymous said…
Thanks
fnaf3 said…
Great post. i like it. feeling great when reading your post .
----
five nights at freddy's | fnaf 3 | five nights at freddy's game
baixar facebook said…
I am an avid reader who likes engaging content.
----
descargar mobogenie gratis and apply baixar facebook gratis | descargar mobogenie
SEO expert said…
Aside from writing real content material, one of the abilities that need to be determined out via those doing article writing is a manner to rewrite articles. Rewriting is the approach of the element author rewording the item while despite the fact that preserving the identical content material. This approach works first rate to keep away from plagiarism, the mortal sin dedicated through many writers. now and again, article writing takes an entire lot of time handiest for completing thorough studies. Of course maximum article directories reject devices which can be located to have reproduction content cloth fabric. Consequently, spending time on a unmarried article without a doubt to have it submitted on only a few internet net websites is probably a remarkable waste of time. this case may be avoided through way of rewriting the whole content material material material of the item. A few human beings use article spinners. however if given a better appearance, this shape of software program software application can also furthermore spin a chunk of writing however lose its feel at one thing or a few special. Doing all your article rewriting manually is the extraordinary desire. Get Best Article Rewriter
Richard Dowson said…
Programming designer Philippines isn't the main choice out there. Obviously, Asia has been one of the first reevaluating centers. Organizations likewise enlist designers in Vietnam, China, or India. While recruiting programming engineers in the Philippines offers many benefits, a few organizations may likewise decide to enhance their group with a product designer Germany to give an extra degree of mastery and variety to their improvement endeavors. Be that as it may, these days Eastern Europe (and particularly Ukraine) is picking up speed with their IT rethinking administrations>> Mobilunity

Popular posts from this blog

Affiliate module for Interspire Shopping Cart

How to fix: Outlook on iOS/iPhone: "No Internet Connection"

Dealing with Nginx 400 Bad Request HTTP errors