Skip to content

Technoob

Hosting Gallery2 with Nginx and rewrite enabled

December 18, 2012

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;

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

#photos# 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 last; }

}

location /lib/yui/ {

alias /path/to/gallery2/lib/yui/; break; }

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\_pass 127.0.0.1:9000;
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 http://example.com/gallery2/v/a+picture+with+spaces.jpg 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.


You are welcome to connect with me. Abhishek Dujari
This is my archived blog where you will find content about early days of Cloud Computing, Cybersecurity, Development and Sysadmin.