. net core + nginx to realize the whole steps of project load balancing


If you haven’t used nginx, you should have heard of it more or less. Deployment of Vue, reverse proxy and load balancing can help you.

Today, let’s talk about our project of nginx load balancing, as shown in the figure below. When the request arrives at nginx, nginx will forward it for us.

First use docker to install nginx

docker pull nginx:latest

Run the container and map the local port 8080 to the internal port 8080

docker run --name nginx -p 8080:80 -d nginx

Check the nginx container. If there is any error, please check the log

Visit in the browser

OK, so far our nginx has been installed.

We have more than three webapi projects ready for release.

Enter nginx container

Docker exec -it nginx bash

find nginx.conf File and modify it, nginx.conf Divided into HTTP block, events block and server block, the main changes in the server block

At this time, it is useless to use VI or VIM in nginx container. You need to execute the following two commands in turn

apt-get update 
apt-get install vim

Enter the file and point to another file at the end. Yes, this file is the configuration content of the server block

Enter etc / nginx / conf.d/ default.conf File and make changes

Upstream serviceinstance {# nginx polls the following service instances by default
  server ***.**.***.***:9007; 
  server ***.**.***.***:9008; 
  server ***.**.***.***:9009;
server { 
  listen    80; 
  server_name localhost; 
  #charset koi8-r; 
  #access_log /var/log/nginx/host.access.log main; 
  location / { 
    #root  /usr/share/nginx/html; 
    #index  index.html  index.htm ; # the request will be forwarded when it arrives
    proxy_pass http://ServiceInstance; 
  #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  /usr/share/nginx/html; 
  # proxy the PHP scripts to Apache listening on 
  #location ~ \.php$ { 
  #  proxy_pass; 
  # pass the PHP scripts to FastCGI server listening on 
  #location ~ \.php$ { 
  #  root      html; 
  #  fastcgi_pass; 
  #  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; 

After completion, restart the container. If there is any error, please check the log

docker restart nginx

An interface is called in the browser.

Each time, different service instances will be polled, and the expectation of load balancing is realized!

We can also set the weight ratio. The larger the weight value is, the more requests will arrive at this instance!

upstream ServiceInstance{ 
  #Nginx will poll the following service instances by default
  server ***.**.***.***:9007 weight=1; 
  server ***.**.***.***:9008 weight=2; 
  server ***.**.***.***:9009 weight=3;

You can also slowly study, nginx is very powerful!


This article about. Net core + nginx project load balancing is introduced here. For more related. Net core + nginx project load balancing content, please search previous articles of developer or continue to browse the following related articles. I hope you can support developer more in the future!