LoginSignup
7
1

More than 5 years have passed since last update.

PHPの非同期処理ライブラリamphpでリバースプロキシを実装する

Last updated at Posted at 2017-06-14

PHPの非同期処理ライブラリamphpでリバースプロキシを実装する

背景

Node.jsやGoなどのリバースプロキシの実装に触発されて、PHPでもamphpを使ってやってみました!

前提

  • CentOS 7.3
  • PHP 7.0+(remiリポジトリ)

パッケージのインストール

PHP

# yum localinstall -y https://rpms.remirepo.net/enterprise/remi-release-7.rpm
# yum install -y php70-php php70-php-intl php70-php-json php70-php-mbstring php70-php-opcache php70-php-xml php70-php-pecl-apcu php70-php-pecl-ev

その他

# yum install -y unzip git

PHPの設定

# cat <<'EOT'>/etc/opt/remi/php70/php.d/99-my.ini
[global]
expose_php = Off
memory_limit = 256M
short_open_tag = Off

[mbstring]
mbstring.language = Japanese

[Date]
date.timezone = Asia/Tokyo

[opcache]
opcache.enable_cli = 1
EOT
# cat <<'EOT'>/etc/profile.d/enablephp70.sh
#!/bin/bash
source /opt/remi/php70/enable
export X_SCLS="`scl enable php70 'echo $X_SCLS'`"
EOT
# source /etc/profile.d/enablephp70.sh

composerのダウンロードと設定

適当な作業用ディレクトリで作業します。

# php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
# php -r "if (hash_file('SHA384', 'composer-setup.php') === '669656bab3166a7aff8a7506b8cb2d1c292f042046c5a994c43155c0be6190fa0355160742ab2e1c88d40d5be660b410') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
# php composer-setup.php
# php -r "unlink('composer-setup.php');"
# php composer.phar config -g repos.packagist composer https://packagist.jp

composerでパッケージをインストール

当初は本家のamphp/aerys-reverseを利用するつもりだったのですが、若干問題があったのでforkしたsadapon2008/aerys-reverseを使います。

# cat <<'EOT'>composer.json
{
    "repositories": [
     {
      "type":"package",
        "package": {
          "name": "sadapon2008/aerys-reverse",
          "version":"master",
          "source": {       
            "url": "https://github.com/sadapon2008/aerys-reverse.git",
            "type": "git",
            "reference":"fix_autoload_misc"
          },
          "require": {
            "amphp/amp": "^1",
            "amphp/aerys": "dev-master",
            "amphp/artax": "^2"
          },
          "autoload": {
            "classmap": [
              {"Aerys\\ReverseProxy": "ReverseProxy.php"}
            ]
          }
        }
      }
    ],
    "minimum-stability":"dev",
    "prefer-stable": true
}
EOT
# php composer.phar require sadapon2008/aerys-reverse

実装

8080/tcpからローカルホストの80/tcpへのリバースプロキシを設定します。以下の内容でconfig.phpを作成します。aerysの設定は正直まだまだ暫定です。

config.php
<?php

const AERYS_OPTIONS = [
    'maxConnections' => 2000,
    'connectionsPerIP' => 2000,
    'disableKeepAlive' => true,
    'socketBacklogSize' => 1000,
];

$fallback = function(Aerys\Request $req, Aerys\Response $res) {
    $res->end('<html><body><h1>Fallback \o/</h1></body></html>');
};

(new Aerys\Host)->expose('*', 8080)->use(new Aerys\ReverseProxy('http://127.0.0.1/'))->use($fallback);

サーバ起動

# php vendor/amphp/aerys/bin/aerys -c config.php

確認

# curl http://127.0.0.1:8080/

PHPのリバースプロキシがキタ━━━━(゚∀゚)━━━━!!

7
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
1