h2o IPアドレス アクセス制限

CentOS8
このサイトはアフィリエイト広告(Amazonアソシエイト含む)を掲載しています。

phpMyAdmin等、管理者系のサイトにはアクセス制限をかけて指定したIPアドレス以外からは接続できないようにする

rbファイル作成

適当な箇所にrbファイルを作成する

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
vi /etc/h2o/ip.rb
vi /etc/h2o/ip.rb
vi /etc/h2o/ip.rb
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
ALLOW_IPS = %w{
127.0.0.1
xxx.xxx.xxx.xxx ← アクセスを許可するIPアドレス
}
class Acl
def call(env)
if ALLOW_IPS.include? env['REMOTE_ADDR']
[399, {}, []]
else
[403, {'Content-Type' => 'text/plain;charset=utf-8'}, ['Forbidden']] ← 許可アドレス以外にはforbiddenを表示
end
rescue => e
$stderr.puts e
end
end
Acl.new
ALLOW_IPS = %w{ 127.0.0.1 xxx.xxx.xxx.xxx ← アクセスを許可するIPアドレス } class Acl def call(env) if ALLOW_IPS.include? env['REMOTE_ADDR'] [399, {}, []] else [403, {'Content-Type' => 'text/plain;charset=utf-8'}, ['Forbidden']] ← 許可アドレス以外にはforbiddenを表示 end rescue => e $stderr.puts e end end Acl.new
ALLOW_IPS = %w{
    127.0.0.1
    xxx.xxx.xxx.xxx ← アクセスを許可するIPアドレス
}
class Acl
  def call(env)
    if ALLOW_IPS.include? env['REMOTE_ADDR']
      [399, {}, []]
    else
      [403, {'Content-Type' => 'text/plain;charset=utf-8'}, ['Forbidden']] ← 許可アドレス以外にはforbiddenを表示
    end
  rescue => e
    $stderr.puts e
  end
end
Acl.new

h2o.conf編集

IPアドレス制限をかけるドキュメントールートの箇所にrbファイルのパスを指定する
例えば今回の場合は mruby.handler-file: /etc/h2o/ip.rb となる

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
user: h2o
gzip: ON
file.index: [ 'index.html', 'index.php' ]
# PHPの設定
file.custom-handler:
extension: .php
fastcgi.connect:
port: /var/run/php-fpm/www.sock
type: unix
hosts:
"xxx.xxxxxx.com:80":
listen:
port: 80
paths:
"/":
file.dir: /var/www/html
redirect:
url: /index.php/
internal: YES
status: 307
"/phpMyAdmin/":
file.dir: /usr/share/phpMyAdmin
----↓ここから下を追加↓----
mruby.handler-file: /etc/h2o/ip.rb
user: h2o gzip: ON file.index: [ 'index.html', 'index.php' ] # PHPの設定 file.custom-handler: extension: .php fastcgi.connect: port: /var/run/php-fpm/www.sock type: unix hosts: "xxx.xxxxxx.com:80": listen: port: 80 paths: "/": file.dir: /var/www/html redirect: url: /index.php/ internal: YES status: 307 "/phpMyAdmin/": file.dir: /usr/share/phpMyAdmin ----↓ここから下を追加↓---- mruby.handler-file: /etc/h2o/ip.rb
user: h2o
gzip: ON
file.index: [ 'index.html', 'index.php' ]

# PHPの設定
file.custom-handler:
  extension: .php
  fastcgi.connect:
    port: /var/run/php-fpm/www.sock
    type: unix
hosts:
  "xxx.xxxxxx.com:80":
    listen:
      port: 80
    paths:
      "/":
        file.dir: /var/www/html
        redirect:
            url: /index.php/
            internal: YES
            status: 307
      "/phpMyAdmin/":
          file.dir: /usr/share/phpMyAdmin

----↓ここから下を追加↓----
          mruby.handler-file: /etc/h2o/ip.rb

サービスの再起動

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
systemctl restart h2o
systemctl status h2o
systemctl restart h2o systemctl status h2o
systemctl restart h2o
systemctl status h2o

参考

H2Oを実戦投入するためのいろいろ - Qiita
下記で大体戦えるservice h2o reloadで反映かな。TOCBasic認証IP制限特定コンテンツの403対応 .gitなどBasic認証@h2o.cfg mruby.hand…

コメント

タイトルとURLをコピーしました