当前位置:   article > 正文

.NetCore项目nginx发布_.net core nginx

.net core nginx

目录

环境:

服务器型号

项目创建:

添加自定义的API控制器

修改访问路径

示例代码

配置跨域

启动测试

项目发布

发布的项目

准备服务器【Alibaba Cloud Linux 3.2104 LTS 64位 快速启动版】

链接linux服务器

安装ASP.Net Core3.1环境

安装 SDK

.NET版本验证

直接启动

安装nginx

查看nginx.service状态命令:

启动nginx.service

nginx文件位置

公网访问


我之前写过docker的,这里写一下nginx的发布方法:

环境:

系统:win10专业版

开发工具:Visual Studio 2019

.NET环境:.NET Core 3.1

项目:ASP.NET Core Web API

运行系统:Alibaba Cloud Linux 3.2104 LTS 64位 快速启动版

服务器:nginx

服务器型号

 

项目创建:

这里选择.NET Core 3.1版本,因为只有它是长期支持的,如果是vs2022版本是.NET Core 6.0那个很麻烦找配套。 

创建完毕 

添加自定义的API控制器

这里我用Test作为控制器名称。 

修改访问路径

【*】代表所有IP都能访问

示例代码

  1. using Microsoft.AspNetCore.Http;
  2. using Microsoft.AspNetCore.Mvc;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Threading.Tasks;
  7. namespace APIDemo20221020.Controllers
  8. {
  9. [Route("api/[controller]/[action]")]
  10. [ApiController]
  11. public class TestController : ControllerBase
  12. {
  13. public Object GetInfo() {
  14. Dictionary<String, Object> map = new Dictionary<string, object>();
  15. map.Add("state",true);
  16. map.Add("message","访问成功");
  17. List<string> list=new List<string>();
  18. for (int i = 0; i < 100; i++)
  19. {
  20. list.Add(((char)(30000 + i)).ToString());
  21. }
  22. map.Add("result", list);
  23. return map;
  24. }
  25. }
  26. }

配置跨域

ASP.NET Core微服务(三)——【跨域配置】_红目香薰的博客-CSDN博客

直接在Startup.cs中添加代码即可。

启动测试

访问:http://localhost:5000/api/Test/GetInfo

访问成功

项目发布

 

 

发布的项目

我的项目名叫做【APIDemo20221020】所以,应用程序叫做【APIDemo20221020.dll】

准备服务器【Alibaba Cloud Linux 3.2104 LTS 64位 快速启动版】

链接linux服务器

安装ASP.Net Core3.1环境

Download .NET (Linux, macOS, and Windows)

选择.NET Core 3.1版本

选择图片中标注红色外框的选项。 

选择脚本安装

将 Microsoft 包签名密钥添加到受信任密钥列表,并添加 Microsoft 包存储库。

sudo rpm -Uvh https://packages.microsoft.com/config/centos/7/packages-microsoft-prod.rpm

安装 SDK

.NET SDK 使你可以通过 .NET 开发应用。 如果安装 .NET SDK,则无需安装相应的运行时。 若要安装 .NET SDK,请运行以下命令:这个版本支持.NET Core

sudo yum install aspnetcore-runtime-3.1 -y

不是很大,一会就安装完毕了。

 

.NET版本验证

dotnet --list-runtimes

上传linux-x64位版本的项目包

使用XFTP上传

直接启动

安装nginx

yum install nginx -y

由于nginx不是自动启动的,需要咱们启动一下。

查看nginx.service状态命令:

systemctl status nginx.service

启动nginx.service

  1. systemctl start nginx.service
  2. systemctl status nginx.service

nginx文件位置

nginx 日志文件 /var/log/nginx
nginx配置文件目录 /etc/nginx
nginx 可执行文件 /usr/sbin/nginx
nginx环境配置 /etc/sysconfig/nginx
nginx默认站点目录 /usr/share/nginx/html

公网访问

修改配置

vi /etc/nginx/nginx.conf

修改到项目路径,输入【i】进行insert模式,修改完毕后按【ESC】加【:wq】进行保存。

这里看到有【;】,不要忘记。保存并重新启动。

这里我给了我修改server的全部编码。当然,端口号是65535里找不常用的随便用都行。

  1. server {
  2. listen 8080;
  3. # listen [::]:80;
  4. server_name _;
  5. # root /usr/share/nginx/html;
  6. location / {
  7. proxy_pass http://localhost:5000;
  8. proxy_http_version 1.1;
  9. proxy_set_header Upgrade $http_upgrade;
  10. proxy_set_header Connection keep-alive;
  11. proxy_set_header Host $host;
  12. proxy_cache_bypass $http_upgrade;
  13. }
  14. root /root/linux-x64;
  15. # Load configuration files for the default server block.
  16. include /etc/nginx/default.d/*.conf;
  17. error_page 404 /404.html;
  18. location = /40x.html {
  19. }
  20. error_page 500 502 503 504 /50x.html;
  21. location = /50x.html {
  22. }
  23. }

 

重新启动并运行【do】

  1. systemctl restart nginx.service
  2. dotnet APIDemo20221020.dll

访问成功

如果访问不了就行没开安全组: 

我这里是全部,肯定是能访问的。 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/232381
推荐阅读
相关标签
  

闽ICP备14008679号