【问题标题】:nginx rewrite rules codeigniternginx重写规则codeigniter
【发布时间】:2015-03-04 13:40:41
【问题描述】:

我正在使用Winginx 在 Windows 上使用 nginx php 和 mysql。 我想运行和配置 CodeIgniter 2。

Winginx结构:

d:\winginx|
    nginx.exe
    php5\php.exe
    mysql\mysqld.exe
    \home\localhost\public_html\codeigniter

以此为基础 (http://wiki.nginx.org/Codeigniter)

还有这个 (http://www.farinspace.com/codeigniter-nginx-rewrite-rules/)

还有这个 (Nginx rewrite rule for CodeIgniter)

我尝试了 coconfig ci,但我只看到 codeigniter 404 页面(没有 nginx 404 页面)

我的 ci config.php 是

$config['base_url'] = "";
$config['index_page']   = "";
$config['uri_protocol'] = "AUTO";
..

nginx.config 是

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

pid        temp/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       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  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    client_max_body_size 55m;

    #gzip  on;

    scgi_temp_path  temp/uwsgi_temp 1 2;
    uwsgi_temp_path  temp/uwsgi_temp 1 2;

    fastcgi_connect_timeout 1;

    include codeigniter.conf;
}

我在 nginx.conf 中包含 codeigniter.conf codeigniter.conf 是:

server {
        listen       80;
        server_name localhost;
        root home/localhost/public_html/codeigniter:
        autoindex on;
        index index.php;

        location / {

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

            location = /index.php{

                fastcgi_pass localhost:9000;
                #fastcgi_split_path_info       ^(.+\.php)(.*)$;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                #fastcgi_param PATH_INFO       $fastcgi_path_info;
                include fastcgi_params;
            }
        }

        location ~ \.php$ {
            return 444;
        }


}

我尝试了 3 种不同的配置 ci,但我只看到 codeigniter 404 页面(没有 nginx 404 页面)。

http://localhost/codeigniter/welcome/index -> CI 404 page!
http://localhost/codeigniter/welcome -> CI 404 page!
http://localhost/codeigniter -> CI 404 page!

我来宾问题是由 codeigniter.conf 中的 php 文件位置造成的

请帮我配置一下!

【问题讨论】:

    标签: php .htaccess codeigniter nginx


    【解决方案1】:

    我会试试这个

    server {
        listen       80;
        server_name localhost;
        root home/localhost/public_html/codeigniter;
        autoindex on;
        index index.php;
    
        location / {
            try_files $uri $uri/ /index.php;
        }
    
        location = /index.php {
            fastcgi_pass localhost:9000;
            #fastcgi_split_path_info       ^(.+\.php)(.*)$;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            #fastcgi_param PATH_INFO       $fastcgi_path_info;
            include fastcgi_params;
        }
    
        location ~ \.php$ {
            return 444;
        }
    }
    

    这是我用于生产 CI 安装的工具

    server
    {
        listen 80 default_server;
        server_name www.site.com;
    
        root /var/www;
        index index.php;
    
        # canonicalize codeigniter url end points
        location ~ ^/index.php/(.*[^/])$ { return 301 $scheme://$host/$1/$is_args$args; }
        location ~ ^/index.php/(.*)/$ { return 301 $scheme://$host/$1/$is_args$args; }
    
        location / {
            # Check if a file or directory index file exists, else route it to index.php.
            try_files $uri $uri/ /index.php;
        }
    
    
    
        location ~ \.php$
        {
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
        }
    }
    

    【讨论】:

    • 还要确保您的 application/config/routes.php 正确处理请求。
    • 我的应用程序/config/routes.php 是:$route['default_controller'] = "welcome"; $route['404_override'] = "";默认值。
    • 使用 PHP 7.1.5 我只需将 fastcgi_pass 端口从 9000 更改为 9071 以避免 504 网关超时。我还在 $fastcgi 之前添加了一个/,因为默认情况下Winginx 具有:$document_root/$fastcgi_script_name。我保留了 CI $config['index_page'] = 'index.php'; 并更改了 $config['base_url'] = '/';(添加了 /)。现在所有路由都在工作,即使是404_override
    • 我只是想知道location 行的顺序是否重要以及“规范化”行的真正作用是什么?
    • 我发现了一个不需要try_files 行的alternative version here,它为CI 提供了更多保护措施,并且还允许正确完成路由。
    【解决方案2】:

    我得到它的工作方式如下......

    server {
            listen 127.0.0.1:80;
            server_name  xxxx www.xxxx;
            root home/xxxx/public_html;
            autoindex on;
            index index.php;
    
            location / {
    
                try_files $uri $uri/ /index.php?/$request_uri;
    
                location ~ \.php$ {
                    if (!-e $document_root$document_uri){
                        return 404;
                    }
                    fastcgi_pass 127.0.0.1:9000;
                    fastcgi_index index.php;
                    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                    include fastcgi_params;
                }
    
            }
    
            location ~ \.php$ {
                return 444;
            }
    }
    

    【讨论】:

    • 实际上,带有错误的返回(404444)是不需要的,CI 会正确处理它们,您可以执行$route['404_override']。丢失并使其正常工作的行是try_files $uri $uri/ /index.php?/$request_uri;
    • 我不确定为什么 Brian 的回答对您不起作用,但您不需要 ?/$request_uri。为什么要在 location / { 块内添加 location ~ \.php$ {
    猜你喜欢
    • 2013-10-21
    • 1970-01-01
    • 2010-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-05
    • 2013-10-12
    相关资源
    最近更新 更多