目前官方已经开放了
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改造为:
public function isAjax($ajax = false)
{
$value = $this->header('x-requested-with') ? $this->header('x-requested-with') : $this->server('HTTP_X_REQUESTED_WITH');
$result = 'xmlhttprequest' == strtolower($value) ? true : false;
if (true === $ajax) {
return $result;
}
$result = $this->param($this->config['var_ajax']) ? true : $result;
$this->mergeParam = false;
return $result;
}
复制代码
2.在swoole中运用Nginx反向代理实时IP获取不到等问题 server{ listen 80; server_name demo.com; root /home/www/demo/public; # 该项要修改为你准备存放相关网页的路径 location / { index index.php index.html index.htm; #如果请求既不是一个文件,也不是一个目录,则执行一下重写规则 proxy_http_version 1.1; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://127.0.0.1:9501; } } public function ip($type = 0, $adv = true) { $type = $type ? 1 : 0; static $ip = null; if (null !== $ip) { return $ip[$type]; } $httpAgentIp = $this->config['http_agent_ip']; if ($httpAgentIp && $this->server($httpAgentIp)) { $ip = $this->server($httpAgentIp); } elseif ($adv) { if ($this->header('x-real-ip')) { $ip = $this->header('x-real-ip'); } elseif ($this->server('HTTP_X_FORWARDED_FOR')) { $arr = explode(',', $this->server('HTTP_X_FORWARDED_FOR')); $pos = array_search('unknown', $arr); if (false !== $pos) { unset($arr[$pos]); } $ip = trim(current($arr)); } elseif ($this->server('HTTP_CLIENT_IP')){ $ip = $this->server('HTTP_CLIENT_IP'); } elseif ($this->server('REMOTE_ADDR')){ $ip = $this->server('REMOTE_ADDR'); } } elseif ($this->server('REMOTE_ADDR')) { $ip = $this->server('REMOTE_ADDR'); } // IP地址类型 $ip_mode = (strpos($ip, ':') === false) ? 'ipv4' : 'ipv6'; // IP地址合法验证 if (filter_var($ip, FILTER_VALIDATE_IP) !== $ip) { $ip = ('ipv4' === $ip_mode) ? '0.0.0.0' : '::'; } // 如果是ipv4地址,则直接使用ip2long返回int类型ip;如果是ipv6地址,暂时不支持,直接返回0 $long_ip = ('ipv4' === $ip_mode) ? sprintf("%u", ip2long($ip)) : 0; $ip = [$ip, $long_ip]; return $ip[$type]; }
通过Nginx反向转发到Swoole内置的NgHttp的时候
配置如下:
由于Swoole接受真实IP X-Real-IP的时候是通过header头接受的,
所以将Request.php里的方法
联系客服