NetCore + Nginx 简单应用

281 阅读1分钟

NetCore + Nginx 简单应用

1. Demo项目搭建

  • 搭建本地集群

    • 创建.netCore 项目

    • Contronller代码如下

      public class HomeController : ControllerBase
          {
              private readonly ILogger<HomeController> _logger;
              private IConfiguration _iConfiguration;
      
              public HomeController(ILogger<HomeController> logger, IConfiguration configuration)
              {
                  this._logger = logger;
                  this._iConfiguration = configuration;
              }
              [HttpGet("Home/Get")]
              public IEnumerable<string> Get()
              {
                  return new string[] {"value","value2",this._iConfiguration["ip"],this._iConfiguration["port"] };
              }
              [HttpGet]
              public string GetOfId(int id)
              {
                  return $@"{id}_{this._iConfiguration["ip"]}:{this._iConfiguration["port"]}";
              }
          }
      

2. Nginx 下载与配置

  • 下载 Nginx 的页面

    • 下载链接Nginx

      我选的是 Mainline version下面的版本

  • 配置 nginx.conf 文件

    • nginx-1.21.3\conf文件下面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{//WebApi 值配置的名字,配置的本地绑定的程序地址
             server 127.0.0.1:8000;
             server 127.0.0.1:8001;
             server 127.0.0.1:8002;
          }
          server {//这个是Nginx 的配置项
              listen       8080;//这个是Nginx 的配置项Ip
              server_name  localhost;
      
              #charset koi8-r;
      
              #access_log  logs/host.access.log  main;
      
              location / {
                  proxy_pass http://WebApi;//这里需要填写
                  root   html;
                  index  index.html index.htm;
              }
      
              #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;
          #    }
          #}
      
      }
      
      

3. 测试

  • 启动API

    • 在项目子目录启动cmd

      NginxDemo 是我的项目名字

      dotnet NginxDemo.dll --urls="http://*:8000" --ip="127.0.0.1" --port=8000
      dotnet NginxDemo.dll --urls="http://*:8001" --ip="127.0.0.1" --port=8001
      dotnet NginxDemo.dll --urls="http://*:8002" --ip="127.0.0.1" --port=8002
      
  • 启动 nginx.exe

    启动 nginx.exe程序

  • 访问 127.0.0.1:8080

    nginx的这里配置的运行机制为: 监听localhost的8080端口,接受到请求转发到webapi。webapi里面定义了3个端口的集群。会轮询转发给配置的集群。

    niginx 默认设置轮询访问