首页 > 解决方案 > ASP .netcore WebAPI 只处理本地请求

问题描述

嗨,我在本地/来自同一 IP 的请求上返回 200,但是当我将手机热点更改 IP 并发出请求时出现以下错误

HTTPConnectionPool(host={myIPAddress}, port=80): Max retries exceeded ..... 无法建立新连接: [WinError 10060] 连接尝试失败,因为连接方在一段时间后没有正确响应,或建立连接失败,因为连接的主机未能响应'

程序.cs文件

public static void Main(string[] args)
{
    CreateWebHostBuilder(args).Build().Run();
}

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
    WebHost.CreateDefaultBuilder(args)
        .UseStartup<Startup>()
        .UseUrls("http://0.0.0.0:5000/")
        .UseKestrel(serverOptions =>
        {
            //Set properties and call methods on serverOptions
            serverOptions.Limits.KeepAliveTimeout = TimeSpan.FromMinutes(2);
            serverOptions.Limits.MaxConcurrentConnections = 100;
            serverOptions.Limits.MaxConcurrentUpgradedConnections = 100;
            serverOptions.Limits.MaxRequestBodySize = 10 * 1024;
            serverOptions.Limits.RequestHeadersTimeout = TimeSpan.FromMinutes(1);
        });
}

NGINX - 站点可用/默认

server {
listen 80;
listen [::]:80;
server_name   HFTest;

location / {
    proxy_pass         http://localhost:5000; **#edited as per comments**
    proxy_http_version 1.1;
    proxy_set_header   Upgrade $http_upgrade;
    proxy_set_header   Connection keep-alive;
    proxy_set_header   Host $host;
    proxy_cache_bypass $http_upgrade;
    proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header   X-Forwarded-Proto $scheme;
}

端口状态 - sudo netstat -tulpn | grep 听

tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      635/nginx: master p
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      628/sshd
tcp        0      0 0.0.0.0:5000            0.0.0.0:*               LISTEN      2048/API/HFTest

关于接下来要解决什么问题的任何想法?

谢谢

编辑:

请求 - 在 python 中

scopes = 'openid'
url='http://{IPAddress}:80/connect/token'

session = OAuth2Session(clientID,clientSecret, scope=scopes)
token = session.fetch_access_token(url,verify=False)

endpoint = "http://{IPAddress}/{endpoint}"

headers = {"Authorization": "Bearer %s" % token['access_token'],"Accept": "text/xml"}

session = requests.Session()

response = session.request("GET",endpoint,headers=headers,verify = False)
print(response.status_code)
print(response.text)

标签: linuxnginxasp.net-core-webapiraspberry-pi4

解决方案


nginx 配置中的上游服务 ip 需要是实际的 ip 地址。你可以试试localhost ip如下

…
location / {
    proxy_pass         http://127.0.0.1:5000;
…

推荐阅读