要将ASP.NET Core应用程序在nginx + Ubuntu上重定向到SSL端口,您可以按照以下步骤进行操作:
Startup.cs
文件的ConfigureServices
方法中添加以下代码来完成:services.AddHttpsRedirection(options =>
{
options.RedirectStatusCode = StatusCodes.Status307TemporaryRedirect;
options.HttpsPort = ;
});
请将
替换为您实际使用的SSL端口号。
sudo apt update
sudo apt install nginx
/etc/nginx/sites-available/default
并进行以下更改:server {
listen 80 default_server;
listen [::]:80 default_server;
server_name your_domain.com;
location / {
return 301 https://$host$request_uri;
}
}
请将your_domain.com
替换为您的域名。
sudo systemctl restart nginx
现在,当用户访问您的应用程序时,他们将自动重定向到SSL端口。
请注意,您还需要为您的域名购买有效的SSL证书,并将其配置到nginx中。这可以通过在nginx配置文件中添加以下代码来完成:
server {
listen 443 ssl;
listen [::]:443 ssl;
server_name your_domain.com;
ssl_certificate /path/to/your/certificate.crt;
ssl_certificate_key /path/to/your/private_key.key;
location / {
proxy_pass http://localhost:;
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;
}
}
请将your_domain.com
替换为您的域名,/path/to/your/certificate.crt
和/path/to/your/private_key.key
替换为您的SSL证书和私钥的实际路径,以及
替换为您的ASP.NET Core应用程序实际使用的端口号。
保存并关闭文件后,使用以下命令重启nginx:
sudo systemctl restart nginx
现在,您的ASP.NET Core应用程序应该在nginx + Ubuntu上正确重定向到SSL端口。
上一篇:ASP.NET Core应用程序在没有任何请求的情况下CPU和内存逐渐增加。
下一篇:ASP.NET Core应用程序在NtWaitForSingleObject中挂起,并有16000多个线程 - 它们在做什么?