2014-01-18 56 views
1

我想配置我的Nginx虛擬主機的所有要求重寫/index.php如與框架標準,但我不認爲Nginx的是通過傳遞URI的正確部分的超全球。重寫的index.php維持查詢參數

我可以重寫/foo/bar/index.php/foo/bar和路由工作正常,但如果我追加查詢參數,因爲查詢參數似乎被列入REQUEST_URI場,他們不應該是路由失敗。例如,導航到http://lts3/test?foo=bar會從Slim返回404錯誤。

這裏是print_r($_SERVER)結果含有參數的查詢字符串(http://lts3/test?foo=bar),它打破:

Array 
(
    [USER] => www-data 
    [HOME] => /var/www 
    [FCGI_ROLE] => RESPONDER 
    [QUERY_STRING] => /test?foo=bar 
    [REQUEST_METHOD] => GET 
    [CONTENT_TYPE] => 
    [CONTENT_LENGTH] => 
    [SCRIPT_FILENAME] => /var/www/lts3/public/index.php 
    [SCRIPT_NAME] => /index.php 
    [REQUEST_URI] => /test?foo=bar 
    [DOCUMENT_URI] => /index.php 
    [DOCUMENT_ROOT] => /var/www/lts3/public 
    [SERVER_PROTOCOL] => HTTP/1.1 
    [GATEWAY_INTERFACE] => CGI/1.1 
    [SERVER_SOFTWARE] => nginx/1.4.1 
    [REMOTE_ADDR] => 192.168.1.134 
    [REMOTE_PORT] => 58050 
    [SERVER_ADDR] => 192.168.1.113 
    [SERVER_PORT] => 80 
    [SERVER_NAME] => lts3 
    [HTTPS] => 
    [REDIRECT_STATUS] => 200 
    [PHP_VALUE] => include_path=.:/var/www/lts3:/usr/share/php:/usr/share/pear 
    [SITEMODE] => development 
    [HTTP_HOST] => lts3 
    [HTTP_CONNECTION] => keep-alive 
    [HTTP_ACCEPT] => text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8 
    [HTTP_USER_AGENT] => Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.76 Safari/537.36 
    [HTTP_ACCEPT_ENCODING] => gzip,deflate,sdch 
    [HTTP_ACCEPT_LANGUAGE] => en-GB,en;q=0.8 
    [PHP_SELF] => /index.php 
    [REQUEST_TIME_FLOAT] => 1390079833.408 
    [REQUEST_TIME] => 1390079833 
) 

而且這裏是我的Nginx虛擬主機:

server { 
     listen 80; 
     server_name lts3; 

     error_log /var/log/nginx/lts3-error.log; 

     root /var/www/lts3/public; 

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

     location ~ \.php$ { 
       include fastcgi_params; 
       fastcgi_param PHP_VALUE "include_path=.:/var/www/lts3:/usr/share/php:/usr/share/pear"; 
       fastcgi_param SITEMODE development; 
       fastcgi_pass unix:/var/run/php5-fpm.sock; 
       fastcgi_index index.php; 
     } 
} 

爲什麼QUERY_STRINGREQUEST_URI相同,以及如何配置我的Nginx虛擬主機將URI的正確部分傳遞給每一個?我的理解是REQUEST_URI只能包含/test,而QUERY_STRING應該包含foo=bar。我嘗試了各種不同的重寫規則,其中沒有一個有任何積極的作用。我現在輪到你了。

我試圖找到解決方案here,herehere,都無濟於事。我使用的是Ubuntu 13.10。

+0

使用'$的index.php REQUEST_URI $ is_args $ query_string'如果你想在'try_files'中附加真正的查詢字符串 –

回答

1

您可能需要添加
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;就像我在下面做的那樣。我也修改了你的try_files一行。

您可能還需要包括你重寫的路徑(我註釋掉)。我希望這有助於...

server { 
     listen 80; 
     server_name lts3; 

     error_log /var/log/nginx/lts3-error.log; 

     root /var/www/lts3/public; 

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

     # Optional Line (un-comment to use): 
     # include /path/to/rewrites.conf; 

     location ~ \.php$ { 
       include fastcgi_params; 
       fastcgi_param PHP_VALUE "include_path=.:/var/www/lts3:/usr/share/php:/usr/share/pear"; 
       fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 
       fastcgi_param SITEMODE development; 
       fastcgi_pass unix:/var/run/php5-fpm.sock; 
       fastcgi_index index.php; 
     } 
} 

一旦你包括你重寫你可以再增加一個重寫規則是這樣的:

rewrite /(.+)/(.+) /index.php?$1=$2;