欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

SWOOLE httpserver

发布时间:2025/5/22 编程问答 35 豆豆
生活随笔 收集整理的这篇文章主要介绍了 SWOOLE httpserver 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

目前PHP项目基本上都是HTTP server ,nginx 代理转发


$http = new swoole_http_server("0.0.0.0", 9503); //设置静态资源 $http->set([ 'enable_static_handler' => true, 'document_root' => '/home/demo/data' //静态资源,默认存放路径 ]); $http->on('request', function ($request, $response) { var_dump($request->get, $request->post); //获取GET参数和POST参数 $response->header("Content-Type", "text/html; charset=utf-8"); $response->cookie('s', 'xx', time()+1800); //设置COOKIES $response->end("<h1>Hello Swoole. #".rand(1000, 9999).json_encode($request->get)."</h1>"); //end只支持字符 }) ; $http -> start () ;

测试可以用

1.curl http://127.0.0.1:9503

2.可以用VHOST配置



也可以在nginx.conf中添加代理

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 (!-e $request_filename) {proxy_pass http://127.0.0.1:9501;}} }

用浏览器访问,记住防火墙端口设置哦

如果不运行http.php,会报502错误,上游错误哦

总结

以上是生活随笔为你收集整理的SWOOLE httpserver的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。