Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ConfigureKestrel IPAddress.Loopback does not work #10873

Closed
khteh opened this issue Jun 5, 2019 · 17 comments
Closed

ConfigureKestrel IPAddress.Loopback does not work #10873

khteh opened this issue Jun 5, 2019 · 17 comments
Labels
area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions investigate Needs: Author Feedback The author of this issue needs to respond in order for us to continue investigating this issue.
Milestone

Comments

@khteh
Copy link

khteh commented Jun 5, 2019

I use ConfigureKestrel in order to enable protocol HTTP1.x and HTTP2. However, using IPAddress.Loopback does not work! The browser opens up 127.0.0.1/index.html and show "Page not found"! Modify the URL at the browser address bar to "localhost" works!

To Reproduce

Steps to reproduce the behavior:

  1. Using this version of ASP.NET Core 2.2
  2. Run this code :https://github.com/khteh/AspNetCoreApiStarter
  3. With these arguments : Hit F5 from Visual Studio Code 1.34.0
  4. See error

Expected behavior

A clear and concise description of what you expected to happen.
Browser opens up 127.0.0.1/index.html works as expected.

Screenshots

If applicable, add screenshots to help explain your problem.

Additional context

Add any other context about the problem here.
Include the output of dotnet --info

$ dn --info
.NET Core SDK (reflecting any global.json):
 Version:   2.2.204
 Commit:    8757db13ec

Runtime Environment:
 OS Name:     ubuntu
 OS Version:  19.04
 OS Platform: Linux
 RID:         linux-x64
 Base Path:   /usr/share/dotnet/sdk/2.2.204/

Host (useful for support):
  Version: 2.2.5
  Commit:  0a3c9209c0

.NET Core SDKs installed:
  2.2.204 [/usr/share/dotnet/sdk]

.NET Core runtimes installed:
  Microsoft.AspNetCore.All 2.2.5 [/usr/share/dotnet/shared/Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.App 2.2.5 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 2.2.5 [/usr/share/dotnet/shared/Microsoft.NETCore.App]

To install additional .NET Core runtimes or SDKs:
  https://aka.ms/dotnet-download

Visual Studio Code:

Version: 1.34.0
Commit: a622c65b2c713c890fcf4fbf07cf34049d5fe758
Date: 2019-05-15T21:55:35.507Z
Electron: 3.1.8
Chrome: 66.0.3359.181
Node.js: 10.2.0
V8: 6.6.346.32
OS: Linux x64 5.0.0-15-generic
@Tratcher
Copy link
Member

Tratcher commented Jun 5, 2019

Are you sure you started Kestrel and not IIS Express? IIS Express is the default profile when you hit F5 and it would require the name localhost in the url.

FYI Http1AndHttp2 requires SSL/HTTPS. This should have thrown.

                options.Listen(IPAddress.Loopback, 5000, listenOptions =>
                {
                    listenOptions.Protocols = HttpProtocols.Http1AndHttp2;
                    //listenOptions.UseHttps("testCert.pfx", "testPassword");
                });

@khteh
Copy link
Author

khteh commented Jun 5, 2019

How to configure it to start kerstrel instead if IIS?

@Tratcher
Copy link
Member

Tratcher commented Jun 6, 2019

https://i.stack.imgur.com/MUVJd.png
The second option is named after your project and runs Kestrel.

@khteh
Copy link
Author

khteh commented Jun 6, 2019

Screenshot from 2019-06-06 09-13-54

@Tratcher
Copy link
Member

Tratcher commented Jun 6, 2019

Ah, VS Code... I don't know if that even supports IIS Express.

@khteh
Copy link
Author

khteh commented Jun 16, 2019

Switch to use IPAddress.Any works.

@analogrelay analogrelay added this to the 3.0.0-preview8 milestone Jun 18, 2019
@analogrelay
Copy link
Contributor

RID: linux-x64

Pretty sure it's not IIS Express if we're on Linux ;).

@khteh Can you clarify exactly what the actual behavior is? What URL is in the address bar (is it localhost or 127.0.0.1, it matters because localhost may be resolving to the IPv6 loopback address).

When you specify IPAddress.Loopback, you get 127.0.0.1 which is the IPv4 Loopback Address, there's also IPAddress.IPv6Loopback that specifies the IPv6 Loopback Address ([::1]). Many modern OSes resolve http://localhost as http://[::1] so if you're only binding to the IPv4 address, you'll get this problem. Binding to 0.0.0.0 (IPAddress.Any) binds to both IPv4 and IPv6, which is why it works.

@analogrelay analogrelay added the Needs: Author Feedback The author of this issue needs to respond in order for us to continue investigating this issue. label Jul 9, 2019
@khteh
Copy link
Author

khteh commented Jul 10, 2019

IPAddress.Loopback: http://127.0.0.1:5000/index.html
IPAddress.IPv6Loopback: http://[::1]:5000/ <= This works

$ nslookup
> localhost
Server:		127.0.0.53
Address:	127.0.0.53#53

Non-authoritative answer:
Name:	localhost
Address: 127.0.0.1
Name:	localhost
Address: ::1

@analogrelay
Copy link
Contributor

I'm not sure I understand you exactly here. So when specifying IPAddress.Loopback and opening the browser to http://127.0.0.1 it doesn't work? That's definitely odd and not behavior we've seen before.

The nslookup output is exactly what I'm talking about. Since both an IPv4 and an IPv6 address are present, it's up to the client which address to use. However, if you use IPAddress.Loopback (or IPAddress.IPv6Loopback) you're only listening on one of the two addresses so you could get inconsistent behavior like this.

@khteh
Copy link
Author

khteh commented Jul 10, 2019

http://127.0.0.1:5000 doesn't work.

@analogrelay
Copy link
Contributor

analogrelay commented Jul 16, 2019

What does dotnet run output if you just run it from the command line manually? I can't seem to reproduce this. If I use IPAddress.Loopback, I can access it from a browser on http://127.0.0.1:5000

Also, your launch.json seems to be setting ASPNETCORE_URLS to http://localhost:5000/swagger. That value is not the URL that is started in the browser, it's the root URL for your site. Kestrel doesn't support paths here (since it takes over the whole port, unlike IIS on Windows which allows multiple apps at different paths to share a single port). Plus, when using UseKestrel and configuring endpoints directly, this variable is ignored. This is evident from the warning message being displayed when I dotnet run the app:

14:56:21 [Warning] () Overriding address(es) '"http://localhost:5000/"'. Binding to endpoints defined in "UseKestrel()" instead.

@analogrelay analogrelay removed this from the 3.0.0-preview8 milestone Jul 16, 2019
@khteh
Copy link
Author

khteh commented Jul 17, 2019

Same behavior as I get from launching the application from VSCode IDE by pressing 'F5':

$ dn run --project src/Web.Api

11:14:34 [Information] () User profile is available. Using '"/home/khteh/.aspnet/DataProtection-Keys"' as key repository; keys will not be encrypted at rest.

11:14:35 [Warning] () Overriding address(es) '"http://localhost:5000/"'. Binding to endpoints defined in "UseKestrel()" instead.

11:14:35 [Information] () Start task.
Hosting environment: Development
Content root path: /usr/src/AspNetCoreApiStarter/src/Web.Api
Now listening on: http://127.0.0.1:5000
Application started. Press Ctrl+C to shut down.

11:14:45 [Information] () Startup Background Service has started.

11:14:52 [Information] () Request starting HTTP/1.1 GET http://127.0.0.1:5000/index.html  

11:14:52 [Information] () Method: GET, Scheme: http, PathBase: , Path: /index.html, IP: 127.0.0.1, Host: 127.0.0.1:5000, ContentLength: 

11:14:52 [Warning] () Failed to determine the https port for redirect.

11:14:52 [Information] () Request finished in 652.6528ms 404 

Thanks for higlighting the ASPNETCORE_URLS in launch.json for Kerstrel.

@analogrelay
Copy link
Contributor

11:14:52 [Information] () Method: GET, Scheme: http, PathBase: , Path: /index.html, IP: 127.0.0.1, Host: 127.0.0.1:5000, ContentLength:

11:14:52 [Warning] () Failed to determine the https port for redirect.

11:14:52 [Information] () Request finished in 652.6528ms 404

Ok so Kestrel is indeed binding to the correct IP/port. Something in your middleware pipeline is yielding the 404. Otherwise you wouldn't see these log messages. I'd suggest debugging your middleware pipeline and seeing if there's somewhere that you could be 404ing.

You're requesting index.html, where do you have that file? I don't see it in wwwroot. Is it being served by a middleware? What happens when you just go to http://localhost:5000 (with no path on the end)

@khteh
Copy link
Author

khteh commented Jul 17, 2019

I didn't request for index.html. I point the browser to http://localhost:5000 and then something somehow redirects to that index.html. Strange. That doesn't happen when I use IPAddress.Any.

@analogrelay
Copy link
Contributor

There's a lot of middleware in your sample that could be interfering. I'd suggest trying to scale this back to a minimal set that reproduces the issue. That would help in further investigation. Turning your logging verbosity up in appsettings.Development.json might also help (turn Microsoft to Debug and you'll get more logs from ASP.NET).

@analogrelay analogrelay added this to the Discussions milestone Jul 17, 2019
@dewelloper
Copy link

index.html coming from the swagger. but iam not sure about the error. it interesting with hosting environment

@khteh
Copy link
Author

khteh commented Oct 11, 2019

I reverify this with ASP.Net Core 3.0 and configure Kestrel only in appsettings.json and I do not see the erroneous behaviour as in my original post. However, hitting F5 from Visual Studio Code does not open up browser automatically. This despite the fact that I have launchBrowser:"enabled": true in my .vscode/launch.json. Not a critical error and feel free to close.

@khteh khteh closed this as completed Oct 11, 2019
@dotnet dotnet locked as resolved and limited conversation to collaborators Dec 3, 2019
@amcasey amcasey added area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions and removed area-runtime labels Aug 24, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions investigate Needs: Author Feedback The author of this issue needs to respond in order for us to continue investigating this issue.
Projects
None yet
Development

No branches or pull requests

6 participants