SWOOLE httpserver

14 篇文章 0 订阅

目前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错误,上游错误哦

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值