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 服务器;

posted @ 2020-01-19 11:42  码农编程进阶笔记  阅读(55)  评论(0编辑  收藏  举报
返回顶部 有事您Q我