我正在尝试使用以下 appsettings.json 在 Ubuntu 上运行 .NET:
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"ConnectionStrings": {
"DefaultConnection": "Server=1.2.3.5:1234;Database=somedomain.com;Uid=default;Pwd=password;"
},
"Kestrel": {
"Endpoints": {
"Https": {
"Url": "https://localhost:7000",
"Certificate": {
"Path": "/etc/letsencrypt/live/somedomain.com/cert.pem",
"KeyPath": "/etc/letsencrypt/live/somedomain.com/privkey.pem"
}
}
}
}
}
以及以下程序.cs:
using Microsoft.AspNetCore.HttpOverrides;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers();
builder.Services.AddRazorPages();
var app = builder.Build();
app.UseForwardedHeaders(new ForwardedHeadersOptions
{
ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
});
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
//app.UseHttpsRedirection();
}
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.MapControllers();
app.MapRazorPages();
app.Run();
它启动正常,但如果我打开端口 7000,那么即使
telnet
也无法连接到 Kestrel。但是,运行 lsof -i:7000
显示它正在侦听该端口。给什么?
更新:
从外部运行 telnet 不起作用,但从本地主机运行就可以了。 如果 Nginx 正在运行并且我尝试在浏览器中连接到端口 443,它会返回“403 Forbidden”。这是该站点的 Nginx 配置:
limit_req_zone $binary_remote_addr zone=one:10m rate=5r/s;
server_tokens off;
upstream somedomain.com
{
server 127.0.0.1:7000;
}
server
{
root /var/www/somedomain.com;
server_name somedomain.com;
listen [::]:443 ssl ipv6only=on; # managed by Certbot
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/somedomain.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/somedomain.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
add_header X-Frame-Options DENY;
add_header X-Content-Type-Options nosniff;
#Redirects all traffic
location /
{
proxy_pass https://somedomain.com;
limit_req zone=one burst=10 nodelay;
try_files $uri $uri/ =404;
}
}
server
{
if ($host = somedomain.com)
{
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80 default_server;
listen [::]:80 default_server;
server_name somedomain.com;
return 404; # managed by Certbot
}
更新2:
我使用以下内容更新了 NGINX 配置,但问题仍然存在:
location / {
proxy_pass https://127.0.0.1:7000/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
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;
}
好的,我通过绑定到
*
而不是 localhost
解决了所描述的问题:
"Kestrel": {
"Endpoints": {
"Http": {
"Url": "http://*:7000"
}
}
}
现在仍然有错误,但是项目中我的代码出现了异常,而不是简单地不响应请求。所以我会认为这个案子结束了。