Understand and implement Nginx reverse proxy in minutes!


Preface: The knowledge points involved in this article are: CentOS7 basic commands, docker common operations, container data volumes, port binding and domain name binding, reverse proxy, if you use a different operating system and different methods than this article Relationships, you can look at the article catalog to understand what steps a reverse proxy generally requires.

1. Concept

Before implementing a reverse proxy , we must first know what a forward proxy is , and understand its role through the differences between them

1.1 Forward proxy

as the picture shows:

Insert picture description here

It is not difficult to see that in the above example, the PC in the LAN sends the request to the proxy server, and then the proxy server forwards the request to the Internet

1.2, reverse proxy

Insert picture description here

It is not difficult to see that in the above example, the request from the Internet will first go through the proxy server, and then determine which website to return based on the proxy server

1.3. Comparison of the two

Types of the difference
Forward proxy Proxy client , unified forwarding requests
Reverse proxy Proxy server , uniformly distribute requests

2. Nginx proxy server

2.1, install Nginx mirror

1. Search nginx mirror

docker search nginx

2. Pull the nginx image (again, you need to configure the source to improve the speed of docker downloading the image, please Baidu for details)

docker pull nginx

2.2, prepare Nginx related configuration

1. Create an nginx directory in the /root directory to store nginx data information

mkdir ~/nginx

2. Enter the catalog

cd ~/nginx

3. Create a conf.d directory under the nginx directory (used to store configuration files related to reverse proxy)

mkdir conf.d

4. Create the configuration file nginx.conf under the conf directory (choose one of the two methods)

vi nginx.conf

or

vim nginx.conf

5. Add the following configuration content

user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
    
    
	worker_connections 1024;
}
http {
    
    
	include /etc/nginx/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 /var/log/nginx/access.log main;
	
	sendfile on;
	#tcp_nopush on;
	
	keepalive_timeout 65;
	#gzip on;
	include /etc/nginx/conf.d/*.conf;
}

7. Create a conf directory under the nginx directory (store nginx configuration information)

mkdir conf

8. Create proxy.conf configuration file in the conf.d directory (reverse proxy related configuration)

upstream tomcat-test {
    
    
	server 192.168.181.5:8080;
}

server {
    
    
	listen	80; #port
	server_name	www.proxy.com; #host
	location / {
    
    
		proxy_pass http://tomcat-test;
		index index.html;
	}
}

Configuration file explanation (ip needs to be changed according to the ip of your own virtual machine):

Insert picture description here

2.3, create a container

1. Create a container, and set the port, directory mapping (under the nginx directory)

docker run -id --name=c_nginx \
-p 80:80 \
-p 81:81 \
-p 82:82 \
-v $PWD/conf/nginx.conf:/etc/nginx/nginx.conf \
-v $PWD/conf.d:/etc/nginx/conf.d \
-v $PWD/logs:/var/log/nginx \
-v $PWD/html:/usr/share/nginx/html \
nginx

Explain the above configuration file a little bit:

-pThe function of the command is to bind the port of the virtual machine to the port of the container

-vThe function of the command is to make the virtual machine and the container share the same directory, $PWDindicating the current directory. The shared directory is in the virtual machine, not in the container (you can learn about data volumes)

3. Tomcat test server

3.1, install mirror

1. Search tomcat mirror

docker search tomcat

2. Pull tomcat image

docker pull tomcat

3.2, create tomcat container and directory

1. Create a tomcat directory in the /root directory (used to store html files)

mkdir ~/tomcat
cd ~/tomcat

2. Create and edit index.html file (html page for display later)

vi index.html

Enter the test content

this is index.html port=8080!!!! 

3. Create container, set port, directory mapping

docker run -id --name=c_tomcat \
-p 8080:8080 \
-v $PWD:/usr/local/tomcat/webapps \
tomcat

4. The host performs the relevant configuration of domain name mapping

As shown below:

Insert picture description here

Now we want is through www.proxy.comaccess nginx proxy server domain name, ip address of the proxy server is 192.168.181.5, in order to make the browser can identify this domain, we need to set the computer's configuration file

This article takes weindows operating system as an example, other operating systems need to be Baidu

domain name mapping file windows system is C:\Windows\System32\drivers\etcunder the directory hostsfile

Insert picture description here

Add the following information to the file:

192.168.181.5 www.proxy.com

The hosts file is equivalent to a DNS server, which www.proxy.comresolves what you send into 192.168.181.5this ip address

5. Test results

Restart tomcat and nginx

docker restart c_nginx
docker restart c_tomcat

After visiting www.proxy.com, you can see the following effects. It
Insert picture description here
is not easy to create. If you have gained, please like and encourage. If you have any questions, please point out, make progress together and learn together~ ^_^

Guess you like

Origin blog.csdn.net/weixin_44829930/article/details/109005883