Nginx for reverse proxy and load balancing

environment:

Ubuntu:20.04

nginx:1.18

tomcat: 10.0.23.0

virtual machine:

tomcat   192.168.111.145:8080

nginx:     192.168.111.144:80

Reverse proxy:

Purpose:

Visit http://192.168.111.144 to jump to tomcat

Install tomcat in the 192.168.111.145 virtual machine, go to the tomcat official website to pull a tomcat package, then unzip it, enter the /bin directory, and execute the command ./startup.sh to start tomcat

That's ok

Install nginx in the 192.168.111.144 virtual machine, because mine is ubuntu20.04 system, I just download it directly

apt -y install nginx

Then enter the /etc/nginx/sites-available folder to configure the default file, the configuration is as follows.

root@apang:/etc/nginx/sites-available# cat default 
##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# https://www.nginx.com/resources/wiki/start/
# https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/
# https://wiki.debian.org/Nginx/DirectoryStructure
#
# In most cases, administrators will remove this file from sites-enabled/ and
# leave it as reference inside of sites-available where it will continue to be
# updated by the nginx packaging team.
#
# This file will automatically load configuration files provided by other
# applications, such as Drupal or Wordpress. These applications will be made
# available underneath a path with that package name, such as /drupal8.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##

# Default server configuration
#
server {
	listen 80 default_server;
	listen [::]:80 default_server;

	# SSL configuration
	#
	# listen 443 ssl default_server;
	# listen [::]:443 ssl default_server;
	#
	# Note: You should disable gzip for SSL traffic.
	# See: https://bugs.debian.org/773332
	#
	# Read up on ssl_ciphers to ensure a secure configuration.
	# See: https://bugs.debian.org/765782
	#
	# Self signed certs generated by the ssl-cert package
	# Don't use them in a production server!
	#
	# include snippets/snakeoil.conf;

	#root /var/www/html;

	# Add index.php to the list if you are using PHP
	#index index.html index.htm index.nginx-debian.html;

	#server_name _;

	location / {
		# First attempt to serve request as file, then
		# as directory, then fall back to displaying a 404.
		#try_files $uri $uri/ =404;
		proxy_pass http://192.168.111.145:8080;
	}
	#location /tomcat {
	#	proxy_pass http://192.168.111.145:8080;
	#	index index.html index.htm;
	#}

	# pass PHP scripts to FastCGI server
	#
	#location ~ \.php$ {
	#	include snippets/fastcgi-php.conf;
	#
	#	# With php-fpm (or other unix sockets):
	#	fastcgi_pass unix:/run/php/php7.4-fpm.sock;
	#	# With php-cgi (or other tcp sockets):
	#	fastcgi_pass 127.0.0.1:9000;
	#}

	# deny access to .htaccess files, if Apache's document root
	# concurs with nginx's one
	#
	#location ~ /\.ht {
	#	deny all;
	#}
}


# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
#server {
#	listen 80;
#	listen [::]:80;
#
#	server_name example.com;
#
#	root /var/www/example.com;
#	index index.html;
#
#	location / {
#		try_files $uri $uri/ =404;
#	}
#}

 Then we use our real machine to visit 192.168.111.144 and found that it has been forwarded

 By the way, forward proxy and reverse proxy.

Forward proxy is just like the VPN technology we often use. We can change our proxy point to access the resources we want to access.

Reverse proxy means that we access a server, and this server is just a proxy point that will forward to the resources we really want to access, just like the above operation.

To put it simply, forward proxy requires us to change the proxy point, but you are not aware of reverse proxy at all.

Load balancing:

I hope to back up all the default configuration files to be modified before operation to develop a good habit.

environment:

free04 192.168.111.144 nginx
ubuntu05 192.168.111.145 tomcat
rooky06 192.168.111.111 tomcat

Modify the default page of tomcat to identify it.

The default page is here

 Modify it to this:

 Configure the nginx configuration file on ubuntu04. Mine is an ubuntu system. Other systems may be different, but they should be similar.

Create a file with the .conf suffix under /etc/nginx/conf.d. What I created here is tomcat.conf 

 The configuration is as follows:

upstream tomcat {
        #负载均衡方法,可以自己选,默认为轮询方式
        #服务器的访问方式,
        server 192.168.111.145:8080 weight=1;
        server 192.168.111.111:8080 weight=1;
}

server {
        #负载均衡监视的端口
        listen 81 default_server;
        listen [::]:81 default_server;
        #负载均衡服务器的名称,没有的话填_
        server_name _;
        location / {
                #代理转发,注意这个tomcat要与上面的upstream后面的字符一样
                proxy_pass http://tomcat;
        }
}

The following value is the weight. I have two servers. If the performance is good, we can increase the weight relatively.

Restart nginx.

Visit http://192.168.111.144:81

Refresh it, it becomes ubuntu05, this is polling, and it takes turns.

 Done! ! !

Another solid day. hhhh

 

Guess you like

Origin blog.csdn.net/qq_48480384/article/details/127253764