Gallery3:Using NGINX - Gallery Codex
Personal tools

Gallery3:Using NGINX

From Gallery Codex

Gallery3:Using NGINX documents part of Gallery3 which is in development.

Gallery3 is unstable and not suitable for production.

Features that are present may change or be removed.

Features that are absent may be planned or not.

If you want to participate, join the discussion!

Gallery3:Using NGINX documents part of Gallery3 which is not supported in the core distribution.

If you want to participate, join the discussion!


The core team only supports Gallery3 running on Apache 2.


For anyone who wants to use NGINX, we will attempt to place info on config and other details here.

NGINX configuration

This config will allow the kohana index.php dispatcher to work properly:

location /path/to/gallery3 {
  if (-f $request_filename) {
    expires max;
  if (!-e $request_filename) {
    rewrite ^/path/to/gallery3/index.php/(.+)$ /path/to/gallery3/index.php?kohana_uri=$1 last;

Modified based on

Protecting your Gallery

Add the following line to your NGINX configuration, just after "if (-f $request_filename) {":

    rewrite ^/gallery3/var/albums/(.*)$ /gallery3/file_proxy/$1 last;

This directs all requests for full-size images to file_proxy. Gallery3 will throw up an error when you restrict access to full-size images in the album permission dialog box, but it does appear to function correctly. Please report any bugs.

Caching issues

If you run into issues where the combined JS and CSS theme files load as empty files on every-other refresh, try adding this to your Nginx configuration:

if_modified_since off;
add_header Last-Modified "";

This should be expanded upon to find the source of the problem and improve caching support in Nginx.

Removing index.php from short URLs

To remove index.php from short URLs, change the rewrite line in the NGINX configuration stanza above as follows:

    rewrite ^/path/to/gallery3/(.+)$ /path/to/gallery3/index.php?kohana_uri=$1 last;

Also, change the definition of $config['index_page'] in /application/config/config.php to:

    $config["index_page"] = "";

Note: See for discussion. This solution appears to work but has not been exhaustively tested.

G2 Rewriting

location /path/to/old/gallery {
    rewrite ^/path/to/old/gallery/([^\?]+)$ /path/to/gallery3/g2/map?path=$1 permanent;

Important content from the .htaccess files

The main directory contains the following:

php_flag short_open_tag 1
php_flag register_globals 0
php_value upload_max_filesize 20M
php_value post_max_size 100M

Make sure these are added to your php.ini file. Note that the first option, short_open_tag, conflicts with the recommended configuration (although short_open_tag is the default in almost all environments)

Getting the REST API to function under older versions of Nginx

The REST API works well for the most part under Nginx. Servers running Nginx versions prior to 0.8.32 should use Matt Bostock's patch, which fixes a bug in Nginx's handling of 201 HTTP responses:

Older content

Nothing in this section appears to be necessary, but it's preserved for posterity...

Alternative configuration for php-fpm

I didn't have to add the rewrites above. I think setting PATH_INFO is enough:

http {
  upstream php {

server {
  location /gallery {
    root   /path/to/gallery;
    fastcgi_pass   php;
    fastcgi_index  index.php;
    fastcgi_split_path_info ^(.+\.php)(.*)$;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    fastcgi_param  PATH_INFO        $fastcgi_path_info;
    include fastcgi_params;

Permissions (.htaccess)

This patch file (attempts) to add support for permissions in NGINX (I assume it would be quite useful in porting G3 to lighttpd as well). Right now it's on my site [1]. In order to use it, apply this patch to the G3 source tree (it was based on r19576, the latest at the time). Modify core/helpers/access.php to point to a suitable location (almost definitely NOT /var/www/testing/gallery3/nginx.htaccess), and add this to your nginx.conf file:

include "/filesystem/path/to/nginx.htaccess";

Patch to helpers/access.php for Nginx

This patch to the htaccess_works() function (last function in the file) will skip the test entirely, if the server software is nginx. Since .htaccess files are not relevant for nginx, this test is entirely irrelevant if you are running under php5-fpm or fastcgi. If you are running under Apache with an Nginx proxy, you don't need this patch since .htaccess will work just fine.

@@ -721,6 +721,10 @@
   static function htaccess_works() {
     $success_url = url::file("var/security_test/success");
+    // If we are running under nginx, then .htaccess is not relevant
+    if (stristr( $_SERVER["SERVER_SOFTWARE"], 'nginx' ))
+      return true;
     @mkdir(VARPATH . "security_test");
     try {
       if ($fp = @fopen(VARPATH . "security_test/.htaccess", "w+")) {

Hopefully this patch is clear, but just in case, you need to add the lines with the + sign at the beginning, between the top part and the bottom part shown in the example above.


There are a few TBDs here:

  • How can we put this file in a better location?
  • Can we use system to send a HUP to NGINX to force it to reread this file? Do we want to?

Please send me (mikeage (talk)) feedback, or post to the devel list.

User contributions that might help as well