{"action":"created","issue":{"url":"https://api.github.com/repos/aspnet/KestrelHttpServer/issues/961","repository_url":"https://api.github.com/repos/aspnet/KestrelHttpServer","labels_url":"https://api.github.com/repos/aspnet/KestrelHttpServer/issues/961/labels{/name}","comments_url":"https://api.github.com/repos/aspnet/KestrelHttpServer/issues/961/comments","events_url":"https://api.github.com/repos/aspnet/KestrelHttpServer/issues/961/events","html_url":"https://github.com/aspnet/KestrelHttpServer/issues/961","id":164475642,"number":961,"title":"HttpContext.Connection.RemoteIpAddress is null when using Nginx","user":{"login":"hamu01","id":4434200,"avatar_url":"https://avatars.githubusercontent.com/u/4434200?v=3","gravatar_id":"","url":"https://api.github.com/users/hamu01","html_url":"https://github.com/hamu01","followers_url":"https://api.github.com/users/hamu01/followers","following_url":"https://api.github.com/users/hamu01/following{/other_user}","gists_url":"https://api.github.com/users/hamu01/gists{/gist_id}","starred_url":"https://api.github.com/users/hamu01/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hamu01/subscriptions","organizations_url":"https://api.github.com/users/hamu01/orgs","repos_url":"https://api.github.com/users/hamu01/repos","events_url":"https://api.github.com/users/hamu01/events{/privacy}","received_events_url":"https://api.github.com/users/hamu01/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":{"url":"https://api.github.com/repos/aspnet/KestrelHttpServer/milestones/13","html_url":"https://github.com/aspnet/KestrelHttpServer/milestones/Discussions","labels_url":"https://api.github.com/repos/aspnet/KestrelHttpServer/milestones/13/labels","id":1255693,"number":13,"title":"Discussions","description":"","creator":{"login":"DamianEdwards","id":249088,"avatar_url":"https://avatars.githubusercontent.com/u/249088?v=3","gravatar_id":"","url":"https://api.github.com/users/DamianEdwards","html_url":"https://github.com/DamianEdwards","followers_url":"https://api.github.com/users/DamianEdwards/followers","following_url":"https://api.github.com/users/DamianEdwards/following{/other_user}","gists_url":"https://api.github.com/users/DamianEdwards/gists{/gist_id}","starred_url":"https://api.github.com/users/DamianEdwards/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/DamianEdwards/subscriptions","organizations_url":"https://api.github.com/users/DamianEdwards/orgs","repos_url":"https://api.github.com/users/DamianEdwards/repos","events_url":"https://api.github.com/users/DamianEdwards/events{/privacy}","received_events_url":"https://api.github.com/users/DamianEdwards/received_events","type":"User","site_admin":false},"open_issues":17,"closed_issues":7,"state":"open","created_at":"2015-08-14T18:23:15Z","updated_at":"2016-07-08T22:13:39Z","due_on":null,"closed_at":null},"comments":3,"created_at":"2016-07-08T07:39:14Z","updated_at":"2016-07-11T06:16:07Z","closed_at":null,"body":"We have a website developed by asp.net core rc2。We publish the website to linux server, use nginx as reverse proxy. We have added \"proxy_set_header X-Forwarded-For $remote_addr;\" config.The problem is HttpContext.Connection.RemoteIpAddress is null, LocalIpAddress,LocalPort is also null, we cannot get the value of these properties.  \r\n\r\nNginx config:\r\n```\r\nserver{\r\n    listen 80;\r\n    server_name  localhost;\r\n\r\n    location / {\r\n        proxy_pass http://unix:/var/aspnet/kestrel.sock;\r\n        proxy_http_version 1.1;\r\n        proxy_set_header Upgrade $http_upgrade;\r\n        proxy_set_header Connection keep-alive;\r\n        proxy_set_header Host $host:80;\r\n        proxy_set_header X-Forwarded-For $remote_addr;\r\n        proxy_set_header X-Original-For $server_addr;\r\n        proxy_cache_bypass $http_upgrade;\r\n    }\r\n}\r\n```\r\n\r\nProgram.cs(simplified)\r\n```\r\npublic class Program\r\n{\r\n        public static void Main(string[] args)\r\n        {\r\n            IConfiguration configuration = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile(\"host.json\").Build();\r\n            var host = new WebHostBuilder()\r\n                .UseConfiguration(configuration)\r\n                .UseKestrel()\r\n                .UseContentRoot(Directory.GetCurrentDirectory())\r\n                .UseIISIntegration()\r\n                .UseStartup<Startup>()\r\n                .Build();\r\n\r\n            host.Run();\r\n        }\r\n }\r\n```\r\n\r\nStartup.cs(simplified)\r\n```\r\npublic class Startup\r\n{\r\n        public void ConfigureServices(IServiceCollection services)\r\n        {\r\n        }\r\n\r\n        public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory)\r\n        {\r\n            app.Run(async (context) =>\r\n                {\r\n                    try\r\n                    {\r\n                        StringBuilder requestBuilder = new StringBuilder();\r\n                        var remoteIp = context.Connection.RemoteIpAddress;\r\n                        string remoteIpstring = remoteIp != null ? remoteIp.ToString() : \"-\";\r\n                        requestBuilder.Append(string.Format(\"remoteIp={0};\", remoteIpstring));\r\n                        \r\n                        var serverIp = context.Connection.LocalIpAddress;\r\n                        string serverIpstring = serverIp != null ? serverIp.ToString() : \"-\";\r\n                        requestBuilder.Append(string.Format(\"serverIp={0};\", serverIpstring));\r\n\r\n                        string serverPort = context.Connection.LocalPort.ToString();\r\n                        requestBuilder.Append(string.Format(\"serverPort={0};\", serverPort));\r\n\r\n                        await context.Response.WriteAsync(requestBuilder.ToString());\r\n                    }\r\n                    catch (Exception e)\r\n                    {\r\n                        Console.WriteLine(e);\r\n                    }\r\n                });\r\n        }\r\n }\r\n```\r\n\r\nOS：Centos 7.1\r\nNginx version：1.10.1\r\n.NET Core version：RC2"},"comment":{"url":"https://api.github.com/repos/aspnet/KestrelHttpServer/issues/comments/231650397","html_url":"https://github.com/aspnet/KestrelHttpServer/issues/961#issuecomment-231650397","issue_url":"https://api.github.com/repos/aspnet/KestrelHttpServer/issues/961","id":231650397,"user":{"login":"hamu01","id":4434200,"avatar_url":"https://avatars.githubusercontent.com/u/4434200?v=3","gravatar_id":"","url":"https://api.github.com/users/hamu01","html_url":"https://github.com/hamu01","followers_url":"https://api.github.com/users/hamu01/followers","following_url":"https://api.github.com/users/hamu01/following{/other_user}","gists_url":"https://api.github.com/users/hamu01/gists{/gist_id}","starred_url":"https://api.github.com/users/hamu01/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hamu01/subscriptions","organizations_url":"https://api.github.com/users/hamu01/orgs","repos_url":"https://api.github.com/users/hamu01/repos","events_url":"https://api.github.com/users/hamu01/events{/privacy}","received_events_url":"https://api.github.com/users/hamu01/received_events","type":"User","site_admin":false},"created_at":"2016-07-11T06:16:06Z","updated_at":"2016-07-11T06:16:06Z","body":"@Tratcher Thx, it works now.\r\nBut the forward configuration really confuse me. I have following questions need to be answered:\r\n1. If I use IIS as reverse proxy, asp.net core rc2 website do not need add the forward middleware to the pipeline. In that situation HttpContext.Connection.RemoteIpAddress is not null, I still can get the remoteip value. Why?It seems the behavior in IIS and Nginx is inconsistent\r\n2. The context.Connection.LocalIpAddress and LocalPort is also inconsistent when use IIS as reverse proxy and when only use kestrel server, donot use the IIS as reverse proxy. If the asp.net core rc2 website get the real remoteip in IIS, then it also should get the real serverip, I think.\r\n3. Only specify ForwardedHeadersOptions.ForwardedHeaders = ForwardedHeaders.XForwardedFor works, if specify ForwardedHeaders.All, HttpContext.Connection.RemoteIpAddress is still null, I think when specify ForwardedHeaders.All, it means forward the x-forward-for,x-forward-host and x-forward-protocol, so HttpContext.Connection.RemoteIpAddress shouldnot be null. The real case seems to forward one of them, not all of them.\r\n\r\n(If any of the question fixed in RTM version, please ignore it, I have not upgraded to RTM version)\r\n\r\nBellow is the demo code and output：\r\n1. Startup.cs(simplified)，display the HttpContext.Connection's property and all the headers\r\n```csharp\r\npublic class Startup\r\n{\r\n        public void ConfigureServices(IServiceCollection services)\r\n        {\r\n        }\r\n\r\n        public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory)\r\n        {\r\n            //ForwardedHeadersOptions options = new ForwardedHeadersOptions();\r\n            //options.ForwardedHeaders = ForwardedHeaders.XForwardedFor;\r\n            //app.UseForwardedHeaders(options);\r\n            app.Run(async (context) =>\r\n                {\r\n                    try\r\n                    {\r\n                        StringBuilder requestBuilder = new StringBuilder();\r\n                        var remoteIp = context.Connection.RemoteIpAddress;\r\n                        string remoteIpstring = remoteIp != null ? remoteIp.ToString() : \"-\";\r\n                        requestBuilder.Append(string.Format(\"remoteIp={0};\", remoteIpstring));\r\n\r\n                        string remotePort = context.Connection.RemotePort.ToString();\r\n                        requestBuilder.Append(string.Format(\"remotePort={0};\", remotePort));\r\n\r\n                        var serverIp = context.Connection.LocalIpAddress;\r\n                        string serverIpstring = serverIp != null ? serverIp.ToString() : \"-\";\r\n                        requestBuilder.Append(string.Format(\"serverIp={0};\", serverIpstring));\r\n\r\n                        string serverPort = context.Connection.LocalPort.ToString();\r\n                        requestBuilder.Append(string.Format(\"serverPort={0};\", serverPort));\r\n\r\n                        foreach (string key in context.Request.Headers.Keys)\r\n                        {\r\n                            string value = context.Request.Headers[key];\r\n                            requestBuilder.Append(string.Format(\"{0}={1};\", key, value));\r\n                        }\r\n\r\n                        await context.Response.WriteAsync(requestBuilder.ToString());\r\n                    }\r\n                    catch (Exception e)\r\n                    {\r\n                        Console.WriteLine(e);\r\n                    }\r\n                });\r\n        }\r\n    }\r\n```\r\n2. use IIS, the output is: \r\n```\r\nremoteIp=10.200.200.113;remotePort=62242;serverIp=127.0.0.1;serverPort=30791;Cache-Control=no-cache;Connection=Keep-Alive;Accept=text/html, application/xhtml+xml, */*;Accept-Encoding=gzip, deflate;Accept-Language=zh-CN;Host=10.200.50.201:6000;User-Agent=Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko;MS-ASPNETCORE-TOKEN=7c45ff68-2c0d-499e-bb53-f89ab8c33554;X-Original-Proto=http;X-Original-For=127.0.0.1:51137;\r\n```\r\n\r\nYou can see that I can get the context.Connection.RemoteIpAddress value, but I donot add the forwardmiddle to the pipeline.\r\n\r\n3.not use IIS, only use the kestrel server,listening on the 5000 port, the output is:\r\n```csharp\r\nremoteIp=10.200.200.113;remotePort=63149;serverIp=10.200.50.201;serverPort=5000;Cache-Control=no-cache;Connection=Keep-Alive;Accept=text/html, application/xhtml+xml, */*;Accept-Encoding=gzip, deflate;Accept-Language=zh-CN;Host=10.200.50.201:5000;User-Agent=Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko;\r\n```\r\nYou can see in this situation, the context.Connection.LocalIpAddress isnot 127.0.0.1, it is the server Intranet IP. But in previous situation the context.Connection.LocalIpAddress is 127.0.0.1\r\n"}}
{"actor":{"display_login":"hamu01"}}