Skip to content
This repository has been archived by the owner on May 6, 2020. It is now read-only.

Commit

Permalink
NGINX X_FORWARDED_FOR example
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanprodan committed Jan 11, 2017
1 parent cf0882a commit 5306bed
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
1 change: 1 addition & 0 deletions MvcThrottle.Demo/App_Start/FilterConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{ "Home/about", new RateLimits { PerHour = 30 } }
}
},
IpAddressParser = new NginxIpAddressParser(),
Logger = new MvcThrottleCustomLogger()
};

Expand Down
28 changes: 28 additions & 0 deletions MvcThrottle.Demo/Helpers/NginxIpAddressParser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MvcThrottle.Demo.Helpers
{
public class NginxIpAddressParser : IpAddressParser
{
public override string GetClientIp(HttpRequestBase request)
{
var ipAddress = request.UserHostAddress;

// get client IP from reverse proxy
var xForwardedFor = request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (!string.IsNullOrEmpty(xForwardedFor))
{
// Search for public IP addresses
var publicForwardingIps = xForwardedFor.Split(',').Where(ip => !IsPrivateIpAddress(ip)).ToList();

// If we found any public IP, return the first one when NGNIX is used, otherwise return the user host address
return publicForwardingIps.Any() ? publicForwardingIps.First().Trim() : ipAddress;
}

return ipAddress;
}
}
}
6 changes: 4 additions & 2 deletions MvcThrottle.Demo/MvcThrottle.Demo.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@
<AssemblyName>MvcThrottle.Demo</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<MvcBuildViews>false</MvcBuildViews>
<UseIISExpress>false</UseIISExpress>
<UseIISExpress>true</UseIISExpress>
<IISExpressSSLPort />
<IISExpressAnonymousAuthentication />
<IISExpressWindowsAuthentication />
<IISExpressUseClassicPipelineMode />
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\</SolutionDir>
<RestorePackages>true</RestorePackages>
<UseGlobalApplicationHostFile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand Down Expand Up @@ -126,6 +127,7 @@
</Compile>
<Compile Include="Helpers\MvcThrottleCustomFilter.cs" />
<Compile Include="Helpers\MvcThrottleCustomLogger.cs" />
<Compile Include="Helpers\NginxIpAddressParser.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
Expand Down Expand Up @@ -194,7 +196,7 @@
<AutoAssignPort>True</AutoAssignPort>
<DevelopmentServerPort>53048</DevelopmentServerPort>
<DevelopmentServerVPath>/</DevelopmentServerVPath>
<IISUrl>http://localhost/MvcThrottle.Demo</IISUrl>
<IISUrl>http://localhost:53048/</IISUrl>
<NTLMAuthentication>False</NTLMAuthentication>
<UseCustomServer>False</UseCustomServer>
<CustomServerUrl>
Expand Down

0 comments on commit 5306bed

Please sign in to comment.