简体   繁体   English

nginx codeigniter重写:控制器名称与目录冲突

[英]nginx codeigniter rewrite: Controller name conflicts with directory

I'm trying out nginx and porting my existing apache configuration to nginx. 我正在尝试使用nginx并将现有的apache配置移植到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. 我已成功地重新路由了codeigniter url,但是我遇到了一个特定控制器的问题,该控制器的名称与站点根目录中的目录一致。

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. 我设法使我的codeigniter url的工作与在Apache中的工作一样,除了,我有一个特殊的URL说http://localhost/hello ,它与站点根目录中的hello目录一致。 Apache had no problem with this. Apache对此没有任何问题。 But nginx routes to this directory instead of the controller. 但是nginx路由到这个目录而不是控制器。

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. 所有codeigniter文件都在站点根目录中。

My nginx configuration (relevant parts) 我的nginx配置(相关部分)

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. 我是nginx的新手,我需要帮助找出与Controller名称的这个目录冲突。 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 你的问题是你告诉nginx在将文件夹传递给主控制器之前检查文件夹是否存在

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 您可以通过告诉nginx不要查找文件夹来简单地修复它,只需删除该部分即可

try_files $uri /index.php/$request_uri;

PS : $request_uri already contains a leading / PS$request_uri已包含领先/

check the wiki link 检查维基链接

So the more correct way is to do 所以更正确的方法是做

try_files $uri /index.php$request_uri;

PS #2 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. 应删除此部分, try_files语句已处理此部分并执行完全相同的操作。

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. 而且你的配置缺少一个root ,我想这就是你需要这些行的原因。

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_filesrewrite语句之间存在细微的差别,我们需要修复try_files以执行与rewrite相同的操作,因此它将从这个

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. 之后,重写应该可以删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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