Nginx下CodeIginter配置

Example, when none-configured:

http://127.0.0.1/ci/index.php/hello/index

Method 1:

当nginx找不到文件和目录时,不返回404,而是重定向到index.php入口文件


location /ci/ { 
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ /ci/index.php;
}

Method 2:


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

 

个人配置:

— application —


// config.php
base_url :  http://172.16.85.221:1981/
index_url:  /index.php?

useage:

1.1 on view, normal


<?php echo anchor('manager/password/reset', 'Password reset');?>
<a href="<?php%20echo%20site_url('manager/AppTranslate')%20?>">Translate</a>

1.2 on view, ajax request(javascript block)


$.ajax({
	'method': 'POST',
	'dataType': 'json',
	'data': {'uid': uid, 'uname': uname},
	'url': '<?php echo site_url('manager/UserManager/delete'); ?>',
	'success': function (data) {
	    if (data && data.data.status == true) {
	        alert('Selected user's state already delete');
	    } else {
	        alert('Delete user failed.');
	    }
	},
	'error': function (err_data){
	    alert('delete failed.');
	    console.log(err_data);
	}
});

2.1 on controller


$this->load->library('pagination');
$config['base_url'] = 'index.php?/manager/ListUser/index';
$config['total_rows'] = $this->users->get_user_count('');
$this->pagination->initialize($config);
$page_size = $this->config->item('per_page');
$pagination_link = $this->pagination->create_links();

— nginx —


 38 server {
 39 >---listen 1081 default_server;
 40 >---listen [::]:1081 default_server;
 41 >---root /home/wwwroot/Maret_API_MGR;
 42 
 43 >---index index.php index.html;
 44 
 45 >---server_name _;
 46 
 47 >---location / {
 48         try_files $uri $uri/ /index.php;
 49         if (!-e $request_filename) {                                                                                                                                                      
 50             rewrite ^/(index.php?/)(.*)([/])$
 51             http://172.16.85.221:1981/$2/index.php break;
 52         }
 53 >---}
 54 
 55 >---location ~ .php$ {
 56 >--->---include snippets/fastcgi-php.conf;
 57 >--->---fastcgi_pass unix:/run/php/php7.0-fpm.sock;
 58 >---}
 59 }