Note that there are some explanatory texts on larger screens.

plurals
  1. POIs there a way to force $c->uri_for in Catalyst to generate a URI that begins with https?
    primarykey
    data
    text
    <p>I've written a web application using Catalyst that has a lot of forms and needs to run over https. There are no hard-coded URLs, everything uses <code>$c-&gt;uri_for</code> or <code>$c-&gt;req-&gt;uri</code>. Everything worked great in the development environment using the dev server running over http.</p> <p>Today, when I went ahead and deployed the application, I noticed a problem. The way our production environment is currently setup, client browsers talk to a F5 load-balancer over HTTPS and the F5 talks to the web server on the internal network over HTTP.</p> <blockquote> <p>[ Browser ] ---HTTPS---> [ F5 ] ---HTTP---> [ Web Server ]</p> </blockquote> <p>Now, because the web server only gets <code>HTTP</code> requests, all URIs are generated starting with HTTP. This means:</p> <pre><code>&lt;form action='[% c.uri_for('/secure/form') %]' method='post'&gt; </code></pre> <p>becomes:</p> <pre><code>&lt;form action='http://websitename.org/secure/form' method='post'&gt; </code></pre> <p>and now all browsers complain you are submitting data over an insecure connection. I need that <code>c.uri_for</code> to begin with https.</p> <p>The app needed to go live today, so I did a mass search/replace for all form actions to this:</p> <pre><code>&lt;form action='[% c.uri_for('/secure/form') | replace('http:', 'https:'%]' method='post'&gt; </code></pre> <p>Well, now that breaks development, so I conditionalized the form actions based on a config key: </p> <pre><code>[% IF c.config.production %] &lt;form action='[% c.uri_for('/secure/form') | replace ('http:', 'https:') %]' method='post'&gt; [% ELSE %] &lt;form action='[% c.uri_for('/secure/form') %]' method='post'&gt; [% END %] </code></pre> <p>Needless to say, this all just seems wrong on multiple levels. Anyone have a better idea? And is there a way to force <code>$c-&gt;uri_for</code> to generate a URI that begins with https?</p> <h2>Solution</h2> <p>If you're using Catalyst 5.80008 or later, set <code>MyApp-&gt;config(using_frontend_proxy =&gt; 1);</code> and simply have your proxy set the <code>X-Forwarded-Port</code> header. For Catalyst versions prior to 5.80008, still set <code>using_frontend_proxy</code> so you get the actual client_ip, but to generate the correct URIs have your web server set the environment variable <code>HTTPS</code> to <code>ON</code></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload