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 %{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.

2 Answers 2


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;
  • I tried it, but I'm getting a 404. I enabled the rewrite_log, and I don't get any error.
    – Andres
    Commented Jul 22, 2013 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; Commented Jul 22, 2013 at 18:55

Try this one:

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

    location / {
      if ($http_host ~* "^alianza\.quehambre\.cl"){
        rewrite ^(.*)$ /index.php/get/$1 break;

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .