Vianney Sserwanga
Vianney Sserwanga

Reputation: 313

Automatic redirection from port 80 to port 433 in nitrogen over Yaws and Cowboy

I am developing my application using nitrogen framework which runs over Yaws and also over Cowboy web servers. My clients only know this 'domain-name.xx'or 'www.domain-name.xx' which by default hits port 80 (unsecure http), yet always it want 'https://www.domain-name.xx' for secure http (port 433)- which they are unwilling to keep entering in browsers.

I have seen a configuration which does this for nginx web server on http://nitrogenproject.com/doc/config.html

I request the community to help me provide the configuration changes for Cowboy and Yaws i can take to always ensure that everyone regardless of the browser entry, is always redirected to port 433 or 'https://www.domain-name.xx' instead of port 80 or 'http://www.domain-name.xx'.

Thank you.

Upvotes: 0

Views: 563

Answers (2)

Steve Vinoski
Steve Vinoski

Reputation: 20014

For Yaws you can modify the rel/nitrogen/etc/yaws.conf file to add the following server block:

<server domain-name.xx>
    port = 80
    listen = 0.0.0.0
    <redirect>
        / = https://www.domain-name.xx
    </redirect>
</server>

Then either restart, or run rel/nitrogen/lib/yaws/bin/yaws --hup to tell Yaws to reload its configuration.

Upvotes: 1

chops
chops

Reputation: 2612

I would recommend making a custom security handler.

Example:

-module (my_security_handler).
-behaviour (security_handler).
-export ([
    init/2,
    finish/2
]).


init(_Config, State) ->
    Bridge = wf:request_bridge(),
    case Bridge:protocol() of
       http ->
          NewURI = "https://" ++ Bridge:header(host) ++ "/" ++ Bridge:uri(),
          wf:status_code(301),
          wf:header(location, NewURI);
       https ->
          ok
    end,
    {ok, State}.

finish(_Config, State) ->
    {ok, State}.

Then load it in nitrogen_yaws.erl and nitrogen_cowboy.erl (You're running both on the same server? Seems like an odd way to go). Just add nitrogen:handler(my_security_handler) between the two lines:

nitrogen:init_request(RequestBridge, ResponseBridge),
nitrogen:handler(my_security_handler),  %% <---- added here
nitrogen:run().

Please Note: Up until very recently, the protocol() function was not available for simple_bridge for cowboy. But I've modified simple_bridge to support it appropriately so make sure you pull from the latest master for simple_bridge.

Upvotes: 1

Related Questions