What is reverse proxy
The reverse proxy server is set up on the server side, which relieves the workload of the server by buffering frequently requested pages, forwards client requests to the target server on the internal network, and returns the results obtained from the server to the client requesting connection on the Internet, when the proxy server and the target host are connected. Together, it is represented as a server.
Many large Web sites now use reverse proxies. In addition to preventing malicious attacks on Intranet servers by external networks and reducing server pressure and access security control by caching, load balancing can also be carried out to distribute user requests to multiple servers.
Using Nginx reverse proxy
demand
- Two Tomcat services are proxied by Nginx reverse
- Nginx server: 192.168.230.130 reverse proxy Tomcat server: 192.168.230.130:90
- Nginx server: 192.168.230.130:8080 reverse proxy Tomcat server: 192.168.230.130:9090
Tomcat container cluster
Start two Tomcat containers, mapping ports 90 and 9090, respectively
Create docker-compose.yml
version: '3' services: tomcat1: image: tomcat container_name: tomcat90 ports: - 90:8080 tomcat2: image: tomcat container_name: tomcat9090 ports: - 9091:8080
Start container
docker-compose up -d
Configuring Nginx reverse proxy
- Modify the nginx.conf configuration file in the / usr/local/docker/nginx/conf directory
user nginx; # Specify the number of CPU resources to be used worker_processes 1; events { # Connection Number worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; # Virtual Host Configuration server { # Designated Port listen 80; # Specify IP (can be domain name) server_name 192.168.230.130; location / { # Configuration Agent proxy_pass http://192.168.230.130:90 # home page index index.html index.htm index.jsp; } } # Virtual Host II Configuration server { listen 8080; server_name 192.168.230.130; location / { proxy_pass http://192.168.230.130:9090 index index.html index.htm index.jsp; } } }
- Start the Nginx container
docker-compose up -d
- Open browser to access separately 192.168.230.130 and 192.168.230.130:8080
Nginx load balancing
Configure Nginx load balancing
- Modify the nginx.conf configuration file in the / usr/local/docker/nginx/conf directory
Relevant configuration instructionsuser nginx; # Specify the number of CPU resources to be used worker_processes 1; events { # Connection Number worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; # Define IP and device status for load balancing devices (Tomcat cluster) upstream tomcats { server 192.168.230.130:90 weight=10; server 192.168.230.130:9090 weight=10; } # Virtual Host Configuration server { # Designated Port listen 80; # Specify IP (can be domain name) server_name 192.168.230.130; location / { # Configuration Agent proxy_pass http://tomcats # home page index index.html index.htm index.jsp; } } # Virtual Host II Configuration server { listen 8080; server_name 192.168.230.130; location / { proxy_pass http://tomcats index index.html index.htm index.jsp; } } }
- upstream: the state of each device:
- down: Represents that the current server is temporarily not participating in the load
- Weight: The default is 1. The larger the weight, the heavier the load.
- max_fails: The number of requests allowed to fail defaults to 1, returning an error defined by the proxy_next_upstream module when the maximum number of requests exceeds
- Fail_timeout: The pause time after the failure of access to max_fails.
- Backup: When all other non-backup machines are down or busy, request the backup machine. So this machine will have the lightest pressure.
- Start container
docker-compose up -d
- Open browser to access separately 192.168.230.130 and 192.168.230.130:8080