Webman:您的专属HTTP代理服务器,让网络浏览更加畅快!

2023-10-16 20:12:02 浏览数 (2)

webman是什么

webman是一款基于workerman开发的高性能HTTP服务框架。webman用于替代传统的php-fpm架构,提供超高性能可扩展的HTTP服务。你可以用webman开发网站,也可以开发HTTP接口或者微服务。

安装webman

环境需求

  • PHP >= 7.2
  • Composer >= 2.0

1、创建项目

代码语言:javascript复制
composer create-project workerman/webman

2、运行项目

进入webman目录

windows用户

双击 windows.bat 或者运行 php windows.php 启动

提示如果有报错,很可能是有函数被禁用,参考函数禁用检查解除禁用

linux用户

debug方式运行(用于开发调试)

代码语言:javascript复制
php start.php start

daemon方式运行(用于正式环境)

代码语言:javascript复制
php start.php start -d

提示如果有报错,很可能是有函数被禁用,参考函数禁用检查解除禁用

3、访问项目

浏览器访问 http://ip地址:8787

配置http代理服务器

1、新建自定义文件 process/Proxy.php

代码语言:javascript复制
<?php
namespace process;

use WorkermanConnectionAsyncTcpConnection;
use WorkermanConnectionTcpConnection;
use WorkermanProtocolsHttpRequest;

class Proxy
{
    public function onMessage(TcpConnection $connection, Request $request)
    {
        $replace = [
            'api.example.com' => 'api.ai.com',
            'discord.example.com' => 'discord.com',
            'cdn.example.com' => 'cdn.discordapp.com',
            'gateway.example.com' => 'gateway.discord.gg',
        ];
        $host = $request->host(true);
        if (!isset($replace[$host])) {
            return $connection->send(response('404 not found', 404));
        }
        $host = $replace[$host];
        $buffer = (string)$request;
        $con = new AsyncTcpConnection("tcp://$host:443", ['ssl' =>[
            'verify_peer' => false
        ]]);
        $buffer = preg_replace("/Host: ?(.*?)rn/", "Host: $hostrn", $buffer);
        $con->transport = 'ssl';
        $connection->protocol = null;
        $con->send($buffer);
        $con->pipe($connection);
        $connection->pipe($con);
        $con->connect();
    }
}

2、修改进程配置文件 config/process.php

代码语言:javascript复制
<?php
return [
    // ... 这里省略了其它配置 ...

    // http代理配置
    'proxy' => [
        'handler' => processProxy::class,
        'listen' => 'http://0.0.0.0:8989',
        'count' => cpu_count(),
        'reloadable' => false,
    ]
];

3、执行 php start.php restart 重启webman 4、设置nginx代理,用于开启https。(以api.example.com为例)

代码语言:javascript复制
server {
  server_name api.example.com;
  listen 80;
  root /home/www/webman/public;
  # 这个配置很重要
  proxy_buffering off;

  # https证书 
  listen 443 ssl;
  ssl_certificate ssl/xxx.pem;
  ssl_certificate_key ssl/xxx.key;
  ssl_session_cache shared:le_nginx_SSL:1m;
  ssl_session_timeout 1440m;
  ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  ssl_prefer_server_ciphers on;
  ssl_ciphers "ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA:ECDHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA256:DHE-RSA-AES256-SHA:ECDHE-ECDSA-DES-CBC3-SHA:ECDHE-RSA-DES-CBC3-SHA:EDH-RSA-DES-CBC3-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:DES-CBC3-SHA:!DSS";

  location ^~ / {
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header Host $host;
      proxy_set_header X-Forwarded-Proto $scheme;
      proxy_http_version 1.1;
      proxy_set_header Connection "";
      if (!-f $request_filename){
          proxy_pass http://127.0.0.1:8989;
      }
  }
}

5、重启nginx

这样访问 https://api.example.com 实际上就是访问 https://api.ai.com 。实现了代理api.example.com代理api.ai.com

0 人点赞