CMS MADE SIMPLE FORGE

CMS Made Simple Core

 

[#7985] Pretty URL's don't work with Nginx ...

avatar
Created By: Ashley Brandwood (albrandwood)
Date Submitted: Thu May 24 11:03:38 -0400 2012

Assigned To:
Version: 1.10.3
CMSMS Version: 1.10.3
Severity: Minor
Resolution: Invalid
State: Closed
Summary:
Pretty URL's don't work with Nginx ...
Detailed Description:
Or at least, the simple config for nginx webserver is to use:

try_files  $uri $uri/ /?page=$request_uri 

... the problem is that $request_uri contains the leading / ... (eg: /contact
/about etc)

the simple fix, is to strip the leading / as well as the trailing / from the URL

file: /lib/page.functions.php
line: 1056
old: $page=rtrim($page,'/');
new: $page=ltrim(rtrim($page,'/'),'/');


History

Comments
avatar
Date: 2012-05-24 11:04
Posted By: Ashley Brandwood (albrandwood)

It may not be the nicest solution, but it allows the pretty URL rewrite to work
with minimal effort using NGINX. (And NGINX/fpm-php is a LOT faster than Apache)
      
avatar
Date: 2012-05-24 11:15
Posted By: Robert Campbell (calguy1000)

CMSMS is using nginx with unmodified files and pretty urls.  No core
modification is necessary, the regular expressions are simple.

if (!-e $request_filename) {
  rewrite  ^\/?(.*)$  /index.php?page=$1  last;
  break;
}

      
avatar
Date: 2012-05-24 11:25
Posted By: Ashley Brandwood (albrandwood)

That method is specifically not recommended by Nginx ... 

http://wiki.nginx.org/Pitfalls#Check_IF_File_Exists ... 

They recommend using the try_files over if statements.




      
avatar
Date: 2012-08-22 19:40
Posted By: Sandro Magri (civita63)

Try  this in nginx server config:
.........................

location / {
         try_files $uri $uri/ @cmsmadesimple;
    }

location @cmsmadesimple {
        rewrite  ^\/?(.*)\.html$  /index.php?page=$1  last;
        rewrite  ^\/?(.*)$    /index.php?page=$1  last;
        break;
  }

location ~ \.php$ {
        include          fastcgi_params;
        fastcgi_index  index.php;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }

........................................

It works in my homesite  with cmsms 1.10.3 and nginx 1.2.3
     
      
avatar
Date: 2012-08-23 05:04
Posted By: Sandro Magri (civita63)

hotfix: The above config  for .php files is ok if cgi.fix_pathinfo=0  in
php.ini,
or there's a security issue. This is better:

upstream php {
	server unix:/tmp/php-fpm.sock;
	#server 127.0.0.1:9000;
   }

location ~ \.php$ {
	try_files $uri =404;
   	fastcgi_split_path_info ^(.+\.php)(/.+)$;
        include        fastcgi_params;
	fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_index  index.php;
        fastcgi_pass   php;
    }



      
Updates

Updated: 2012-08-22 19:51
state: Open => Closed

Updated: 2012-05-24 11:15
resolution_id: 5 => 9

Updated: 2012-05-24 11:04
description: Or at least, the simple config for nginx webserver is to use: try_files $uri $uri/ /?page=$request_uri ... the problem is that $request_uri contains the leading / ... (eg: /contact /about etc) the simple fix, is to strip the leading / as well a => Or at least, the simple config for nginx webserver is to use: try_files $uri $uri/ /?page=$request_uri ... the problem is that $request_uri contains the leading / ... (eg: /contact /about etc) the simple fix, is to strip the leading / as well a
resolution_id: => 5
severity_id: 4 => 3