赞
踩
目录
准备服务器【Alibaba Cloud Linux 3.2104 LTS 64位 快速启动版】
我之前写过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那个很麻烦找配套。
创建完毕
这里我用Test作为控制器名称。
【*】代表所有IP都能访问
- using Microsoft.AspNetCore.Http;
- using Microsoft.AspNetCore.Mvc;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
-
- namespace APIDemo20221020.Controllers
- {
- [Route("api/[controller]/[action]")]
- [ApiController]
- public class TestController : ControllerBase
- {
- public Object GetInfo() {
- Dictionary<String, Object> map = new Dictionary<string, object>();
- map.Add("state",true);
- map.Add("message","访问成功");
- List<string> list=new List<string>();
- for (int i = 0; i < 100; i++)
- {
- list.Add(((char)(30000 + i)).ToString());
- }
- map.Add("result", list);
- return map;
- }
- }
- }

ASP.NET Core微服务(三)——【跨域配置】_红目香薰的博客-CSDN博客
直接在Startup.cs中添加代码即可。
访问:http://localhost:5000/api/Test/GetInfo
访问成功
我的项目名叫做【APIDemo20221020】所以,应用程序叫做【APIDemo20221020.dll】
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
.NET SDK 使你可以通过 .NET 开发应用。 如果安装 .NET SDK,则无需安装相应的运行时。 若要安装 .NET SDK,请运行以下命令:这个版本支持.NET Core
sudo yum install aspnetcore-runtime-3.1 -y
不是很大,一会就安装完毕了。
dotnet --list-runtimes
上传linux-x64位版本的项目包
使用XFTP上传
yum install nginx -y
由于nginx不是自动启动的,需要咱们启动一下。
systemctl status nginx.service
- systemctl start nginx.service
- systemctl status nginx.service
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里找不常用的随便用都行。
- server {
- listen 8080;
- # listen [::]:80;
- server_name _;
- # root /usr/share/nginx/html;
- location / {
- proxy_pass http://localhost:5000;
- 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;
- }
- root /root/linux-x64;
- # Load configuration files for the default server block.
- include /etc/nginx/default.d/*.conf;
- error_page 404 /404.html;
- location = /40x.html {
- }
- error_page 500 502 503 504 /50x.html;
- location = /50x.html {
- }
- }

重新启动并运行【do】
- systemctl restart nginx.service
- dotnet APIDemo20221020.dll
访问成功
如果访问不了就行没开安全组:
我这里是全部,肯定是能访问的。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。