swoole 建立httpserver 服务

1.上代码:http_server.php文件

<?php
/**
 *User: lxw
 *Date: 2020-01-16
 */

/*$http = new Swoole\Http\Server("127.0.0.1", 9501);

$http->on('request', function ($request, $response) {
    var_dump($request->get, $request->post);
    $response->header("Content-Type", "text/html; charset=utf-8");
    $response->end("<h1>Hello Swoole. #".rand(1000, 9999)."</h1>");
});

$http->start();*/

$http = new swoole_http_server('0.0.0.0', 8811);

$http->on('request', function ($request, $response) {
//    print_r($request->get);
    $response->end("ss".json_encode($request->get));
//    $response->end("<h1> http server</h1>>");
    //end :发送html响应体,并结束请求处理,  end操作后将向客户端浏览器发送html内容
    //end 只能调用一次,如果需要向客户端发送多次数据,请使用write
});
//开启服务
$http->start();

2.服务端:启动服务

3.客户端:浏览器,谷歌浏览器带上子域名前缀www

http://www.httpserver.com:8811/?m=222

4.补充httpserver.com 在NGINX中配置

server
    {
        listen 8811;
        #listen [::]:80 default_server ipv6only=on;
        server_name httpserver.com  www.httpserver.com;
        index index.html index.htm index.php;
        root  /home/wwwroot/default/newproject/swoolechat/swoole-src/examples/server;

        #error_page   404   /404.html;

        # Deny access to PHP files in specific directory
        #location ~ /(wp-content|uploads|wp-includes|images)/.*\.php$ { deny all; }

        include enable-php.conf;

        location /nginx_status
        {
            stub_status on;
            access_log   off;

        }

        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
        {
            expires      30d;
        }

        location ~ .*\.(js|css)?$
        {
            expires      12h;
        }

        location ~ /.well-known {
            allow all;
        }

        location / {
            if (!-e $request_filename){
                rewrite ^/(.*)$ /index.php?s=/$1 last;
            }
        }
                location ~ /\.
        {
          deny all;
        }

        access_log  /home/wwwlogs/access.log;
    }

5.如果出现拒绝访问:考虑下防火墙

如果要修改防火墙配置,如增加防火墙端口3306

vi /etc/sysconfig/iptables 

增加规则

-A INPUT -p tcp -m state --state NEW -m tcp --dport 8811 -j ACCEPT

或,参数顺序前后没区别

-A INPUT -m state --state NEW -m tcp -p tcp --dport 8811 -j ACCEPT

重新启动iptables ,使其生效

======================================================

额外补充:

nignx 与swoole hhtp-server 不能同时开启,两者只能开启其一;两者不能共存.

两者切换,ctrl+c 取消swoole-http-server 服务器,service nginx restart 重启NGINX 服务器;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lxw1844912514

你的打赏就是对我最大的鼓励

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值