Convert lighttpd rewrite rule to nginx

I have the following simple lighttpd rewrite rule:

$HTTP["host"] =~ "^(app\.mydomain\.com)$" {
    url.rewrite-once = (
            "^[^.]*$" => "controller.php/$1"
    )
}

I am trying to port this rule to nginx and have the following:

server {
  listen 80;

  server_name app.mydomain.com;

  if ($host = 'app.mydomain.com') {
    rewrite ^[^.]*$ controller.php/$1 last;
  }

  root /srv/www/domains/app.mydomain.com;

  index index.php;

  access_log /var/log/nginx/domains/app.mydomain.com/access.log;
  error_log /var/log/nginx/domains/app.mydomain.com/error.log;

  include /etc/nginx/excludes.conf;
  include /etc/nginx/php.conf;
 include /etc/nginx/expires.conf;

}

The problem is that the rewrite rule for nginx is not working correctly?
Any idea what I am doing wrong?

Thanks much.

Posted at Nginx Forum:

On Tue, Jul 24, 2012 at 8:23 AM, justin [email protected] wrote:

server {
  index index.php;

Any idea what I am doing wrong?

Maybe post the content of php.conf. Also, the if test is not needed.

Thanks for the tip on removing the if block. So, perhaps the regular
expression is off? Basically, the rewrite-rule should fire if the url
does NOT contain a period.

Examples:

 /some/path   //should hit the rewrite rule
 /file.php         //should NOT hit the rewrite rule

Here is php.conf as well:

location ~.php {
try_files $uri =404;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors on;
fastcgi_pass 127.0.0.1:9000;
include /etc/nginx/fastcgi_params;
}

Posted at Nginx Forum:

On Jul 24, 2012, at 5:23 , justin wrote:

server {
index index.php;
Any idea what I am doing wrong?

server {
server_name app.mydomain.com;

root /srv/www/domains/app.mydomain.com;

location ~ ^[^.]+$ {
    fastcgi_intercept_errors on;
    fastcgi_param SCRIPT_FILENAME $document_root/controller.php;
    fastcgi_param PATH_INFO $uri;
    fastcgi_pass 127.0.0.1:9000;
    include fastcgi_params;
}

...

}


Igor S.

Igor,

Thanks for the reply. Still not working, getting 404 error.

Here is the config snippet I am using:

server {
listen 80;

server_name app.mydomain.com;

root /srv/www/domains/app.mydomain.com;

index index.php;

access_log /var/log/nginx/domains/app.mydomain.com/access.log;
error_log /var/log/nginx/domains/app.mydomain.com/error.log;

include /etc/nginx/excludes.conf;

location ~ ^[^.]+$ {
try_files $uri =404;
fastcgi_param SCRIPT_FILENAME $document_root/controller.php;
fastcgi_param PATH_INFO $uri;
fastcgi_intercept_errors on;
fastcgi_pass 127.0.0.1:9000;
include /etc/nginx/fastcgi_params;
}

#Needed for standard php files which end with the .php extension
include /etc/nginx/php.conf;

include /etc/nginx/expires.conf;
}

Posted at Nginx Forum:

On Tue, Jul 24, 2012 at 1:08 PM, justin [email protected] wrote:

location ~.php {
try_files $uri =404;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors on;
fastcgi_pass 127.0.0.1:9000;
include /etc/nginx/fastcgi_params;
}

I think instead of using rewrite, this should work:

location ~ ^[^.]+$ {
fastcgi_intercept_errors on;
fastcgi_param SCRIPT_FILENAME $document_root/controller.php;
fastcgi_param PATH_INFO $uri;
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
}

Alternatively, check this: Module ngx_http_fastcgi_module

Once again, thanks Igor.

Removed the try_files and now I am getting:

Status: HTTP/1.1 403 Forbidden
Server: nginx/1.2.2
Date: Thu, 26 Jul 2012 04:58:09 GMT
Content-Type: text/html
Transfer-Encoding: chunked
Connection: close
X-Powered-By: PHP/5.4.5

Posted at Nginx Forum:

On Wed, Jul 25, 2012 at 08:47:57PM -0400, justin wrote:

try_files $uri =404;

As I understand the requested file does not exist on file system,
so try_files cannot find it and returns 404. The try_files is not
needed here.

include /etc/nginx/expires.conf;
}


Igor S.

On Thu, Jul 26, 2012 at 12:00 PM, justin [email protected] wrote:

X-Powered-By: PHP/5.4.5

Try setting fastcgi_intercept_errors to off to see what returned by php.

Same thing. Access denied.

Thanks.

Posted at Nginx Forum:

Edho,

Confirmed the entire directory is owned by the php-fpm user 'php`.
Should not have any permission issues.

Posted at Nginx Forum:

On Thu, Jul 26, 2012 at 1:05 PM, justin [email protected] wrote:

Same thing. Access denied.

Make sure the file is readable by user of php process.

On Thu, Jul 26, 2012 at 2:04 PM, justin [email protected] wrote:

Edho,

Confirmed the entire directory is owned by the php-fpm user 'php`.
Should not have any permission issues.

how about

sudo -u php ls /path/to/php/file.php

?

On 26 Jul 2012 09h23 CEST, [email protected] wrote:

2012/07/25 23:05:12 [error] 1040#0: *1 FastCGI sent in stderr:
“Access to the script ‘/srv/www/domains/app.mydomain.com/’ has been
denied (see security.limit_extensions)” while reading response
header from upstream, client: XXX.XXX.XXX.91, server:
app.mydomain.com, request: “GET / HTTP/1.1”, upstream:
“fastcgi://127.0.0.1:9000”, host: “app.mydomain.com

Any idea what that means?

Yes. It means that the file that you’re trying to access has an
extension that your PHP runtime config forbids. You must enable that
extension by setting the security.limit_extensions parameter.

— appa

Humm, well all the files have a .php extension in reality, but the
entire point of this is to generate “permalinks” or pretty urls like:

http://app.mydomain.com/add-person

Posted at Nginx Forum:

On Thu, Jul 26, 2012 at 2:23 PM, justin [email protected] wrote:

2012/07/25 23:05:12 [error] 1040#0: *1 FastCGI sent in stderr: “Access
to the script ‘/srv/www/domains/app.mydomain.com/’ has been denied (see
security.limit_extensions)” while reading response header from upstream,
client: XXX.XXX.XXX.91, server: app.mydomain.com, request: “GET /
HTTP/1.1”, upstream: “fastcgi://127.0.0.1:9000”, host:
app.mydomain.com

Any idea what that means?

try adding

fastcgi_param SCRIPT_NAME /controller.php;

to the location ~ ^[^.]+$ block.

Yeap, both:

sudo -u php ls /path/to/php/file.php

and

sudo -u php cat /path/to/php/file.php

Work. However, took a peak at the error log and found this:

2012/07/25 23:05:12 [error] 1040#0: *1 FastCGI sent in stderr: “Access
to the script ‘/srv/www/domains/app.mydomain.com/’ has been denied (see
security.limit_extensions)” while reading response header from upstream,
client: XXX.XXX.XXX.91, server: app.mydomain.com, request: “GET /
HTTP/1.1”, upstream: “fastcgi://127.0.0.1:9000”, host:
app.mydomain.com

Any idea what that means?

Posted at Nginx Forum:

Same thing, access denied. How does Wordpress and PHP 5.4 with Nginx
work with permalinks? It would seem it would have the same problem since
the paths are like:

http://www.my-wordpress-domain.com/some/blog/post

Thanks for the assistance Edho.

Posted at Nginx Forum:

Sorry, bump. Anybody have ideas?

Posted at Nginx Forum: