NGINX + Codeigniter 无法工作,无法访问控制器并向控制器发送参数

NGINX + Codeigniter 无法工作,无法访问控制器并向控制器发送参数

我想在 NGINX 下运行 codeigniter,但它就是不起作用。我希望能够向控制器方法发送参数。但到目前为止,我甚至无法使用地址访问控制器example.com/index.php/welcome.php。 它说未指定输入文件。然而当我输入example.com/index.php/它会重定向到欢迎控制器。我想说我已经尝试了互联网上的所有 nginx 配置文件。

这是我的 ngnix 配置

  server {
      ## Your website name goes here.
      server_name compute.amazonaws.com;
      ## Your only path reference.
      root /var/www/;
      listen 80;
      ## This should be in your http block and if it is, it's not needed here.
      index index.html index.htm index.php;

            location ~* \.(ico|css|js|gif|jpe?g|png)(\?[0-9]+)?$ {
                  expires max;
                  log_not_found off;
          }


         # location / {
                  # This is cool because no php is touched for static content
                #try_files $uri $uri/ /index.php?q=$uri&$args;
            #   try_files $uri $uri/ /index.php;
         #

       location / {
              try_files $uri $uri/ /index.php?$args;
              if (!-e $request_filename) {
                    rewrite ^/(.*)$ /index.php?q=$1 last;
              }
            }

          location ~ \.php$ {
              #try_files $uri =404;
              #fastcgi_split_path_info ^(.+\.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;
              fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
          }
  }     

这是我的欢迎控制器

           <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

           class Welcome extends CI_Controller {

            function __construct()
            {
                parent::__construct();
            }

            function index()
            {
                $this->load->helper('url');
                $this->load->view('welcome_message');
            }
            function a($a)
            {
                echo "AHOJ ";
                echo $a;
            }
           }

我希望能够调用函数A从欢迎控制器获取响应。基本上我想构建一个 REST 服务器,这是我的路线.php

     $route['default_controller'] = "welcome";
     $route['404_override'] = '';
     $route['test_welcome'] = 'welcome';
     $route['api/v1/(:any)'] = "api_v1/$1";
     $route['item/(:any)'] = "item/show_item/$1";

     $route['scaffolding_trigger'] = "";

重要的一行是$route['api/v1/(:any)'] = "api_v1/$1";我希望它能起作用。

这是我的 codeigniter config.php

           $config['base_url']  = '';               
           $config['index_page'] = '';                              
           $config['uri_protocol']  = 'REQUEST_URI';

请帮帮我。非常感谢

答案1

最后我终于让它工作了。我使用了 nginx 配置https://gist.github.com/lynxluna/1050850并做了一些修改。这是我的配置

     server
     {
         # GENERAL CONFIGS
         listen 80;
         server_name example.com .example.com;
         root /var/www;

         index index.html index.htm index.php;

         location ~* \.(ico|css|js|gif|jpe?g|png)$ 
        {
            expires max;
            add_header Pragma public;
            try_files $uri /index.html;
        }

         # ROOT Location
         location / {
             try_files $uri $uri/ @rewrites;
         }    

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

         location = /favicon.ico {
             access_log off;
             log_not_found off;
         }



         location ~ /\. {
             access_log off;
             log_not_found off;
             deny all;
         }

         # PHP Configs
         fastcgi_intercept_errors on;

         location ~ \.php {
          fastcgi_split_path_info ^(.+\.php)(/.+)$;

          fastcgi_param  PATH_INFO            $fastcgi_path_info;
          fastcgi_param  PATH_TRANSLATED      $document_root$fastcgi_path_info;

          fastcgi_param  QUERY_STRING         $query_string;
          fastcgi_param  REQUEST_METHOD       $request_method;
          fastcgi_param  CONTENT_TYPE         $content_type;
          fastcgi_param  CONTENT_LENGTH       $content_length;

          fastcgi_param  SCRIPT_NAME          $fastcgi_script_name;
          fastcgi_param  SCRIPT_FILENAME      $document_root$fastcgi_script_name;
          fastcgi_param  REQUEST_URI          $request_uri;
          fastcgi_param  DOCUMENT_URI         $document_uri;
          fastcgi_param  DOCUMENT_ROOT        $document_root;
          fastcgi_param  SERVER_PROTOCOL      $server_protocol;

          fastcgi_param  SERVER_SOFTWARE      nginx;

          fastcgi_param  REMOTE_ADDR          $remote_addr;
          fastcgi_param  REMOTE_PORT          $remote_port;
          fastcgi_param  SERVER_ADDR          $server_addr;
          fastcgi_param  SERVER_PORT          $server_port;
          fastcgi_param  SERVER_NAME          $server_name;

          fastcgi_index  index.php;
         fastcgi_pass unix:/var/run/php5-fpm.sock;
         }
     }

相关内容