【问题标题】:How do I configure nginx and CodeIgniter?如何配置 nginx 和 CodeIgniter?
【发布时间】:2011-04-16 17:07:20
【问题描述】:

我正在家用计算机上运行 nginx 进行开发。我还将它链接到 DynDNS,这样我就可以更轻松地向我的同事展示进度。我似乎无法让 nginx 正确地重写为 CodeIgniter。我将 CodeIgniters uri_protocol 设置为 REQUEST_URI。

所有应该显示内容的页面都显示为完全空白。如果我 phpinfo();死();在 Codeigniter 的 index.php 文件中,它按预期工作,我得到了 phpinfo。

此外,应该给出 404 的页面会给出正确的 CodeIgniter 404 错误。

这是我目前所拥有的。

user www-data;
worker_processes  1;

error_log  /var/log/nginx/error.log;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
    # multi_accept on;
}

http {
    include       /etc/nginx/mime.types;

    access_log  /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;
    tcp_nodelay        on;

    #gzip  on;
    #gzip_disable "MSIE [1-6]\.(?!.*SV1)";

    root /home/zack/Development/public_html;

    include /etc/nginx/conf.d/*.conf;
    #include /etc/nginx/sites-enabled/*;


    server {
        listen 80;
        server_name zackhovatter.dyndns.info;

        index index.php;
        root /home/zack/Development/public_html;

        location /codeigniter/ {
            if (!-e $request_filename) {
                rewrite ^/codeigniter/(.*)$ /codeigniter/index.php/$1 last;
            }
        }
            #this is for the index.php in the root folder
        location /index.php {
            fastcgi_pass    127.0.0.1:9000;
            fastcgi_index   index.php;
            fastcgi_param   SCRIPT_FILENAME /home/zack/Development/public_html/index.php;
            include         fastcgi_params;
        }

        location /codeigniter/index.php {
            fastcgi_index   index.php;
            include         fastcgi_params;
            fastcgi_param   SCRIPT_FILENAME  /home/zack/Development/public_html/codeigniter/index.php;
            fastcgi_param   REQUEST_URI      $request_uri;
            fastcgi_param   QUERY_STRING     $query_string;
            fastcgi_param   PATH_INFO        $document_uri;
            fastcgi_param   REQUEST_METHOD   $request_method;
            fastcgi_param   CONTENT_TYPE     $content_type;
            fastcgi_param   CONTENT_LENGTH   $content_length;
            fastcgi_pass    127.0.0.1:9000;
        }
    }
}

【问题讨论】:

    标签: codeigniter nginx rewrite fastcgi


    【解决方案1】:

    实际上我得到了这个工作。不知道怎么做,但我认为是文件权限设置不正确或其他原因。

    编辑: 对于那些想知道的人,我似乎安装了旧版本的 PHP。恢复到旧的类构造函数解决了这个问题。达尼特。

    【讨论】:

      【解决方案2】:

      我有完全相同的问题(Codeigniter 给出空白页,错误页和 php 正在工作),我发现问题来自 mysql db 驱动程序,应该更改为 mysqli 驱动程序。

      所以我们只需要更改config/database.php中的db驱动

      $db['default']['dbdriver'] = 'mysqli';
      

      这是由于新版本的php中不推荐使用mysql驱动程序。

      【讨论】:

        【解决方案3】:

        CodeIgniter4 如何在 RasperyPi Debian 10.4 上设置 NGINX 服务器块(虚拟主机)

        我使用 RasperyPi-4 作为 Web 开发服务器,上面运行着不同的项目。 Visual Studio Code 可以非常轻松地通过 Remote-SSH 扩展编辑文件。 在 NGINX 上设置 CI4 让您有机会在同一台服务器上运行不同的项目。 因为我花了几天时间才让这个配置运行起来,所以我会给你一个快速参考指南,让你快速轻松地进行设置。 如果你还没有安装 NGINX 和 composer,请看这里: http://nginx.org/en/docs/beginners_guide.html

        https://codeigniter4.github.io/userguide/installation/installing_composer.html

        https://getcomposer.org/download/

        1. 通过 composer 安装 CodeIgniter4

        服务器ip:10.0.0.10 端口:8085 项目名称是“测试” 请根据您的需要进行修改!

        cd /var/www/html
        composer create-project codeigniter4/appstarter test
        

        上面的命令将创建一个“测试”文件夹 /var/www/html/test

        1. 修改“env”文件

        sudo nano /var/www/html/test/env
        
        
        app.baseURL = 'http://10.0.0.10:8085'
        

        重要提示:服务器 URL 请勿使用“localhost”!!!!!!

        请取消注释: # CI_ENVIRONMENT = 生产 并修改: CI_ENVIRONMENT = 发展

        将文件另存为“.env”以隐藏文件

        1. NGINX 服务器块

        cd /etc/nginx/sites-available
        touch test
        sudo nano test
        

        将其粘贴到文件中并在修改为您的要求后保存:

        server {
            listen 8085;
            listen [::]:8085;
        
            server_name test;
        
            root  /var/www/html/test/public;
            index index.php index.html index.htm;
        
            location / {
                try_files $uri $uri/ /index.php$is_args$args;
            }
        
            location ~ \.php$ {
                include snippets/fastcgi-php.conf;
        
                # With php-fpm:
                fastcgi_pass unix:/run/php/php7.4-fpm.sock;
                # With php-cgi:
                # fastcgi_pass 127.0.0.1:9000;
            }
        
            error_page 404 /index.php;
        
            # deny access to hidden files such as .htaccess
            location ~ /\. {
                deny all;
            }
        }
        

        保存文件后,创建一个启用站点的符号链接:

        cd /etc/nginx/sites-enabled
        sudo ln -s /etc/nginx/sites-available/test  /etc/nginx/sites-enabled/test
        
        1. 修改文件和组权限

        chown -v -R pi:www-data /var/www/html/test
        sudo chmod -v -R 775 /var/www/html/test
        
        1. 启动 NGINX

        sudo service nginx start
        
        1. 打开 CI4 欢迎页面

         http://10.0.0.10:8085
        

        【讨论】:

        • 建议在 app/Config/App.php 中设置 Base Site URL 例如公共 $baseURL = '10.0.0.10:8085';
        【解决方案4】:

        这里有一个解决方案。

        https://web.archive.org/web/20120504010725/http://hiteshjoshi.com/linux/secure-nginx-and-codeigniter-configuration.html

        另外,我必须更改 $config['uri_protocol'] = “DOCUMENT_URI”; 才能使其正常工作。

        更新:修复了网络存档中的 404 网址。

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-03-15
        • 2020-12-21
        • 1970-01-01
        • 2020-09-18
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多