2012-11-10 94 views
1

如何讓PHP-FPM規則在Nginx重寫規則中很好地發揮作用?Nginx,PHP並重寫

當前配置文件

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; 

    } 
} 
+0

究竟是什麼問題? – jagsler

+0

它要麼不能正確重寫,要麼根據重寫的URL正確加載PHP文件。 – bear

回答

1

您的位置塊更改爲以下。還試圖避免if陳述,在這裏閱讀有關它(和更多):http://wiki.nginx.org/Pitfalls

我已經用下面的配置中的@missing塊替換了if (!-e ...)部分。

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; 
    } 
}