Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I'm having trouble with an nginx rewrite. I want to rewrite ^(.*?)/(.*?)/?$ to controllers/$1.php?action=$2. This worked for me on an Apache server earlier. After that, I want to route that file to php-fastcgi.

For a normal request (i.e. when I type in the full path, without using a rewrite), php-fastcgi works normally.

My configuration:

server {
    listen [::]:80;

    root            /var/www/my-dir;
    index           index.php index.html index.htm;
    charset         utf-8;

    server_name         my-domain;

    location / {
        autoindex   off;
        rewrite     ^(.*?)/(.*?)/?$ controllers/$1.php?action=$2 last;
    }

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass    127.0.0.1:9000;
        fastcgi_index   index.php;
        include     fastcgi_params;
    }

    location ~ /\. {
        deny        all;
    }
}

After editing this configuration, I did a service nginx restart, of course.

For some reason the location block for php-fastcgi isn't used after the rewrite. Why is that & how to resolve it?

Instead, I'm getting a "No input file specified" as a response. In the error log, I see:

2013/06/01 19:00:25 [error] 14288#0: *1 access forbidden by rule, client: xxx, server: my-domain, request: "GET /user/create HTTP/1.1", host: "my-domain"
share|improve this question

1 Answer 1

up vote 1 down vote accepted

You're missing a try_files.

location / {
    try_files $uri $uri/ /index.php;
    # ....everything else
}

You might also have a problem with the rewrite since you don't have a / at the beginning of the relative URL controllers/$1.php.... Try changing that to /controllers/$1.php....

And, you're missing a fastcgi parameter that tells php-fpm where to find the PHP script. Add this into the location ~ \.php$ block.

fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
share|improve this answer
    
Thanks, it works now. –  Camil Staps Jun 1 '13 at 17:22

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.