net core+webapi+nginx的基本使用(win10版)

net core+webapi+nginx的基本使用(win10版)

一、nginx的基本使用

1、nginx详解
2、访问"https://nginx.org/en/download.html"下载Nginx。

3、在本地解压Nginx压缩包,找到"conf"->“nginx.conf”,右键打开nginx.conf,server中的listen表示nginx监听的端口,默认为80,这里我改为8081(#表示注释,不使用80是因为本地有其他项目使用该端口)
在这里插入图片描述
4、其中下图圈起来的是nginx执行程序,双击运行即可(会看到命令行一闪而过)
在这里插入图片描述
5、浏览器中访问"http://localhost:8081",可看到下图所示的界面,该界面放在html文件夹中(注:现在vue等单体应用,使用nginx时都是将build生成的dist文件夹中的文件复制到html文件中,就可直接访问)
在这里插入图片描述
6、nginx常用命令:

1、查看nginx帮助信息:nginx -h
2、查看当前nginx版本:nginx -v
3、启动nginx:start nginx
4、关闭nginx(退出前完成已经接受的连接请求):nginx -s quit
5、关闭nginx(快速关闭,不管有没有正在处理的请求):nginx -s stop
6、重启nginx:nginx -s reload

二、创建webapi项目

7、打开VS2019,创建一个名为"NginxTest"的web api项目,如下图:
在这里插入图片描述
8、将WeatherForecast控制器中的Get()方法内容 做如下更改,其中使用到的IConfiguration可用来获取执行命令时指定的参数或appsetting.json中的参数,后面通过命令行部署net core应用时会传递参数,以做区分。

using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace NginxTest.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class WeatherForecastController : ControllerBase
    {
        private static readonly string[] Summaries = new[]
        {
            "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
        };

        private readonly ILogger<WeatherForecastController> _logger;

        private IConfiguration _configuration;

        public WeatherForecastController(ILogger<WeatherForecastController> logger, IConfiguration configuration)
        {
            _logger = logger;
            _configuration = configuration;
        }
        
        [HttpGet]
        public string Get()
        {
            //获取启动时的IP和Port(端口)
            string json = "NginxTest:" + _configuration["ip"] + ":" + _configuration["port"];
            return json;
        }
    }
}

9、右键重新生成"NginxTest"项目,重新生成成功后,右键"在文件资源管理器中打开文件夹(X)“来到项目所在目录,进入"bin/debug"文件夹,可看到生成的"NginxTest.dll”,在路径中输入"cmd"(即CMD进入该路径)
在这里插入图片描述
10、在命令行中输入如下命令,其中urls表示服务部署在666端口,–port和ip表示传递的参数,可通过上面讲到的IConfiguration获取到(注:–port和ip可写随意写,这是我们传递的参数,这样写是后面我们开多个微服务时起一定的标识作用)

dotnet NginxTest.dll --urls="http://*:666" --port=666  --ip="127.0.0.1"
dotnet NginxTest.dll --urls="http://*:777" --port=777  --ip="127.0.0.1"

在这里插入图片描述

三、使用nginx代理webapi 服务

11、打开nginx中的"conf"文件夹中的nginx.conf文件,做如下更改:

//1、在server上增加upstream webapi{}块,webapi是自定义的名称,其中存放的server是我们
upstream webApi {   
      server 127.0.0.1:666;
      server 127.0.0.1:777;
    }
//2、将server中的监听端口更改为8081,location中配置上面定义的服务器列表
location / {
        proxy_pass http://webApi;   #请求转向webApi 定义的服务器列表
}
//3、重新启动nginx  在nginx.exe所在路径进入cmd执行如下指令即可
nginx -s reload

12、浏览器中访问"http://localhost:8081/weatherforecast",并刷新几次,可看到输出内容的端口在666和777之间转换(注:nginx默认的策略就是轮询,即服务器列表中的服务会依次循环访问),如下图:
在这里插入图片描述
11、打开nginx.conf中文件,更改服务器列表中对应服务的权重,如下更改666端口的权重为2,777端口权重为1,并使用"nginx -s reload"命令重启nginx服务器。

upstream webApi {   
      server 127.0.0.1:666 weight=2;
      server 127.0.0.1:777 weight=1;
    }

13、访问8081端口时,每次轮询,666端口是两次,777是1次(可通过不断刷新界面进行观察),如下图:
在这里插入图片描述
14、完成的nginx.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;

    upstream webApi {   
      server 127.0.0.1:666 weight=2;
      server 127.0.0.1:777 weight=1;
    }

    server {
        listen       8081;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
        proxy_pass http://webApi;   #请求转向webApi 定义的服务器列表
        }

        #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       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}
  • 6
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值