2013-02-03 94 views
0

我有一個奇怪的codeigniter 2問題,我在運行nginx 1.3.8的服務器上遇到。在我的URI段破折號被路由到我的控制器方法爲強調:codeigniter路由段錯誤地用nginx將下劃線轉換爲下劃線

網址:http://myserver.com/dothis/some-slug-with-dashes/someid

在routes.php文件:

$route['^dothis/(.+)/(.+)$'] = "mycontroller/dothis/$1/$2"; 

在mycontroller.php

function dothis($slug, $id) { 
    // echo $slug shows a value of "some_slug_with_dashes" 
} 

的apache web服務器上的相同代碼按預期工作(破折號仍爲破折號)。

我做了一些跟蹤和調試,發現問題發生在core/Router.php的_parse_routes()和_set_segments()函數週圍。在線Router.php的389

return $this->_set_request(explode('/', $val)); 

呼應$ val的價值在這裏表明,它是/mycontroller/dothis/some-slug-with-dashes/F3e

1.4.3的爆炸()中的值示出了

Array 
(
    [0] => mycontroller 
    [1] => dothis 
    [2] => some-slug-with-dashes 
    [3] => someid 
) 

跟蹤的執行以_set_request(),並且如果我插入線以輸出$segments PARAM的值:

function _set_request($segments = array()) 
{ 
    echo "\n<br/>_set_request() segments: <pre>";print_r($segments);echo "</pre>"; // inserted debug 
    $segments = $this->_validate_request($segments); 
    ... 
} 

我得到的調試輸出是

_set_request() segments: 

Array 
(
    [0] => mycontroller 
    [1] => dothis 
    [2] => some_slug_with_dashes 
    [3] => someid 
) 

如果我回應額外的調試輸出$this->uri->segments$this->uri->rsegments我得到這個:

// $this->uri->segments 
Array 
(
    [0] => dothis 
    [1] => some-slug-with-dashes 
    [2] => someid 
) 

// $this->uri->rsegments 
Array 
(
    [0] => mycontroller 
    [1] => dothis 
    [2] => some_slug_with_dashes 
    [3] => someid 
) 

我檢查了我的笨URI允許的字符,它的默認設置。我也檢查過nginx和fastcgi params,它們是基本規則。我也搜索了stackoverflow問題和nginx論壇。看起來奇怪的是,php在調用_set_request()之前顯示正確的值,但在_set_request() param中修改了值。

有沒有人知道或有一些建議的想法是什麼可能導致這種情況發生?

更新 - 我的nginx CONFIGS如下:

nginx.conf:

user nginx; 
worker_processes 1; 

error_log /var/log/nginx/error.log warn; 
pid  /var/run/nginx.pid; 


events { 
    worker_connections 1024; 
} 


