お知らせ

現在サイトのリニューアル作業中のため、全体的にページの表示が乱れています。

HTTPヘッダを持たないHTTPリクエストはあり得るのか?というのを検証しているときに気づいた話。

RFC 7230ではHostヘッダを持たないHTTPリクエストは禁止されており、これを受けたサーバーは400応答を返すことを必須としている。

RFC 7230:Hypertext Transfer Protocol (HTTP/1.1): Message Syntax and RoutingHostより

A client MUST send a Host header field in an HTTP/1.1 request even if
the request-target is in the absolute-form, since this allows the
Host information to be forwarded through ancient HTTP/1.0 proxies
that might not have implemented Host.
A server MUST respond with a 400 (Bad Request) status code to any
HTTP/1.1 request message that lacks a Host header field and to any
request message that contains more than one Host header field or a
Host header field with an invalid field-value.

なお、Node.jsのHTTPサーバー機能ではHostヘッダーのない要求を受け入れることができる。

http.createServer([options][, requestListener])を見ると、以下のようにrequireHostHeader: falseを渡すことで実現可能だ。規定値はtrueであるため、基本的にはHostヘッダーなしの要求は400応答が返される。

import http from 'node:http';

http
  .createServer({requireHostHeader: false}, (req, res) => {
    console.log(req.headers);
    res.statusCode = 200;
    res.end();
  })
  .listen(3000);

nginxにおいてもHostヘッダーなしの要求は以下の応答が返されたため同様と思われる。

<html>
<head><title>400 Bad Request</title></head>
<body>
<center><h1>400 Bad Request</h1></center>
<hr><center>nginx/1.26.0</center>
</body>
</html>

但し、nginxにおいてHostなしの要求を許容する方法は見つからなかった。Server namesによるとserver_name "";とすることで出来そうに見えたが、これは機能させることができず、400応答が返された。

ローカル環境用の開発ドメインをhttps化する時に使えるやつ
例えばローカル環境に複数のサービスがいて、それぞれをhttps://*.example.com/のようなドメインで管理したい時に使える

確認環境

同じことをすればLinuxとかでも応用できると思う

Env Ver
nginx 1.19.8
mkcert 1.4.3
Windows 10 Pro 19043.1415

手順

  1. mkcert の導入とワイルドカード証明書の作成
    choco install mkcert
    # mkcertを認証局として登録
    mkcert -install
    # 証明書を作成するドメインを列挙
    mkcert example.test *.example.test
    mv _wildcard.example.com+1.* C:/nginx/conf/.ssl/
    
  2. nginxの設定に証明書を記載

    server {
        server_name  dev.example.com;
        listen 443 ssl;
    
        ssl_certificate     ssl/_wildcard.example.com+1.pem;
        ssl_certificate_key ssl/_wildcard.example.com+1-key.pem;
        ...
    }