简体   繁体   中英

nginx codeigniter rewrite: Controller name conflicts with directory

I'm trying out nginx and porting my existing apache configuration to nginx. I have managed to reroute the codeigniter url's successfully, but I'm having a problem with one particular controller whose name coincides with a directory in site root.

I managed to make my codeigniter url's work as it did in Apache except that, I have a particular url say http://localhost/hello which coincides with a hello directory in site root. Apache had no problem with this. But nginx routes to this directory instead of the controller.

My reroute structure is as follows

http://host_name/incoming_url => http://host_name/index.php/incoming_url

All the codeigniter files are in site root.

My nginx configuration (relevant parts)

server {

listen   80; ## listen for ipv4; this line is default and implied
#listen   [::]:80 default ipv6only=on; ## listen for ipv6

root /path/to/site/root;
index index.php index.html index.htm;

# Make site accessible from http://localhost/
server_name localhost;

location / {
    # First attempt to serve request as file, then
    # as directory, then fall back to index.html

    index index.php index.html index.htm;

    try_files $uri $uri/ /index.php/$request_uri;

    #apache rewrite rule conversion

    if (!-e $request_filename){
        rewrite ^(.*)/?$ /index.php?/$1 last;
    }

    # Uncomment to enable naxsi on this location
    # include /etc/nginx/naxsi.rules
}

location ~ \.php.*$ {
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini

    # With php5-cgi alone:
    fastcgi_pass 127.0.0.1:9000;
    # With php5-fpm:
    #fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    include fastcgi_params;
}

I'm new to nginx and I need help in figuring out this directory conflict with the Controller name. I figured this configuration from various sources in the web, and any better way of writing my configuration is greatly appreciated.

Your problem is that you are telling nginx to check for the existence of the folder before passing it to the main controller

try_files $uri ($uri/) /index.php/$request_uri;
                 |
             this part

You can simply fix it by telling nginx not to look for the folders, just by removing that part

try_files $uri /index.php/$request_uri;

PS : $request_uri already contains a leading /

check the wiki link

So the more correct way is to do

try_files $uri /index.php$request_uri;

PS #2

#apache rewrite rule conversion

if (!-e $request_filename){
    rewrite ^(.*)/?$ /index.php?/$1 last;
}

This part should be removed, the try_files statement already handles this part and does exactly the same.

I also believe that your site will still work after removing this line too

fastcgi_index index.php;

and this

fastcgi_split_path_info ^(.+\.php)(/.+)$;

I never really use these.

And your config is missing a root , I think this is why you needed those lines.

EDIT:

As you mentioned the website doesn't work if the rewrite is removed, I noticed there's a slight difference between the try_files and the rewrite statement, we need to fix the try_files to do the same thing that the rewrite does, so it will changed from this

try_files $uri /index.php$request_uri;

To this

try_files $uri /index.php/?$request_uri;

After that the rewrite should be ok to remove.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM