简体   繁体   中英

How to start WEBrick Ruby server in secure SSL mode HTTPS

Start situation:

Installed Linux Ubuntu operating system and Ruby via apt installer. After that installed WEBrick Ruby server via gem packgake handler.

Applications with version numbers:

  • Linux Ubuntu 18.04
  • Ruby 2.5.1p57
  • OpenSSL 1.1.1
  • gem WEBrick 2.7.6
  • gem openssl 2.7.6

Target situation:

To run WEBrick server in https mode

I took a look into WEBricks homepage, where was following setup example

require 'webrick'
require 'webrick/https'
require 'openssl'

cert = OpenSSL::X509::Certificate.new File.read '/path/to/cert.pem'
pkey = OpenSSL::PKey::RSA.new File.read '/path/to/pkey.pem'

server = WEBrick::HTTPServer.new(:Port => 8000,
                                 :SSLEnable => true,
                                 :SSLCertificate => cert,
                                 :SSLPrivateKey => pkey)

trap 'INT' do server.shutdown end

server.start

I generated cert/key with the following command

openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -days 365

After all, WEBrick does not work and following error message prints out

ERROR OpenSSL::SSL::SSLError: SSL_accept returned=1 errno=0 state=error: http request
    /var/lib/gems/2.5.0/gems/webrick-1.6.0/lib/webrick/server.rb:299:in `accept'
    /var/lib/gems/2.5.0/gems/webrick-1.6.0/lib/webrick/server.rb:299:in `block (2 levels) in start_thread'

Does anyone know, how to fix the problem?

The answer can be found in the error message you receive:

ERROR OpenSSL::SSL::SSLError: SSL_accept returned=1 errno=0 state=error: http request

You are making an HTTP request, not an HTTPS request, when you attempt to visit the server. OpenSSL is expecting an HTTPS request.

Change http://localhost:8000 in your client to https://localhost:8000 and you'll get:

[2020-04-25 13:08:49] ERROR `/' not found.
::1 - - [25/Apr/2020:13:08:49 PDT] "GET / HTTP/1.1" 404 285
- -> /

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM