fcgiProxy Tutorial

下载安装

下载地址

安装简述

这里我们以v0.1.5版本为例

1
2
3
4
5
6
mkdir -p /data/server/fcgi
cd /data/server/fcgi
wget https://github.com/jonnywang/fcgiProxy/files/2289660/v0.1.5.tar.gz
tar zxvf v0.1.5.tar.gz
cd v0.1.5
./fcgiProxy_linux --config=config.xml

配置

精简配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?xml version="1.0" encoding="UTF-8" ?>
<config>
<!-- redis protocol server -->
<admin_server>0.0.0.0:6899</admin_server>
<!-- http[s] -->
<http_server>0.0.0.0:8899</http_server>
<http_ssl_cert></http_ssl_cert>
<http_ssl_key></http_ssl_key>
<!-- fastcgi -->
<fcgi_server>0.0.0.0:9000</fcgi_server>
<script_filename>/data/server/fcgi/php/proxy.php</script_filename>
<query_string><![CDATA[name=xingqiba&version=0.1.5]]></query_string>
<header_params>
<param>
<key>FcgiVersion</key>
<value>0.1.5</value>
</param>
</header_params>
<origins>*</origins>
</config>

配置说明

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
开启redis服务给本地
<admin_server>127.0.0.1:6899</admin_server>

对外开启http服务
<http_server>0.0.0.0:8899</http_server>

开启https服务时需要配置,空则为http
<http_ssl_cert></http_ssl_cert>
<http_ssl_key></http_ssl_key>

转发websocket数据给php-fpm监听地址
<fcgi_server>0.0.0.0:9000</fcgi_server>

接收websocket数据的php文件地址
<script_filename>/data/server/fcgi/php/proxy.php</script_filename>

连接参数和转发header的默认参数,自己需要什么可配置在里面,比如
<query_string><![CDATA[name=zhangsan]]></query_string>
<header_params>
<param>
<key>isWebsocketProxy</key>
<value>1</value>
</param>
</header_params>

接收数据 修改/data/server/fcgi/php/proxy.php为一下内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?php

$data = [
'get' => $_GET,
'server' => $_SERVER,
'raw' => file_get_contents('php://input'),
];

//借助redis服务推送消息给指定客户(uuid标识)
try {
$notification_handle = new Redis();
$notification_handle->pconnect(
'127.0.0.1',
6899,
30,
'push'
);
$notification_handle->rawCommand('set', $_GET['uuid'], 'message from php');
} catch (Throwable $e) {

}

echo json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);

测试发送

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>websocket</title>
</head>
<body>
<script>
var ws = new WebSocket("ws://192.168.1.41:8899/sock?uuid=174171262&proxy=1");
ws.onopen = function (ev) {
ws.send("hello");
};
ws.onerror = function (ev) {
console.log(ev);
ws = null;
};
ws.onmessage = function (ev) {
console.log(ev.data);
};
</script>
</body>
</html>

服务器返回

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
{
"get": {
"name": "zhangsan",
"uuid": "174171262",
"proxy": "1"
},
"server": {
"USER": "www",
"HOME": "/home/www",
"FCGI_ROLE": "RESPONDER",
"QUERY_STRING": "name=zhangsan&uuid=174171262&proxy=1",
"REMOTE_ADDR": "192.168.1.184",
"SCRIPT_FILENAME": "/data/server/fcgi/php/gateway.php",
"isWebsocketProxy": "1",
"REMOTE_PORT": "51115",
"PROXY_UUID": "174171262",
"REQUEST_METHOD": "POST",
"CONTENT_LENGTH": "5",
"CONTENT_TYPE": "application/octet-stream",
"PHP_SELF": "",
"REQUEST_TIME_FLOAT": 1534840518.8024,
"REQUEST_TIME": 1534840518
},
"raw": "hello"
}

截图

引深

  • 自定义消息内容结构,利用转发到的gateway.php解析消息并内部转发到不同的controller或者service去执行对应逻辑,官方的框架暂无开源计划,实现也不是什么高难度大伙自行解决吧。