2

How do I make PHP-FPM rules play nicely with Nginx rewrite rules?

Current config file

server {

location / {

  location ~ \.php$ {
   try_files        $uri =404;
   fastcgi_split_path_info  ^(.+\.php)(/.+)$;
   fastcgi_pass     127.0.0.1:9000;
   fastcgi_param        PATH_INFO   $fastcgi_path_info;
   fastcgi_param        PATH_TRANSLATED $document_root$fastcgi_script_name;
   include          fastcgi.conf;
  }

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

    rewrite ^/(admincp/)$ /index.php?routestring=$1 break;

  }
}
4

1 回答 1

4

将您的位置块更改为以下内容。也尽量避免if陈述,在这里阅读它(以及更多):http ://wiki.nginx.org/Pitfalls

我已经用if (!-e ...)下面配置中的@missing 块替换了该部分。

server {
    root /your/root/path
    index index.php index.html index.htm;

    server_name your.domain.com;

    rewrite ^/(admincp/)$ /index.php?routestring=$1 break;

    location / {
            # First attempt to serve request as file, then
            # as directory, then fall back to index.php
            try_files $uri $uri/ /index.php?$args;
    }

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    location ~ \.php$ {
            # Move to the @missing part when the file doesn't exist
            try_files $uri @missing;

            # Fix for server variables that behave differently under nginx/$
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            # Include the standard fastcgi_params file included with ngingx
            include fastcgi_params;
            fastcgi_param PATH_INFO $fastcgi_path_info;
            fastcgi_index index.php;

            # Override the SCRIPT_FILENAME variable set by fastcgi_params
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_nam$

            # Pass to upstream PHP-FPM; This must match whater you name you$
            #fastcgi_pass phpfpm;
            fastcgi_pass 127.0.0.1:9000;
    }

    location @missing {
            rewrite ^(.*)$ /index.php?routestring=$1 break;
    }
}
于 2012-11-13T16:42:13.417 回答