发表于 2018-11-14 09:05:34

nginx rewrite php的CI(CodeIgniter)框架

  一. 了解nginx rewrite:
  正则表达式匹配,其中:
  * ~ 为区分大小写匹配
  * ~* 为不区分大小写匹配
  * !~和!~*分别为区分大小写不匹配及不区分大小写不匹配
  文件及目录匹配,其中:
  * -f和!-f用来判断是否存在文件
  * -d和!-d用来判断是否存在目录
  * -e和!-e用来判断是否存在文件或目录
  * -x和!-x用来判断文件是否可执行
  flag标记有:
  * last 相当于Apache里的标记,表示完成rewrite
  * break 终止匹配, 不再匹配后面的规则
  * redirect 返回302临时重定向 地址栏会显示跳转后的地址
  * permanent 返回301永久重定向 地址栏会显示跳转后的地址
  一些可用的全局变量有,可以用做条件判断(待补全)
  $args
  $content_length
  $content_type
  $document_root
  $document_uri
  $host
  $http_user_agent
  $http_cookie
  $limit_rate
  $request_body_file
  $request_method
  $remote_addr
  $remote_port
  $remote_user
  $request_filename
  $request_uri
  $query_string
  $scheme
  $server_protocol
  $server_addr
  $server_name
  $server_port
  $uri
  实例:
  1.   文件反盗链并设置过期时间
  这里的return 412 为自定义的http状态码,默认为403,方便找出正确的盗链的请求
  “rewrite ^/ http://http://374400.blog.51cto.com/addblog.jpg;” 显示一张防盗链图片
  “access_log off;”不记录访问日志,减轻压力
  “expires 3d”所有文件3天的浏览器缓存
  location ~* ^.+/.(jpg|jpeg|gif|png|swf|rar|zip|css|js)$ {
  valid_referers none blocked *.c1gstudio.com *.c1gstudio.net localhost 208.97.167.194;
  if ($invalid_referer) {
  rewrite ^/ http://374400.blog.51cto.com/addblog.jpg;
  return 412;
  break;
  }
  access_log   off;
  root /opt/htdocs/web;
  expires 3d;
  break;
  }
  2.   只充许固定ip访问网站,并加上密码
  root/opt/htdocs/www;
  allow   208.97.167.194;
  allow   222.33.1.2;
  allow   231.152.49.4;
  deny    all;
  auth_basic "C1G_ADMIN";
  auth_basic_user_file htpasswd;
  3.   文件和目录不存在的时候重定向:
  if (!-e $request_filename) {
  proxy_pass http://127.0.0.1;
  }
  二. nginx location 匹配顺序
  location 匹配的原型是这样的:location [=|~|~*|^~|@] /uri/ { … }
  “=”是精确匹配
  “@”是命名的location ,在正常的location 匹配中不会使用,仅仅在内部跳转中才会使用到。
  “~”是区分大小写的匹配
  “~*”是不区分大小写的匹配
  “^~”表示中止正则匹配(这个平时没太注意)
  匹配步骤:
  1. 带”=”前缀的先进行匹配,如果找到了,中止查找。
  2. 所有其它location 进行非正则的匹配,找到最精确匹配的那个,如果匹配到带”^~”前缀的,则中止查找。
  3. 正则查找,按照我们配置文件中配置的location 顺序进行查找。
  4. 如果正则查找匹配成功,则使用此正则匹配的location ,否则,使用第二步查找的结果。
  这里要特别说明下”=”与”^~”的区别:
  “=”在匹配时,则匹配带”=”的location 。而”^~”,则会匹配所有非”=”的非正则location ,只有在确认它是最精确匹配的location 后,才生效。
  三. CI框架伪静态
  CI 在 APACHE .htaccess 文件配置
  RewriteEngine on
  RewriteCond $1 !^(index\.php|system\.php|images|js|swfupload|robots\.txt)
  RewriteRule ^(.*)$ /wj/index.php/$1
  注: RewriteRule ^(.*)$ /wj/index.php/$1 里的wj是你的CI程序目录
  Nginx服务器伪静态设置首先需要设置nginx开启 path_info,nginx模式默认是不支持path_info模式的
  1. pathinfo,一种伪静态的用法,
  1.1、让 Apache 支持 PathInfo(Apache 版本 : 2.2.13)
  在配置文件中加入
  
  AcceptPathInfo On
  
  这样 Apache 就可以支持针对 php 文件的 PathInfo 了.
  1.2、pathinfo 模式 需要 php.ini 开启下面这个参数
  cgi.fix_pathinfo=1
  path_info模式:http://www.xxx.com/index.php/模块/方法
  1.3、让 Nginx 支持 PathInfo
  在配置文件里添加
  location ~ \.php {
  fastcgi_pass 127.0.0.1:9000;
  fastcgi_index index.php;
  set $path_info "";
  set $real_script_name $fastcgi_script_name;
  if ($fastcgi_script_name ~ “^(.+?\.php)(/.+)$”) {
  set $real_script_name $1;
  set $path_info $2;
  }
  }
  fastcgi_param SCRIPT_FILENAME $document_root$real_script_name;
  fastcgi_param SCRIPT_NAME $real_script_name;
  fastcgi_param PATH_INFO $path_info;
  include conf/fastcgi.conf;
  }
  要点:
  1.~ \.php 后面不能有$以便能匹配所有 *.php/* 形式的url
  2. 通过设置更改 SCRIPT_FILENAME
  cat fastcgi.conf
  #fastcgi_paramSCRIPT_FILENAME    $document_root$fastcgi_script_name; #注释掉的
  fastcgi_paramQUERY_STRING       $query_string;
  fastcgi_paramREQUEST_METHOD   $request_method;
  fastcgi_paramCONTENT_TYPE       $content_type;
  fastcgi_paramCONTENT_LENGTH   $content_length;
  #fastcgi_paramSCRIPT_NAME      $fastcgi_script_name;                #注释掉的
  fastcgi_paramREQUEST_URI      $request_uri;
  fastcgi_paramDOCUMENT_URI       $document_uri;
  fastcgi_paramDOCUMENT_ROOT      $document_root;
  fastcgi_paramSERVER_PROTOCOL    $server_protocol;
  fastcgi_paramHTTPS            $https if_not_empty;
  fastcgi_paramGATEWAY_INTERFACECGI/1.1;
  fastcgi_paramSERVER_SOFTWARE    nginx/$nginx_version;
  fastcgi_paramREMOTE_ADDR      $remote_addr;
  fastcgi_paramREMOTE_PORT      $remote_port;
  fastcgi_paramSERVER_ADDR      $server_addr;
  fastcgi_paramSERVER_PORT      $server_port;
  fastcgi_paramSERVER_NAME      $server_name;
  # PHP only, required if PHP was built with --enable-force-cgi-redirect
  fastcgi_paramREDIRECT_STATUS    200;
  2. CI rewrite 配置
  2.1
  location / {
  indexindex.php;
  if (!-e $request_filename) {
  rewrite^/(.*)$/index.php/$1last;
  break;
  }
  }
  2.2
  假设我们的子目录名称为 wj
  location /wj/ {
  root   /var/www/html/;
  index index.html index.htm index.php;
  if ($request_filename !~* (index\.php|system\.php|images|js|swfupload|robots\.txt)) {
  rewrite ^/(.*)$ /index.php?$1 last;
  }
  另外附上主目录伪静态规则
  #rewrite ^/$ /index.php last;
  #一下是防止某些文件夹被直接访问
  #rewrite ^/(?!index\.php|images|robots\.txt|js|css|upimg|artDialog|style)(.*)$ /index.php/$1 last;

页: [1]
查看完整版本: nginx rewrite php的CI(CodeIgniter)框架