ci中讓nginx能使用.htaccess文件

(1)加載 htaccess 文件php

#  /etc/nginx/sites-available/dev.gingili.link (部分文件)html

server {
        listen 80;

        root /home/wwwroot/dev.gingili.link; #網站所在目錄的絕對路徑
        index index.php index.html index.htm; #目錄索引

        server_name 'dev.gingili.link'; #網站的域名

        include /home/wwwroot/dev.gingili.link/.htaccess;

        location / {
                index index.html index.htm index.php; #目錄索引
        }

重點是:nginx

include /home/wwwroot/dev.gingili.link/.htaccess;

讓nginx 讀取到 htaccess  文件shell

(2)開啓 PATH_INFO 支持app

location ~ \.php { # 不能有 $
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                fastcgi_split_path_info ^(.+\.php)(.*)$;  # 添加這行
                fastcgi_param PATH_INFO $fastcgi_path_info;  #添加這行
                fastcgi_param SCRIPT_FILENAME /home/wwwroot/www.gingili.link/$fastcgi_script_name;
                include fastcgi_params;
        }

在 index.php中寫入框架

var_dump($_SERVER['PATH_INFO']);exit;

測試。測試

(3) 修改ci框架的配置文件網站

#application/config/config.phpunix

$config['enable_query_strings'] = TRUE;  // 開啓支持
$config['controller_trigger'] = 'c';
$config['function_trigger'] = 'm';
$config['directory_trigger'] = 'd'

(4)以 dev.gingili.link/login.html 爲例寫路由code

#.htaccess

# login
rewrite ^(.*)/login.html$  /index.php?c=Login&m=Index;

測試:

#application/controllers/Login.php

class Login extends MY_Controller
{
	public function __controller()
	{
		parent::__construct();
	}

	public function Index()
	{
		echo 'hello world';
		exit;
	}
}

輸出: hello world;

中途遇到問題

測試nginx文件。報錯

nginx: [warn] the "user" directive makes sense only if the master process runs with super-user privileges, ignored in /etc/nginx/nginx.conf:1
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: [emerg] open() "/run/nginx.pid" failed (13: Permission denied)
nginx: configuration file /etc/nginx/nginx.conf test failed

根據錯誤意思。換用戶執行nginx

>su root

錯誤解決

相關文章
相關標籤/搜索