php.htaccessnginxkirbyflightphp

Running a secondary PHP app from a subdirectory in Nginx


I have a site built on the Kirby flat file CMS and its running in the root directory just fine, but I'm trying to get an instance of FlightPHP running in a subdirectory (called crm) of the above project. This FlightPHP instance will handle form submissions so I need to be able to map the urls correctly.

With Apache and .htaccess, it's easy:

# make forms/crm links work
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^crm/(.*) crm/index.php [L]

I just can't seem to get this working with Nginx, here's what I have so far:

server {
  listen 80;
  listen [::]:80;
  root /var/www/mainsite;
  index index.php index.html index.htm;
  server_name mydomain.com;
  autoindex on;
  access_log off;

  # Kirby Specific Directories
  rewrite ^/site/(.*) /error permanent;
  rewrite ^/kirby/(.*) /error permanent;    

  add_header X-UA-Compatible "IE=Edge,chrome=1";

  location ~ .php {
      fastcgi_pass  unix:/var/run/php5-fpm.sock;
      fastcgi_index index.php;
      include fastcgi_params;
      fastcgi_hide_header X-Powered-By;
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
      fastcgi_param PATH_INFO $fastcgi_script_name;
      fastcgi_buffer_size 128k;
      fastcgi_buffers 256 16k;
      fastcgi_busy_buffers_size 256k;
      fastcgi_temp_file_write_size 256k;        
      include fastcgi_params;            
  }

  location ^~ /crm {
      root /var/www/mainsite/crm;

      if (!-e $request_filename)
      {
          rewrite ^/(.*)$ /index.php/$1 last;
          break;
      }
  } 

  location / {
      try_files $uri $uri/ /index.php?$query_string;
  }   

  location ~* .(?:xml|ogg|mp3|mp4|ogv|svg|svgz|eot|otf|woff|ttf|css|js|jpg|jpeg|gif|png|ico)$ {
    try_files $uri =404;
    expires max;
    access_log off;
    log_not_found off;
    add_header Pragma public;
    add_header Cache-Control "public, must-revalidate, proxy-revalidate";
  }         
}

I have tried quite a few approaches from similar posts online, but none that work correctly.


Solution

  • Try replacing your location ^~ /crm block with this

    location /crm {
        try_files $uri $uri/ /crm/index.php?$args;
    }
    

    And if that one doesn't solve it try

    location /crm {
        try_files $uri $uri/ /crm/index.php?$uri;
    }