Dec 182012

There is already a nice port of the rewrite rules for lighttpd available somewhere on the gallery2 site.
I found it quite a challenge to get nginx to work with gallery2 for several reasons one of them being that there was no written example of working NGINX rules.
Gallery2 allows you to control which URLs you want to rewrite. Here I will rewrite a few of them as an example of the overall implementation. Anyone looking to do this can simply adapt for their own use case. Just a note that you should adapt and test the setup instead of copy pasting it as is. Lets get right to it then.


server {
        listen       IP_ADDRESS;
        server_name  HOST_NAME;

    rewrite_log on;
    root        /path/to/gallery2;
    index       index.php index.html index.htm main.php;

#file upload size that you have configured in PHP and G2
        client_max_body_size 2m;
        client_body_buffer_size 256k;

        location /v/
    if ($request_uri !~ /main.php)
        rewrite ^/v/(.*)$ /main.php?g2_view=core.ShowItem&g2_path=$1 last;

location /d/

   if ($request_uri !~ /main.php)

rewrite /d/(d+)-(d+)/([^/?]+)  /main.php?g2_view=core.DownloadItem&g2_itemId=$1&g2_serialNumber=$2&g2_fileName=$3


        location /lib/yui/ {
alias /path/to/gallery2/lib/yui/;

## Dynamic Albums Plugin and RSS (optional)###
        location /updates {
        rewrite /updates(.*)$ /main.php?g2_view=dynamicalbum.UpdatesAlbum last;

        location /popular {
        rewrite /popular(.*)$ /main.php?g2_view=dynamicalbum.PopularAlbum last;

        location /srss/ {
        rewrite /srss/(d+)$ /main.php?g2_view=rss.SimpleRender&g2_itemId=$1 last;
##  End Dynamic Albums ###

        location / {
                try_files $uri $uri /main.php?url=$1;
                #rewrite ^/(.+)$ /main.php?url=$1 last;

        location ~ .php$ {
        fastcgi_index  main.php;
        fastcgi_intercept_errors on; # to support 404s for PHP files not found
        fastcgi_split_path_info ^(.+.php)(/.+)$;
        fastcgi_param  PATH_INFO          $fastcgi_path_info;
        fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
         fastcgi_param REMOTE_ADDR $remote_addr;
        include        fastcgi_params;
                fastcgi_buffer_size 4k;
                fastcgi_buffers 256 4k;
                fastcgi_busy_buffers_size 16k;
                fastcgi_read_timeout 300s;

        # Static files.
        # Set expire headers, Turn off access log
        location ~* favicon.ico$ {
                access_log off;
                expires 1d;
add_header Cache-Control public;
       location ~ ^/(img|cjs|ccss)/ {
             access_log off;
                expires 7d;
                add_header Cache-Control public;

    # Deny access to .htaccess files,
    # git & svn repositories, etc
    location ~ /(.ht|.git|.svn) {
        deny  all;


This is a working setup. As it is with NGINX there is no one way to do the same thing so feel free to suggest your improvements for everyone’s benefit. Note that I know some of the things like “try_files” is not recommended but I used what works.

Known Issue:
Gallery2 generates URLs for filenames with spaces by substituting a + sign. therefore will not work with nginx at all. I did not find an nginx solution for this but it seems G2 throws a security violation error. You can ofcourse edit the code and handle it because it seems apache handles “+” differently (url encoding) the details of which I don’t wan’t to get into. Suffice to say, it is manageable and I was able to do so though it may not be necessary for everyone. If anyone requires that fix I can provide it though I am not sure if that is the best way to do it.

Gallery2 is still in use. I have not tried nginx with Gallery 3 “G3”. I did do an install on regular apache2 for testing purposes and that’s about it.

Leave a Reply