Nginx负载均衡,配置轮询

1.建立3个web api程序。

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

namespace WebApplication1.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class TestController : ControllerBase
    {
        [HttpGet]
        public string Get()
        {
            return ".net core-001";
        }
    }
}

分别发布在3个文件夹中

Nginx负载均衡,配置轮询_第1张图片

2. 使用命令分别进行启动

dotnet WebApplication1.dll --urls="http://*:8001" --ip="127.0.0.1" --port=8001
dotnet WebApplication1.dll --urls="http://*:8002" --ip="127.0.0.1" --port=8002
dotnet WebApplication1.dll --urls="http://*:8003" --ip="127.0.0.1" --port=8003

Nginx负载均衡,配置轮询_第2张图片

测试是否成功

Nginx负载均衡,配置轮询_第3张图片

3.下载nginx-1.20.2版本

4.打开配置文件,默认是80

Nginx负载均衡,配置轮询_第4张图片

5.修改成9090,启动Nginx

注意:端口不要被占用,否则启动不了

Nginx负载均衡,配置轮询_第5张图片

6.配置Nginx

Nginx负载均衡,配置轮询_第6张图片


#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 localhost:8001;
	server localhost:8002;
	server localhost:8003;
    }
    server {
        listen       9090;
        server_name  9090.max.com;#转发设置

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            #root   html;
            #index  index.html index.htm;
            proxy_pass  http://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;
    #    }
    #}

}

7.效果。可以看到3个api轮流被此IP地址调用

Nginx负载均衡,配置轮询_第7张图片

Nginx负载均衡,配置轮询_第8张图片

Nginx负载均衡,配置轮询_第9张图片

1.拓展:权重配置,权重策略

在IP后面加上weight。

Nginx负载均衡,配置轮询_第10张图片

session问题:

让同一个IP每次访问都到同一个服务器上面,ip_hash。

 Nginx负载均衡,配置轮询_第11张图片

2.拓展:缓存,性能优化

Nginx属于架构的性能优化,多线程属于代码的性能优化

二八原则,20%是增删改,80%是查询

key,value。key不变的话,就是同一个value

Nginx负载均衡,配置轮询_第12张图片 把上面代码写入下面

Nginx负载均衡,配置轮询_第13张图片Nginx负载均衡,配置轮询_第14张图片


#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 {
     #在当前盘符下的路径,设置缓存地址,设置反向代理缓存,数据缓存地址
     #data的路径在Nginx下面的data(里面会产生文件夹)
    proxy_cache_path    /a/c/data levels=1:2 keys_zone=web_cache:50m inactive=10m max_size=1g;
      
    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 {
                ip_hash;
	server localhost:8001 weight=10;
	server localhost:8002 weight=40; 
	server localhost:8003 weight=40;
    }
    server {
        listen       9090;
        server_name  9090.max.com;#转发设置

        #charset koi8-r;

        #access_log  logs/host.access.log  main;


        location / {
            #root   html;
            #index  index.html index.htm;
            proxy_pass  http://webApi;
        }
        #设置反向代理缓存的key
        #/api/v1/First/  只要是这个路径(包含)的话,支持缓存
        location /api/v1/First/ {
            proxy_store off;
            proxy_redirect off;
            proxy_set_header x-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header x-Rea1-IP $remote_addr ;
            proxy_set_header Host $http_host;
            proxy_pass http://webApi/api/v1/First/ ;#反向代理路径,这个就是网址请求输入的IP地址

            proxy_cache web_cache ;
            proxy_cache_valid 200 304 2m;
            proxy_cache_key $scheme$proxy_host$request_uri;#ur1地址做key
        }
        #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;
    #    }
    #}

}

之后在网页上输入,IP地址:http://webApi/api/v1/First/ ,在data文件夹中会看到缓存的文件

如果不想要缓存的数据,每次都拿最新的:

 1.加随机数,url产生不一样了,即key不一样了,proxy_cache_key。

 

 

 

你可能感兴趣的:(#,WebApi,nginx,运维)