Announcement

#1 2012-09-20 21:56:09

alex2
Member
2012-09-20
15

url alias nginx config

Hello and bonjour,

I've installed piwigo with nginx + php-fpm. It's working great except for URL aliases (question_mark_in_urls, php_extension_in_urls, etc.). Everything else seems fine.

I believe the problem is I need to emulate Apache MultiViews - via a redirect - and not sure exactly how to do it. I'm not an nginx expert and not very good with regex either ... Only other topic I found was here and doesn't cover URLs. Anybody else done this?

My basic nginx config is something like this, based on a drupal config I'm using already (see docs here). EDIT: this was needlessly complex and have simplified

Code:

  # this assumes piwigo is under the docroot, e.g. /var/www/piwigo
  location /piwigo/ {
    try_files $uri $uri/ index.php ;
  }

When I turn on URL aliases, the front page still loads fine. When I click on a gallery, the basic page html loads but all URLs are broken since they're relative. Maybe obvious what I mean, but here's an example.

The page has a link to a thumbnail at :

    _data/i/galleries/gallery_name/Scan-090520-0004-cu_s150x9999.jpg

but since we're sitting on the new alias gallery url :

    /images/index/category/2-gallery_name

the browser wants to load :

    /images/index/category/2-gallery_name/_data/i/galleries/gallery_name/Scan-090520-0004-cu_s150x9999.jpg

instead of :

    /images/_data/i/galleries ...

So I imagine a rewrite could take care of this...

I think some webapps deal with this problem via a config param to explicitly set the root directory - not the domain name but the subdirectory off the webroot, in my case /images/. Drupal has such a parameter, although it's not always necessary. The idea is something like the apache RewriteBase directive.

If I figure this out, perhaps I will write up a general nginx how-to.

Last edited by alex2 (2012-09-22 23:49:24)

Offline

 

#2 2012-09-20 22:24:55

alex2
Member
2012-09-20
15

Re: url alias nginx config

Perhaps this has to do with passing the SCRIPT_FILENAME and/or PATH_INFO variables. Looking at the source it seems I shouldn't have to do a redirect at all, if those variables are specified properly.

Offline

 

#3 2012-09-21 10:10:56

plg
Piwigo Team
Nantes, France, Europe
2002-04-05
13786

Re: url alias nginx config

Hi alex2,

