Skip to content

Commit

Permalink
use forwarded headers
Browse files Browse the repository at this point in the history
Use X-Forwarded-For etc headers to override standard headers.  Existing
Plack middleware don't quite cover the use case we have.  ReverseProxy
will will only take the nearest link from X-Forwarded-For, which for us
will be the Fastly servers.  Fastly is configured to override any
incoming X-Forwarded-For header, so we can safely use the farthest link,
which will be the end user.

Other middleware don't support the other headers we would like to use.
  • Loading branch information
haarg committed Nov 30, 2017
1 parent 740d82b commit 4d52ba3
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion app.psgi
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,27 @@ STDERR->autoflush;
# explicitly call ->to_app on every Plack::App::* for performance
builder {

enable 'ReverseProxy';
enable sub {
my $app = shift;
sub {
my ($env) = @_;
if ( $env->{HTTP_FASTLY_SSL} ) {
$env->{HTTPS} = 'ON';
$env->{'psgi.url_scheme'} = 'https';
}
if ( my $host = $env->{HTTP_X_FORWARDED_HOST} ) {
$env->{HTTP_HOST} = $host;
}
if ( my $port = $env->{HTTP_X_FORWARDED_PORT} ) {
$env->{SERVER_PORT} = $port;
}
if ( my $addrs = $env->{HTTP_X_FORWARDED_FOR} ) {
my @addrs = map s/^\s+//r =~ s/\s+$//r, split /,/, $addrs;
$env->{REMOTE_ADDR} = $addrs[0];
}
$app->($env);
};
};
enable sub {
my $app = shift;
sub {
Expand Down

0 comments on commit 4d52ba3

Please sign in to comment.