linux - 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)
解决方案
nginx 配置中的上游服务 ip 需要是实际的 ip 地址。你可以试试localhost ip如下
…
location / {
proxy_pass http://127.0.0.1:5000;
…
推荐阅读
- javascript - 是否可以使用检索历史记录(或文档状态)作为浏览器中的导航?
- javascript - 在 ruby rails html 视图中使用 Ajax 表单更改图标“like”按钮类
- apache-kafka - 为什么 kafka 集群会出现错误“活动代理的数量 '0' 不符合所需的复制因子”?
- python - 使用 Python DataFrame 尝试提取以某个数字开头的列的所有行
- c# - 使用leadTools从图像c#创建电影
- android-studio - 如何在 Android Studio 中增加弹出窗口的字体大小?
- c# - 在 Visual Studio IDE 中运行测试时单元测试通过,但在使用 vstest.console.exe 和 Microsoft Fakes 时失败
- java - 在后台使用 JobService 时线程停止运行
- json - JSON数组比较自定义错误(sky-screamer)
- jquery - 如何使用数据表显示选择框特定项目的数据