How to create a https server on localhost

前端 未结 6 436
误落风尘
误落风尘 2021-01-30 08:47

I followed the tutorial below to create a https server https://docs.nodejitsu.com/articles/HTTP/servers/how-to-create-a-HTTPS-server/

and the program runs without errors

相关标签:
6条回答
  • 2021-01-30 09:12

    Assuming you are using node.js, then http-server has -S or --ssl with -C and -K to enable https.

    0 讨论(0)
  • 2021-01-30 09:21

    If this is meant for testing and you don't need a valid cert (which seems to be the case since you're using "localhost") you can use a "self-signed" cert, just make sure to configure nginx to point to those.

    I could explain the details, but there's actually a great post about that on Digital Ocean community tutorials:

    https://www.digitalocean.com/community/tutorials/how-to-create-a-self-signed-ssl-certificate-for-nginx-in-ubuntu-16-04

    just be sure to adapt the port (443) if you want to listen on 8000.

    0 讨论(0)
  • 2021-01-30 09:26

    I use Caddyserver with config like this:

    :443
    tls self_signed
    
    0 讨论(0)
  • 2021-01-30 09:29

    Well one quick way to do this is with ngrok.

    It's really easy to use and only takes few secs to run. It is as simple as downloading your system version. Unzip and run ngrok.exe. It will open a command line type of window. Make sure your Apache server or the one you use is running.

    Then to only listen on an HTTPS tunnel endpoint run the following

    ngrok http -bind-tls=true site.dev:80
    

    or on whatever port you need https to be installed.

    Open browser and type https://localhost/myApp you will see it works.

    And if you type http://localhost/myApp it also works.

    Hope this is helpful to anyone for a fast solution.

    0 讨论(0)
  • 2021-01-30 09:35

    You need to do two things:

    • generate a self-signed SSL certificate and
    • add it to the trusted certificates

    Managed to do this on a macOS like so:

    • In order to generate the SSL certificate, run the follosing command in a terminal (according to the instructions from Let's Encrypt):
    openssl req -x509 -out localhost.crt -keyout localhost.key \
      -newkey rsa:2048 -nodes -sha256 \
      -subj '/CN=localhost' -extensions EXT -config <( \
       printf "[dn]\nCN=localhost\n[req]\ndistinguished_name = dn\n[EXT]\nsubjectAltName=DNS:localhost\nkeyUsage=digitalSignature\nextendedKeyUsage=serverAuth")
    
    • And to add the certificate to the trusted certificates, ran the following command (suggested on this blog):
    sudo security add-trusted-cert -d -r trustRoot -k "/Library/Keychains/System.keychain" "/private/tmp/certs/certname.cer"
    
    0 讨论(0)
  • 2021-01-30 09:37

    I finally set up my create-react-app https dev server

    the reason why I'm doing this is to test device motion API on a mobile device.

    install mkcert https://github.com/FiloSottile/mkcert

    brew install mkcert
    
    mkcert -install
    

    generate cert files.

    mkcert -key-file ./.cert/key.pem -cert-file ./.cert/cert.pem "<LAN_IP_ADDRESS>"
    

    use LAN IP address instead of "localhost", because we will open our https page on a mobile device that has connected to the same WiFi.

    create a .env file to set env variables

    HTTPS=true
    SSL_CRT_FILE=./.cert/cert.pem
    SSL_KEY_FILE=./.cert/key.pem
    

    start the dev server

    npm start
    

    last but not least, install the cert file on the mobile device, the .pem file generated by mkcert is located in ~/Library/Application Support/mkcert in my case.

    install cert file on an Android device

    https://support.google.com/pixelphone/answer/2844832?hl=en

    install cert file on an iOS device

    serve the .pem file on a static server, and open the file address on Safari

    0 讨论(0)
提交回复
热议问题