Php

NGINX + Codeigniter 不起作用無法訪問控制器並將參數發送到控制器

  • November 26, 2021

我想在 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 伺服器,這是我的routes.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”;**我希望它能夠工作。

這是我的程式碼點火器 config.php

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

請以某種方式幫助我。非常感謝你

最後我讓它工作了。我使用了來自https://gist.github.com/lynxluna/1050850的 nginx 配置並對其進行了一些修改。這是我的配置

    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;
        }
    }

引用自:https://serverfault.com/questions/509070