How to implement Nginx reverse proxy and load balancing (based on Linux)

How to implement Nginx reverse proxy and load balancing (based on Linux)

Let's try out nginx's reverse proxy here.

The reverse proxy method refers to using a proxy server to accept connection requests on the Internet, then forwarding the requests to a server on the internal network, and returning the results obtained from the server to the client requesting the connection on the Internet. At this time, the proxy server appears to the outside world as a reverse proxy server.

In our Java project, the access is made through port 80, Nginx receives it, then forwards it to the tomcat server, and then returns the result of the server.

You need to modify the nginx.conf file here.

upstream backend {
  #The larger the proxy IP weight is, the more traffic it receives. On the contrary, server localhost:8084 weight=50;
  server localhost:8088 weight=50;
}

Forward the received request:

# / all load balancing + reverse proxy location / {
      root /data/wwwroot1;
      index index.html index.htm;#index file proxy_pass http://backend;
    }

In this way, the request made to nginx can be assigned and forwarded to tomcat. Here I have defined two tomcat servers, which are used for load balancing. By setting weight, you can control the amount of access.

The specific configuration code is as follows;

#user nobody;
# worker process usually sets the number of CPUs * the number of cores worker_processes 1;
 
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
 
#pid logs/nginx.pid;
 
# Set connection properties events {
  worker_connections 1024;#How many connections does one worker generate}
 
# Configure the main section of the HTTP server 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 compression function set gzip on;
  gzip_min_length 1k;
  gzip_buffers 4 16k;
  gzip_http_version 1.0;
  gzip_comp_level 6;
  gzip_types text/plain text/css text/javascript application/json application/javascript application/x-javascript application/xml;
  gzip_vary on;
   
  #Set the load balancing backend server list upstream backend {
    #The larger the proxy IP weight is, the more traffic it receives. On the contrary, server localhost:8084 weight=50;
    server localhost:8088 weight=50;
  }
   
   
   
  server {
    listen 2022;
    server_name localhost;
    charset utf-8;
    access_log logs/wwwroot2.access.log main;
    location / {
      root /data/wwwroot2;
      index index.html index.htm;#index file}
  }
  # Virtual host section server {
    listen 80;
    server_name localhost;
    root /data/wwwroot1;
    charset utf-8;
    #Access log access_log logs/wwwroot1.access.log main;
    # / all load balancing + reverse proxy location / {
      root /data/wwwroot1;
      index index.html index.htm;#index file proxy_pass http://backend;
    }
 
    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;
  # }
  #}
 
}

The test results show that by accessing the address of port 80, the results displayed are basically 50-50.

Also randomly access

The above two screenshots correspond to the test files under my two tomcat servers.

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Detailed explanation of nginx server installation and load balancing configuration on Linux system
  • How to configure multiple tomcats with Nginx load balancing under Linux
  • How to build nginx load balancing under Linux
  • Detailed explanation of Linux system configuration nginx load balancing
  • Detailed explanation of the use cases of Nginx load balancing configuration on Linux.

<<:  Some summary of MySQL's fuzzy query like

>>:  Web development js string concatenation placeholder and conlose object API detailed explanation

Recommend

Solution to mysql prompt "got timeout reading communication packets"

Error message: user: 'root' host: `localh...

Detailed explanation of JS ES6 variable destructuring assignment

Table of contents 1. What is deconstruction? 2. A...

How to detect Ubuntu version using command line

Method 1: Use the lsb_release utility The lsb_rel...

HTML table markup tutorial (5): light border color attribute BORDERCOLORLIGHT

In a table, you can define the color of the upper...

The rel attribute of the HTML link tag

The <link> tag defines the relationship bet...

JavaScript quickly implements calendar effects

This article example shares the specific code of ...

Detailed tutorial on installing harbor private warehouse using docker compose

Overview What is harbor? The English word means: ...

Detailed process of deploying Docker to WSL2 in IDEA

The local environment is Windows 10 + WSL2 (Ubunt...

CSS controls the spacing between words through the letter-spacing property

letter-spacing property : Increase or decrease th...

Native js implementation of magnifying glass component

This article example shares the specific code for...

MySQL scheduled backup solution (using Linux crontab)

Preface Although some love in this world has a pr...

Detailed tutorial on docker-compose deployment and configuration of Jenkins

Docker-compose deployment configuration jenkins 1...

Method of building redis cluster based on docker

Download the redis image docker pull yyyyttttwwww...

How to create a Docker repository using Nexus

The warehouse created using the official Docker R...