I have no practical experience on nginx (even if I'm seriously thinking about switching to it on Piwigo.com).

What I understand is that you made it work fine with default configuration, ie:

Code:

$conf['php_extension_in_urls'] = true;
$conf['question_mark_in_urls'] = true;

The problem arises when you switch values to "false".

An alternative source of information might come from the WordPress documentation http://codex.wordpress.org/Nginx maybe you can find there some bits to improve nginx configuration for Piwigo.

Is performance really better?

Offline

 

#4 2012-09-21 15:10:02

PaulRitzkat
Guest

Re: url alias nginx config

I'm also searching for rewrite rules for piwigo on nginx, didn't find any till now. If I hack something usefull together I'll let you know.

@plg: Yes, performance is stellar in comparison to apache2! Have a look at my piwigo installation http://ritzkat.com It runs on nginx/1.3.5 with php-fpm/5.3.17-1 and APC (alternate PHP cache). I use the config from perusio: https://github.com/perusio/wordpress-nginx https://github.com/perusio/php-fpm-example-config https://github.com/perusio/php-ini-cleanup he builds some very good nginx configs, which speed things up a lot and make setting up a new installation a lot easier :)

This site is hosted on a small dedicated server, but I ran similar and bigger sites (mostly wordpress) on a small vserver. Apache2 was slow in every regard, even with caching and with nginx (or lighttpd) the site was usable.

 

#5 2012-09-21 18:02:02

alex2
Member
2012-09-20
15

Re: url alias nginx config

@plg : yes, everything works fine with the default configuration, the problem is when I enable URL aliases (disable question marks / .php).

Rather than hack away I'll look at nginx configs for other php apps, that should help me figure it out. Thanks for the link for WordPress, and PaulRitzkat thanks for pointing out perusio. The nginx docs have quite a nice selection of sample configurations, would be nice to see piwigo there : http://wiki.nginx.org/Configuration

I concur with PaulRitzkat, performance is great. I run some webapps on a dedicated virtual server - only 512k RAM. With Apache, even the most minimal configuration it was impossible to avoid swapping, and using a hard limit on worker threads made certain pages load slowly. nginx is much better. In addition, configuration is far easier to understand than Apache.

Not sure when I'll work on this but will post whatever I find here ...

Offline

 

#6 2012-09-21 21:30:07

alex2
Member
2012-09-20
15

Re: url alias nginx config

I've gotten it working with no question marks, but not without the .php extension.

The real problem is PATH_INFO : when it is correct, piwigo works properly. I confirmed this by hard-coding it for one page, and it works fine.

First off you need to do some magic, typically this is set in a separate fastcgi config file (in my case /etc/nginx/fastcgi_params) which is included in the site config. As described here, something like this:

Code:

fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;

Then, based on this thread I configured a different handler for php, like this:

Code:

location ~ ^(?P<script_name>.+\.php)(?P<path_info>/.*)$ {
  try_files $script_name =404;
  include fastcgi_params;
  fastcgi_param SCRIPT_FILENAME $document_root$script_name;
  fastcgi_param PATH_INFO $path_info;
  fastcgi_param PATH_TRANSLATED $document_root$path_info;
  fastcgi_pass 127.0.0.1:9000;
}

RESULT : the key here is PATH_INFO. With the above config

(1) turn off question marks in URLs - $conf['question_mark_in_urls'] = false;

* hit a URL like .../piwigo/index.php/category/1
* nginx sets PATH_INFO to /category/1
* everything works

(2) also turn off the .php extension - $conf['php_extension_in_urls'] = false;

* hit a URL like .../piwigo/index/category/1
* nginx sets PATH_INFO to /index/category/1
* everything breaks

Unfortunately I'm way over my head dealing with regex here, and a little confused by certain nginx config issues. For one thing how to change this without possibly breaking other php apps. In addition whatever regex is required ought to support all URLs, whether they have ? or .php or not in any combination.

I need to read a little more to better understand the nginx-fastcgi connector, so far I've mostly been hacking.

Offline

 

#7 2012-09-23 00:02:07

alex2
Member
2012-09-20
15

Re: url alias nginx config

I'm giving up on this for the time being, but anyone feel free to comment if you have any ideas. A few more comments ...

About handling piwigo without php extensions in URLs, earlier I thought that this ONLY required rewriting requests to piwigo/index.php (I was assuming ALL requests went through ONE controller, as this is a fairly common pattern). In fact some requests go directly to other controllers, e.g. piwigo/picture.php (possibly others, I didn't look into it). In other words, with both .php and ? turned off, a URL like piwigo/picture.php?/1/category/1 --becomes--> piwigo/picture/1/category/1 .

So with that in mind I defined an nginx location as follows - this didn't work for me but I'm puzzled as to what's wrong.

Code:

location ~ ^(/piwigo/)(?<script_name>.[^\/]+)(?<path_info>.*)$ {
  include fastcgi_params;
  set $php_extension ".php";
  fastcgi_param SCRIPT_FILENAME /var/www/piwigo/$script_name$php_extension;
  fastcgi_param SCRIPT_NAME /piwigo/$script_name$php_extension;
  fastcgi_param PATH_INFO $path_info;
  fastcgi_param PATH_TRANSLATED $path_info;
  fastcgi_pass 127.0.0.1:9000;
}

This location definition uses a regex to split the script name and path info - these are variables available within the location {} block. For script name instead of expecting ".php" it just takes the first-level directory under /piwigo, and for the path it takes everything after that. i.e.: given : ...myserver.../piwigo/picture/1/category/1, script_name is 'picture' and path_info is '/1/category/1'. From those it defines the cgi params that get passed to PHP - key here is to append '.php' to the script_name.

This didn't work. I added debug code to piwigo to inspect the $_SERVER vars and could see that $PATH_INFO, $SCRIPT_FILENAME, etc. all were defined properly, exactly as they are in a local installation I've got running under Apache. So it seems it should work, but it doesn't. The relative URLs in the HTML are off so none of the images or assets loads -- which is what I saw before when PATH_INFO wasn't being set properly.

Last edited by alex2 (2012-09-23 00:04:15)

Offline

 

#8 2012-09-23 00:05:27

alex2
Member
2012-09-20
15

Re: url alias nginx config

also as a FYI, the config in the very first post was unnecessarily complex and I have edited to provide a simpler base config. As I said this works fine with ? and .php in the URLs.

Offline

 

#9 2013-07-01 14:20:00

teekay
Member
2013-06-12
427

Re: url alias nginx config

Better late than never - here you go. The trick is to create a @rewrite alias location, and use try_files against it.
The PATH_INFO stuff in the fcgi location handler is (only) required for the ? removal.

Code:

        location / {
                index index.php;
                try_files $uri $uri/ @rewrite;
        }

        location @rewrite {
                rewrite ^/picture((/|$).*)$ /picture.php$1 last;
                rewrite ^/index((/|$).*)$ /index.php$1 last;
        }

        location ~ ^(?<script_name>.+?\.php)(?<path_info>/.*)?$ {
                try_files $script_name = 404;

                fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
                fastcgi_param PATH_INFO $path_info;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;
        }

Offline

 

Board footer

Powered by FluxBB

github twitter newsletter Donate Piwigo.org © 2002-2024 · Contact