2013-05-20 30 views
1

對於nginx是否可以重寫像'/submit.php'這樣的URL來進行魔術重寫(就像它在Apache上那樣)從index.php處理它們?由於網站結構和以前的所有網址都像'/addrate.php','/my_settings.php','/profile.php' - >有超過50個這樣的文件,爲每個函數創建一個單獨的.php文件是非常不專業和不合適的,而不是通過index.php解析它們,並使用所需的類,就像我們對其他重寫所做的那樣。對於所有.PHP文件/請求被index.php解析的Nginx重寫規則

請問您今天能找到一個解決方案/給我們一個建議嗎?

我想這一些信息是在這裏,但我想確切的逆轉結果: http://www.nullis.net/weblog/2011/05/nginx-rewrite-remove-file-extension/

回答

1

這種配置使您可以處理所有URL(在文件系統中不存在的文件)與一個PHP腳本放在/var/www/example.org/htdocs/index.php

server { 

    listen 80; ## listen for ipv4 

    server_name example.org www.example.org; 
    root   /var/www/example.org/htdocs; 
    access_log  /var/log/nginx/example.org.access.log; 
    error_log  /var/log/nginx/example.org.error.log; 

    location/{ 
      index index.php index.html index.htm; 
      try_files $uri $uri/ @php; 
    } 

    # enable running php files under php fastcgi 
    location ~ \.php { 
      fastcgi_pass 127.0.0.1:9000; 
      fastcgi_index index.php; 
      fastcgi_param SCRIPT_FILENAME /var/www/example.org/htdocs$fastcgi_script_name; 
      #fastcgi_param QUERY_STRING $uri; 
      include fastcgi_params1; 
    } 

    location @php { 
      fastcgi_pass 127.0.0.1:9000; 
      fastcgi_index index.php; 
      fastcgi_param SCRIPT_FILENAME /var/www/example.org/htdocs/index.php; # this script catches all non existing URLs 
      fastcgi_param QUERY_STRING $uri; 
      include fastcgi_params1; 
    } 
}