2013-02-03 95 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按预期工作。开发者(我)不在这种情况下。