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

I'm moving a PHP application from Apache to Nginx, using php-fpm. I'm almost done, except by a rewrite directive that I haven't been able to do in Nginx. The following is the excerpt from the Apache config that I need to port:

# Handle alianza.quehambre.cl
RewriteCond %{HTTP_HOST} ^alianza\.quehambre\.cl [NC]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-l
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(api|get)/(.*)
RewriteRule ^(.*)$ /index.php/get/$1 [L,QSA]

The full Nginx configuration is in this gist, including my current non-working attempt to write the above rules.

share|improve this question
add comment

2 Answers

This should do the trick:

server {
    server_name alianza.quehambre.cl;

    location / {

        location ~ ^/(api|get)/(.*) {
            # Whatever should happen at this point...
        }

        try_files $uri /index.php/get/$request_uri =404;
    }
}
share|improve this answer
    
I tried it, but I'm getting a 404. I enabled the rewrite_log, and I don't get any error. –  Andres Jul 22 '13 at 18:39
    
Try removing the =404 part, PHP has to handle that request then. Replace your index.php temporarily to check what's coming in, e.g. by doing <?php header("content-type: text/plain"); print_r($_SERVER); ?> so you get a sense of what's going on. Also check that your fastcgi_params are set correctly. This rewrite will most certainly destroy the SCRIPT_FILENAME variable. You'd have to set this to a constant: fastcgi_param SCRIPT_FILENAME $document_root/index.php; –  Fleshgrinder Jul 22 '13 at 18:55
add comment

Try this one:

    location ~ (api|get)/(.*) {
    }

    location / {
      if ($http_host ~* "^alianza\.quehambre\.cl"){
        rewrite ^(.*)$ /index.php/get/$1 break;
      }
    }
share|improve this answer
add comment

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.