zoukankan      html  css  js  c++  java
  • ASP.Net Core Web API使用Nginx部署

    本文作为ASP.Net Core Web API学习的一部分,介绍了如何使用Nginx进行简单的负载配置。

    1、将ASP.Net Core Web API项目发布到不同的服务器

    例如,将项目发布到本地不同的文件夹中。

    2、使用dotnet命令启动已发布的ASP.Net Core Web API服务

    -> dotnet WebApi.dll --urls="http://*:3001" --ip="127.0.0.1" --port=3001

    -> dotnet WebApi.dll --urls="http://*:3002" --ip="127.0.0.1" --port=3002

    -> dotnet WebApi.dll --urls="http://*:3003" --ip="127.0.0.1" --port=3003

    3、安装Nginx

    在http://nginx.org/en/download.html下载安装Nginx稳定版,这里安装的版本是1.18.0。

    4、设置Nginx

    Nginx配置文件路径为nginx-1.18.0\conf\nginx.conf。

    (1) 配置端口

    Nginx启动不了时,可能就是因为端口被占用了,因此可以手动设置Nginx的端口。

    端口位置为nginx.conf -> http -> server -> listen,默认为80端口,可以修改为需要设置的端口。

    (2) 配置基于转发服务器

    server_name  localhost;

    server_name指令可以设置基于域名的虚拟主机,根据请求头部的内容,一个ip的服务器可以配置多个域名,多个域名之间以空格分开。

    (3) 设置代理路径

    location / {

             #root   html;

             #index  index.html index.htm;

             proxy_pass http://webApi; #设置代理转发,转发到服务器列表

    }

    (4) 设置服务器列表

    upstream webApi {

             server localhost:3001;

             server localhost:3002;

             server localhost:3003;

    }

    5、测试Nginx服务器

    假如api接口路径为http://localhost:3001/api/weatherforecast,在浏览器中输入http://localhost:80/api/ weatherforecast,则依次会调用3001、3002、3003端口所在服务器接口,对所有接口轮询调用。

    Nginx默认使用轮询策略转发请求到服务器列表,也可以设置权重等策略。

  • 相关阅读:
    linux MySQL 初始化数据库
    linux 建立 MySQL 账号
    linux MySQL 安装
    Background-Size
    .net文件压缩和解压及中文文件夹名称乱码问题
    C# 浅拷贝与深拷贝区别
    移动端手势库hammerJS 2.0.4官方文档翻译
    期待已久的2012年度最佳jQuery插件揭晓
    Hammer.js
    jQuery Validate验证框架详解
  • 原文地址:https://www.cnblogs.com/xhubobo/p/14397064.html
Copyright © 2011-2022 走看看