dongleiwei2182 2014-12-18 22:52
浏览 54
已采纳

从Nginx中的子目录运行辅助PHP应用程序

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.

  • 写回答

1条回答 默认 最新

  • duancoubeng5909 2014-12-19 06:28
    关注

    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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效