codeigniter $uri getting mangled with SCRIPT_NAME from nginx - El Forum - 06-06-2011
[eluser]Unknown[/eluser]
I am running nginx 1.04 and CI 2.02.
After spending a couple of days learning more than I wanted to on nginx configs and location rewrite rules, I think there may be a problem with CI. I have spent these last two days on the irc with no relief - I would very much appreciate some guidance. I just started experimenting with CI after having tried out rails. I like this lean and well documented framework and the frustration I've had to get this going is probably unusual. I am looking to the CI community to offer some advice and help, so thanks for reading so far .
I get a "No URI present. Default controller set" regardless of changes in nginx.conf and config.php (different protocols). I have settled on the REQUEST_URI protocol and though nginx passes the correct SCRIPT_NAME, it gets mangled in the URI.php.
Code: log_message('debug', "**********".$uri); // valid $uri
$uri = substr($uri, strlen($_SERVER['SCRIPT_NAME']));
log_message('debug', "**********".$uri); //$uri is blank
Because the $uri is blank, I get this in my CI logs:
Code: DEBUG - 2011-06-06 18:35:12 --> No URI present. Default controller set.
Please see the attached ci_config.php.txt for the full configuration, but here is the important stuff:
Code: #config.php
$config['base_url'] = 'http://www.example.net/ci/';
$config['index_page'] = 'index.php';
$config['uri_protocol'] = 'REQUEST_URI';
$config['url_suffix'] = '';
Here is debug log from nginx.conf that shows that REQUEST_URI is resolved correctly. Also "echo $_SERVER[REQUEST_URI]" shows up correctly in the output of the default controller. The text below is just some important stuff, but please see attachment for a complete log.
Code: ##tail -300 /home/examples/example.net/logs/error.log
2011/06/06 18:35:12 [debug] 18729#0: *148 using configuration "/ci/index.php/"
2011/06/06 18:35:12 [debug] 18729#0: *148 http cl:-1 max:3145728
2011/06/06 18:35:12 [debug] 18729#0: *148 http init upstream, client timer: 0
2011/06/06 18:35:12 [debug] 18729#0: *148 http script copy: "SCRIPT_FILENAME"
2011/06/06 18:35:12 [debug] 18729#0: *148 http script copy: "/srv/www/www.example.net/public_html/ci/index.php"
2011/06/06 18:35:12 [debug] 18729#0: *148 fastcgi param: "SCRIPT_FILENAME: /srv/www/www.example.net/public_html/ci/index.php"
2011/06/06 18:35:12 [debug] 18729#0: *148 http script copy: "QUERY_STRING"
2011/06/06 18:35:12 [debug] 18729#0: *148 http script copy: "CONTENT_LENGTH"
2011/06/06 18:35:12 [debug] 18729#0: *148 http script var: "/ci/index.php/welcome/test"
2011/06/06 18:35:12 [debug] 18729#0: *148 fastcgi param: "SCRIPT_NAME: /ci/index.php/welcome/test"
2011/06/06 18:35:12 [debug] 18729#0: *148 http script copy: "REQUEST_URI"
2011/06/06 18:35:12 [debug] 18729#0: *148 http script var: "/ci/index.php/welcome/test"
2011/06/06 18:35:12 [debug] 18729#0: *148 fastcgi param: "REQUEST_URI: /ci/index.php/welcome/test"
2011/06/06 18:35:12 [debug] 18729#0: *148 http script copy: "DOCUMENT_URI"
2011/06/06 18:35:12 [debug] 18729#0: *148 http script var: "/ci/index.php/welcome/test"
2011/06/06 18:35:12 [debug] 18729#0: *148 fastcgi param: "DOCUMENT_URI: /ci/index.php/welcome/test"
2011/06/06 18:35:12 [debug] 18729#0: *148 http script copy: "DOCUMENT_ROOT"
2011/06/06 18:35:12 [debug] 18729#0: *148 http script var: "/srv/www/www.example.net/public_html"
2011/06/06 18:35:12 [debug] 18729#0: *148 fastcgi param: "DOCUMENT_ROOT: /srv/www/www.example.net/public_html"
2011/06/06 18:35:12 [debug] 18729#0: *148 http script copy: "SERVER_PROTOCOL"
11/06/06 18:35:12 [debug] 18729#0: *148 chain writer out: 00000000
2011/06/06 18:35:12 [debug] 18729#0: *148 http output filter "/ci/index.php/welcome/test?"
2011/06/06 18:35:12 [debug] 18729#0: *148 http copy filter: "/ci/index.php/welcome/test?"
2011/06/06 18:35:12 [debug] 18729#0: *148 http postpone filter "/ci/index.php/welcome/test?" 0972A510
Here is my nginx configuration:
Code: # /etc/nginx/sites-enabled/www.example.net
server {
listen 80;
server_name example.net;
root /srv/www/www.example.net/public_html;
# If file is an asset, set expires and break
location ~* \.(ico|css|js|gif|jpe?g|png)(\?[0-9]+)?$ {
expires max;
break;
}
location /ci {
rewrite ^/ci/?(.*)$ /ci/index.php$1 permanent;
}
# Serve the directory/file if it exists, else pass to CodeIgniter front controller
location / {
try_files $uri @codeigniter;
}
# Do not allow direct access to the CodeIgniter front controller
location ~* ^/index.php {
rewrite ^/index.php/(.*)$ /$1 permanent;
}
# CodeIgniter Front Controller
location @codeigniter {
internal;
# include fastcgi.conf;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root/index.php;
fastcgi_param SCRIPT_NAME $document_root/index.php$request_uri;
}
# If directly accessing a PHP file in the public dir other than index.php
location ~* \.php$ {
try_files $uri @codeigniter;
# include fastcgi.conf;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_split_path_info ^(/+\.php)(.*)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
}
}
|