Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

share|improve this question

This should do the trick:

server {

    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

Try this one:

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

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

Your Answer


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.