打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
TP5.1 - Swoole在使用时候需要改造的几点

目前官方已经开放了
ThinkPHP5.1版本的swoole:
https://github.com/top-think/think-swoole

但是还有一些不完善的地方:

1.比如swoole中session中的运用: https://github.com/top-think/think-swoole/issues/39
2.在swoole中isAjax判断失效
3.在swoole中运用Nginx反向代理实时IP获取不到等问题
4.中间件的使用,无法使用controller,目前暂时控制器中间件
5.配置失效问题,每次请求完成后,重新初始化配置:https://github.com/top-think/think-swoole/issues/38


我们现在对这些问题改造一下

1.isAJax的判断失效

由于swoole中,jQuery的ajax头是通过header传递的,打开.Request.php核心内,将isAJax改造为:

  1.  public function isAjax($ajax = false)

  2.     {

  3.         $value = $this->header('x-requested-with') ? $this->header('x-requested-with') : $this->server('HTTP_X_REQUESTED_WITH');

  4.         $result = 'xmlhttprequest' == strtolower($value) ? true : false;

  5.         if (true === $ajax) {

  6.             return $result;

  7.         }

  8.         $result = $this->param($this->config['var_ajax']) ? true : $result;

  9.         $this->mergeParam = false;

  10.         return $result;

  11.     }

复制代码

2.在swoole中运用Nginx反向代理实时IP获取不到等问题
通过Nginx反向转发到Swoole内置的NgHttp的时候
配置如下:

  1. server{

  2.     listen 80;

  3.     server_name  demo.com;

  4.     root /home/www/demo/public; # 该项要修改为你准备存放相关网页的路径

  5.     location / {

  6.          index  index.php index.html index.htm;

  7.          #如果请求既不是一个文件,也不是一个目录,则执行一下重写规则

  8.          proxy_http_version 1.1;

  9.          proxy_set_header X-Real-IP $remote_addr;

  10.          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

  11.          proxy_pass http://127.0.0.1:9501;

  12.     }

  13. }

复制代码
由于Swoole接受真实IP X-Real-IP的时候是通过header头接受的,
所以将Request.php里的方法
  1. public function ip($type = 0, $adv = true)

  2.     {

  3.         $type = $type ? 1 : 0;

  4.         static $ip = null;

  5.         if (null !== $ip) {

  6.             return $ip[$type];

  7.         }

  8.         $httpAgentIp = $this->config['http_agent_ip'];

  9.         if ($httpAgentIp && $this->server($httpAgentIp)) {

  10.             $ip = $this->server($httpAgentIp);

  11.         } elseif ($adv) {

  12.             if ($this->header('x-real-ip')) {

  13.                 $ip = $this->header('x-real-ip');

  14.             }

  15.            elseif ($this->server('HTTP_X_FORWARDED_FOR')) {

  16.                 $arr = explode(',', $this->server('HTTP_X_FORWARDED_FOR'));

  17.                 $pos = array_search('unknown', $arr);

  18.                 if (false !== $pos) {

  19.                     unset($arr[$pos]);

  20.                 }

  21.                 $ip = trim(current($arr));

  22.             } elseif ($this->server('HTTP_CLIENT_IP')){

  23.             $ip = $this->server('HTTP_CLIENT_IP');

  24.             } elseif ($this->server('REMOTE_ADDR')){

  25.             $ip = $this->server('REMOTE_ADDR');

  26.             }

  27.         } elseif ($this->server('REMOTE_ADDR')) {

  28.             $ip = $this->server('REMOTE_ADDR');

  29.         }

  30.         // IP地址类型

  31.         $ip_mode = (strpos($ip, ':') === false) ? 'ipv4' : 'ipv6';

  32.         // IP地址合法验证

  33.         if (filter_var($ip, FILTER_VALIDATE_IP) !== $ip) {

  34.             $ip = ('ipv4' === $ip_mode) ? '0.0.0.0' : '::';

  35.         }

  36.         // 如果是ipv4地址,则直接使用ip2long返回int类型ip;如果是ipv6地址,暂时不支持,直接返回0

  37.         $long_ip = ('ipv4' === $ip_mode) ? sprintf("%u", ip2long($ip)) : 0;

  38.         $ip = [$ip, $long_ip];

  39.         return $ip[$type];

  40.     }

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
nginx配置详解
Nginx 简单的负载均衡配置示例
php利用curl伪造IP来源的实例代码
websocket+前后端分离+https的nginx配置
Nginx(三):反向代理负载均衡集群配置详解
总结高并发下Nginx性能如何优化
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服