Proxy
由于 Swoole Server 对 HTTP 协议的支持并不完整,建议仅将 EasySwoole 作为后端服务,并且在前端增加 NGINX 或 APACHE 作为代理,参照下面的例子添加转发规则
Nginx
server { root /data/wwwroot/; server_name local.swoole.com; location / { proxy\_http\_version 1.1; proxy\_set\_header Connection "keep-alive"; proxy\_set\_header X-Real-IP $remote_addr; if (!-f $request_filename) { proxy_pass http://127.0.0.1:9501; } } }
代理之后,可通过
$request->getHeader('x-real-ip')[0]
获取客户端真实ipApache
<IfModule mod_rewrite.c> Options +FollowSymlinks RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f #RewriteRule ^(.*)$ index.php/$1 \[QSA,PT,L\] fcgi下无效 RewriteRule ^(.*)$ http://127.0.0.1:9501/$1 \[QSA,P,L\] #请开启 proxy\_mod proxy\_http\_mod request\_mod </IfModule>