当前位置: 首页 > news >正文

netcore8.0项目部署到windows服务器中(或个人windows电脑),利用nginx反向代理

1、发布netcore项目,默认即可

1.1、前提,需在appsettings添加Kestrel代理

配置如下:

{"Kestrel": {"Endpoints": {"http": {"Url": "http://localhost:7022"},"Https": {"Url": "https://localhost:7023","Certificate": {"Path": "C:\\Users\\Administrator\\Desktop\\nginx-1.27.4\\localhost.pfx","Password": "123456"}}}},"Logging": {"LogLevel": {"Default": "Information","Microsoft.AspNetCore": "Warning"}},"AllowedHosts": "*"
}

1.2、在Program.cs中添加:

// 添加Nginx反向代理支持
builder.Services.Configure<ForwardedHeadersOptions>(options =>
{options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;options.KnownNetworks.Clear();options.KnownProxies.Clear();
});
var app = builder.Build();app.UseForwardedHeaders();// 使用转发头中间件

1.3、发布

将publish文件夹复制到服务器

接下来服务器操作

2、安装dotnet-hosting-8.0.14-win,下载链接

下载 ASP.NET Core 8.0 Runtime (v8.0.14) - Windows Hosting Bundle Installer

3、下载nginx,解压,将nginx注册为服务,方便实现自启动

winsw/winsw: A wrapper executable that can run any executable as a Windows service, in a permissive license.

3.1按需求修改\nginx-1.27.4\conf,示例:


#user  nobody;
worker_processes  1;#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;#pid        logs/nginx.pid;events {worker_connections  1024;
}http {include       mime.types;default_type  application/octet-stream;#log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '#                  '$status $body_bytes_sent "$http_referer" '#                  '"$http_user_agent" "$http_x_forwarded_for"';#access_log  logs/access.log  main;sendfile        on;#tcp_nopush     on;#keepalive_timeout  0;keepalive_timeout  65;#gzip  on;server {listen       88;server_name  localhost;#charset koi8-r;#access_log  logs/host.access.log  main;#将http重定向https这里要用IP地址,不然无法实现跳转,如果用localhost就只能本机跳转,如果不要要https可以注释下面语句return 301 https://192.168.1.5:89;location / {proxy_pass         http://localhost:7022;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;# 增加超时设置proxy_connect_timeout       600;proxy_send_timeout          600;proxy_read_timeout          600;send_timeout                600;}#error_page  404              /404.html;# redirect server error pages to the static page /50x.html#error_page   500 502 503 504  /50x.html;location = /50x.html {root   html;}# proxy the PHP scripts to Apache listening on 127.0.0.1:80##location ~ \.php$ {#    proxy_pass   http://127.0.0.1;#}# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000##location ~ \.php$ {#    root           html;#    fastcgi_pass   127.0.0.1:9000;#    fastcgi_index  index.php;#    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;#    include        fastcgi_params;#}# deny access to .htaccess files, if Apache's document root# concurs with nginx's one##location ~ /\.ht {#    deny  all;#}}# another virtual host using mix of IP-, name-, and port-based configuration##server {#    listen       8000;#    listen       somename:8080;#    server_name  somename  alias  another.alias;#    location / {#        root   html;#        index  index.html index.htm;#    }#}# HTTPS server#server {listen       89 ssl;server_name  localhost;#注意证书路径 使用“/”ssl_certificate      C:/Users/Administrator/Desktop/nginx-1.27.4/localhost.pem;ssl_certificate_key  C:/Users/Administrator/Desktop/nginx-1.27.4/localhost.key;ssl_session_cache    shared:SSL:1m;ssl_session_timeout  5m;ssl_ciphers  HIGH:!aNULL:!MD5;ssl_prefer_server_ciphers  on;location / {proxy_pass https://localhost:7023;proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;}}}

3.2、将利用openssl生成的证书复制到nginx-1.27.4目录下

最后的配置如下

4、启动项目

4.1、netcore项目

4.2启动nginx服务:

5、测试

在客户端用浏览器测试

https://192.168.1.5:89/

如果访问 http://192.168.1.5:88/ 会自动重定向https://192.168.1.5:89/

 netcore项目源码见

githubK4Android/docktestapp 

gitee文艺不会丢失./docktestapp

相关文章:

  • 解决 EasyExcel 填充图片占满单元格问题
  • javascript全栈开发之旅01
  • Spring-Framework源码环境搭建
  • window和ubuntu自签证书
  • Node.js 应用场景
  • vue3中nextTick的作用及示例
  • Asp.Net Core 基于(asp.net core 2.2) 创建asp .net core空项目
  • vite+vue2+elementui构建之 vite.config.js
  • 优化算法
  • uniapp小程序开发入门01-快速搭建一个空白的项目并预览它
  • 用C语言实现——一个中缀表达式的计算器。支持用户输入和动画演示过程。
  • 文档编辑:reStructuredText全面使用指南 — 第一部分 介绍
  • 百度打响第一枪!通用超级智能体时代,真的来了
  • Spring 与 ActiveMQ 的深度集成实践(二)
  • OpenCv高阶(九)——背景建模
  • playwright的简单使用
  • Linux实验课
  • SQL进阶知识:九、高级数据类型
  • 【Pandas】pandas DataFrame rsub
  • 在构造函数内部和外部定义的方法区别
  • 调查丨当节气出现“时差”,陕西的果农们经历着什么?
  • 两部门:推动“青年驿站”为毕业生跨地区求职提供住宿优惠便利
  • 从篆刻书画到装帧设计,再看钱君匋的“艺兼众美”
  • 山西10岁男孩遭生母和继父殴打遇害,案件庭审延期
  • 上海银行一季度净赚逾62亿增2.3%,不良贷款率与上年末持平
  • 云南富源回应“岔河水库死鱼”事件: 初步研判与水体缺氧有关