Techioz Blog

Rails 7.1.2 無効な HTTP 形式、解析が失敗する 非 SSL Puma への SSL 接続を開こうとしていますか?

概要

次のように資格情報を生成しました。

$ EDITOR="code --wait" bin/rails credentials:edit --environment production

Adding config/credentials/production.key to store the encryption key: d109e47d27a487cfe6227723f5ed4446

Save this in a password manager your team can access.

If you lose the key, no one, including you, can access anything encrypted with it.

      create  config/credentials/production.key

Editing config/credentials/production.yml.enc...
File encrypted and saved.

次に、次の方法で Rails サーバーを実行しました。

RAILS_ENV=production bin/rails s=> Booting Puma
=> Rails 7.1.2 application starting in production 
=> Run `bin/rails server --help` for more startup options
[310165] Puma starting in cluster mode...
[310165] * Puma version: 6.4.0 (ruby 3.2.0-p0) ("The Eagle of Durango")
[310165] *  Min threads: 5
[310165] *  Max threads: 5
[310165] *  Environment: production
[310165] *   Master PID: 310165
[310165] *      Workers: 8
[310165] *     Restarts: (✔) hot (✔) phased
[310165] * Listening on http://0.0.0.0:3000
[310165] Use Ctrl-C to stop
[310165] - Worker 0 (PID: 310188) booted in 0.02s, phase: 0
[310165] - Worker 1 (PID: 310190) booted in 0.02s, phase: 0
[310165] - Worker 2 (PID: 310195) booted in 0.01s, phase: 0
[310165] - Worker 3 (PID: 310200) booted in 0.01s, phase: 0
[310165] - Worker 4 (PID: 310211) booted in 0.01s, phase: 0
[310165] - Worker 5 (PID: 310217) booted in 0.01s, phase: 0
[310165] - Worker 6 (PID: 310229) booted in 0.01s, phase: 0
[310165] - Worker 7 (PID: 310251) booted in 0.0s, phase: 0

しかし、localhostにアクセスしようとするたびに、エラーが発生します。

[310165] - Worker 7 (PID: 310251) booted in 0.0s, phase: 0
2024-02-27 12:24:40 +0100 HTTP parse error, malformed request: #<Puma::HttpParserError: Invalid HTTP format, parsing fails. Are you trying to open an SSL connection to a non-SSL Puma?>
2024-02-27 12:24:40 +0100 HTTP parse error, malformed request: #<Puma::HttpParserError: Invalid HTTP format, parsing fails. Are you trying to open an SSL connection to a non-SSL Puma?>

前もって感謝します

解決策

Rails サーバーを実稼働モードで実行しています。

=> Rails 7.1.2 application starting in production 

ただし、https 以外のローカル URL、つまり http://localhost:3000 でアクセスしています。 Rails では、本番環境ではデフォルトで非 https リクエストが許可されません。

config.force_ssl = false を使用して、https なしで本番環境にアクセスできるように Rails を構成できます。ただし、そうしないことを強くお勧めします。

あなたが開発中であることは明らかだと思います。したがって、次のように Rails を開始する必要があります

$ bin/rails s

開発で実行します。