HTTP基础应用 - 常见问题

优质
小牛编辑
126浏览
2023-12-01

如何获取$HTTP_RAW_POST_DATA

  1. $content = $this->request()->getBody()->__toString();
  2. $raw_array = json_decode($content, true);

如何获取客户端IP

举例,如何在控制器中获取客户端IP

  1. //真实地址
  2. $ip = ServerManager::getInstance()->getServer()->connection_info($this->request()->getSwooleRequest()->fd);
  3. var_dump($ip);
  4. //header 地址,例如经过nginx proxy后
  5. $ip2 = $this->request()->getHeaders();
  6. var_dump($ip2);

如何处理静态资源

Apache URl rewrite

  1. <IfModule mod_rewrite.c>
  2. Options +FollowSymlinks
  3. RewriteEngine On
  4. RewriteCond %{REQUEST_FILENAME} !-d
  5. RewriteCond %{REQUEST_FILENAME} !-f
  6. #RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L] fcgi下无效
  7. RewriteRule ^(.*)$ http://127.0.0.1:9501/$1 [QSA,P,L]
  8. #请开启 proxy_mod proxy_http_mod request_mod
  9. </IfModule>

Nginx URl rewrite

  1. server {
  2. root /data/wwwroot/;
  3. server_name local.swoole.com;
  4. location / {
  5. proxy_http_version 1.1;
  6. proxy_set_header Connection "keep-alive";
  7. proxy_set_header X-Real-IP $remote_addr;
  8. if (!-e $request_filename) {
  9. proxy_pass http://127.0.0.1:9501;
  10. }
  11. }
  12. }

HTTP 状态码总为500

自 swoole 1.10.x2.1.x 版本存起,未执行http server回调中,若未执行response->end(),则全部返回500状态码