http { 
    include  /etc/nginx/mime.types; 
    default_type application/octet-stream; 

    log_format main '$remote_addr - $remote_user [$time_local] "$request" ' 
         '$status $body_bytes_sent "$http_referer" ' 
         '"$http_user_agent" "$http_x_forwarded_for"'; 

    access_log /var/log/nginx/access.log main; 

    sendfile  on; 
    #tcp_nopush  on; 

    keepalive_timeout 35; 

    # limit num of requests from single IP to 5req/s 
    limit_req_zone $binary_remote_addr zone=flood:10m rate=5r/s; 

    ########################################################## 

    # load gzip settings 
    include /etc/nginx/conf.d/gzip.conf; 

    # load geoip 
    include /etc/nginx/conf.d/geoip.conf; 

    ########################################################## 

    # load all vhosts 
    include /etc/nginx/sites-enabled/*.conf; 
} 

php.conf:

location ~ \.php { 
    #fastcgi_pass unix:/tmp/php5-fpm.sock; 
    #fastcgi_pass 127.0.0.1:9000; 
    fastcgi_pass php; 

    fastcgi_buffers 16 16k; 
    fastcgi_buffer_size 32k; 

    fastcgi_param QUERY_STRING  $query_string; 
    fastcgi_param REQUEST_METHOD  $request_method; 
    fastcgi_param CONTENT_TYPE  $content_type; 
    fastcgi_param CONTENT_LENGTH  $content_length; 

    fastcgi_param PATH_INFO   $fastcgi_script_name; 
    fastcgi_param SCRIPT_NAME  $fastcgi_script_name; 
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 
    fastcgi_param REQUEST_URI  $request_uri; 
    fastcgi_param DOCUMENT_URI  $document_uri; 
    fastcgi_param DOCUMENT_ROOT  $document_root; 
    fastcgi_param SERVER_PROTOCOL $server_protocol; 

    fastcgi_param GATEWAY_INTERFACE CGI/1.1; 
    fastcgi_param SERVER_SOFTWARE nginx; 

    fastcgi_param REMOTE_ADDR  $remote_addr; 
    fastcgi_param REMOTE_PORT  $remote_port; 
    fastcgi_param SERVER_ADDR  $server_addr; 
    fastcgi_param SERVER_PORT  $server_port; 
    fastcgi_param SERVER_NAME  $server_name; 

    ### SET GEOIP Variables ### 
    fastcgi_param GEOIP_COUNTRY_CODE $geoip_country_code; 
    fastcgi_param GEOIP_COUNTRY_CODE3 $geoip_country_code3; 
    fastcgi_param GEOIP_COUNTRY_NAME $geoip_country_name; 
    fastcgi_param GEOIP_CITY_COUNTRY_CODE $geoip_city_country_code; 
    fastcgi_param GEOIP_CITY_COUNTRY_CODE3 $geoip_city_country_code3; 
    fastcgi_param GEOIP_CITY_COUNTRY_NAME $geoip_city_country_name; 
    fastcgi_param GEOIP_REGION    $geoip_region; 
    fastcgi_param GEOIP_CITY    $geoip_city; 
    fastcgi_param GEOIP_POSTAL_CODE   $geoip_postal_code; 
    fastcgi_param GEOIP_CITY_CONTINENT_CODE $geoip_city_continent_code; 
    fastcgi_param GEOIP_LATITUDE   $geoip_latitude; 
    fastcgi_param GEOIP_LONGITUDE   $geoip_longitude; 
} 

我的虛擬主機的conf:

upstream php { 
    server 127.0.0.1:8004; 
} 

server { 
    listen 80; 
    server_name myserver.com; 
    return  301 http://www.myserver.com$request_uri; 
} 

server { 
    server_tokens off; 
    listen 80; 
    server_name www.myserver.com; 

    root /var/www/sites/com.mysite/httpdocs; 
    access_log /var/www/sites/com.mysite/logs/access.log; 
    error_log /var/www/sites/com.mysite/logs/error.log; 

    index index.html index.php; 

     location /test { 
       auth_basic "Restricted"; 
       auth_basic_user_file /var/www/sites/com.mysite/.htpasswd; 
     } 

    location/{ 
     # if you're just using wordpress and don't want extra rewrites 
     # then replace the word @rewrites with /index.php 
     try_files $uri $uri/ @rewrites; 
    } 

    location @rewrites { 
     # Can put some of your own rewrite rules in here 
     # for example rewrite ^/~(.*)/(.*)/? /users/$1/$2 last; 
     # If nothing matches we'll just send it to /index.php 
     rewrite^/index.php last; 
    } 

    # This block will catch static file requests, such as images, css, js 
    # The ?: prefix is a 'non-capturing' mark, meaning we do not require 
    # the pattern to be captured into $1 which should help improve performance 
    #location ~* \.(?:ico|css|js|gif|jpe?g|png|txt|xml)(\?[0-9]+)?$ { 
    location ~* \.(?:ico|css|js|gif|jpe?g|png|txt|xml)$ { 
     # Some basic cache-control for static files to be sent to the browser 
     expires max; 
     add_header Pragma public; 
     add_header Cache-Control "public, must-revalidate, proxy-revalidate"; 
    } 

    error_page 500 502 503 504 /50x.html; 
    location = /50x.html { 
     root /usr/share/nginx/html; 
    } 

    include /etc/nginx/conf.d/php.conf; 
    include /etc/nginx/conf.d/drop.conf; 
} 
+0

我不認爲codeigniter喜歡在網址破折號。最好避開它。 –

+0

@何慧這根本不是真的。周杰倫,你需要提供更多關於你的php-fpm和nginx配置的信息,因爲很難確定原因。另外,你有沒有嘗試nginx 1.2.6? 1.3.x目前不是穩定版本。 – Brendan

+0

HeHui codeigniter如果改寫就可以使用破折號。我一直在apache上運行多年的破折號。這是我第一次安裝nginx,而且我似乎陷入了這個問題。 @brendan,nginx configs已被添加。 –

回答

0

我找到了p roblem - 開發者錯誤!原來一個部署腳本沒有正確地從nginx服務器上刪除一箇舊的錯誤MY_Router.php文件(這個文件本地不存在,也不在apache安裝中)。對不起,虛驚一場 - codeigniter,nginx和php-fpm按預期工作。開發者(我)不在這種情況下。