简体   繁体   中英

How can I run Play framework in HTTPS only in the dev mode?

I'd like to run Play Framework over HTTPS only in the development mode and I've done so using the following bit of configuration:

https.port=9443
trustmanager.algorithm=JKS
keystore.file=conf/certificate.jks
keystore.password=password
certificate.password=password
application.mode=dev
%prodenv.application.mode=prod

This works when I run play run but in production we run play run --%prodenv and I want to disable HTTPS as the HTTPS is handled by Nginx. I'm lost with how to do this. I would like to do this via the configuration file and not via additional command-line arguments as it does defy the purpose of having all my application configuration in the application.conf file.

One way to do it is to have two confs file: application.conf and prod.conf

application.conf stays the way it is and prod.conf would look something like

include "application.conf"
https.port = myProdPort

### other params to be overwritten

when launching your application in prod you can do

play run -Dconfig.file=/mypath/prod.conf

sbt run -Dhttps.port=9443 -Dhttp.port=disabled

Rather than have two configuration files, I achieved this by using just one. In order to run the app, I run play run --%dev and this is what the configuration looks like.

%dev.https.port=9443
%dev.trustmanager.algorithm=JKS
%dev.keystore.file=conf/certificate.jks
%dev.keystore.password=password
%dev.certificate.password=password

Similar to the other answer by Johan, I do it the reverse way: my application.conf is for prod and I run a dev.conf just in development:

include "application.conf"
https.port = devPort

And run locally like so:

play run -Dconfig.file=dev.conf

This way you don't have to change any configuration on your prod server.

You could remove the https.port param from your conf file and pass it in via the command line, when you run it in development mode:

play run -Dhttp.port=9443

See: Sprecifying server address and port

Play framework runs using Netty server you can overwrite the server configuration using -D parameters.

In sbt it can be done like:

sbt "project pepe-grillo-server" "run -Dhttps.port=42443 -Dhttp.port=disabled"

If you are using custom ssl engine provider CustomSSLEngineProvider you can use below command to run netty in ssl mode.

./sbt "-Dhttps.port=9443" "-Dplay.server.https.engineProvider=services.https.CustomSSLEngineProvider" "-Dconfig.resource=<config file> run

Once the server is up and running you can curl the endpoint to check cert validity.

curl -v https://127.0.0.1:9443

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