Understanding of implementing reverse proxy with nginx

Time:2021-9-17

Understanding of implementing reverse proxy with nginx

1 principle of user access to server

Understanding of implementing reverse proxy with nginx

1.1 user access to local database

There are two deployment locations for databases, local and remote. The route to be taken when accessing the local service database is:
Local hosts file —– > firewall —– > server
The hosts file is located in C: windowssystem32drivers etc. it will establish a “database” to associate some common web address domain names with their corresponding IP addresses. When the user enters a web address to log in in the browser, the system will first automatically find the corresponding IP address from the hosts file. Once it is found, the system will immediately open the corresponding web page. If it is not found, Then the system will submit the web address to the DNS domain name resolution server for IP address resolution. In short, it helps computers speed up domain name resolution.
However, the route to access the remote database is:
Understanding of implementing reverse proxy with nginx

Local hosts file —– > DNS —– > firewall —– > server
DNS (domain name system) is a service of the Internet. As a distributed database that maps domain names and IP addresses to each other, it can make it easier for people to access the Internet. In short, if you want to use a domain name, you have to register on the Internet first, which is the same principle as writing in the host file.

2 nginx reverse proxy server

Nginx is a lightweight web server, reverse proxy server and e-mail (IMAP / POP3) proxy server, which is distributed under BSD like protocol. It is characterized by less memory and strong concurrency. In fact, nginx has better concurrency in the same type of web server. It has the characteristics of small memory occupation (2m), large concurrency (3w-5w) and parsing block (written in C language).

2.1 reverse proxy

Understanding of implementing reverse proxy with nginx
Reverse proxy can be simply understood as a kind of protection for big brother. Once the real server is attacked in the network, it will bring a huge risk of data loss and theft. Therefore, in the Internet environment, a problem to be solved by software engineering is the protection of the real server—The principle of reverse proxy server is to hide the real server:
① The direction proxy server is between the user and the real server.
② The mapping of the real server IP address is recorded in the direction proxy server
③ When users access server data, they first access the direction proxy server to make users think that the direction proxy server is the real server.

2.2 forward agency

Understanding of implementing reverse proxy with nginx
The forward proxy is VPN(the function of virtual private network (VPN) is to establish private network on public network for encrypted communication. It is widely used in enterprise network. VPN gateway realizes remote access through encryption of data packet and conversion of data packet target address. VPN can be realized through server hardware, software and other methods.)The biggest difference between VPN and reverse proxy is that it protects the user’s identity, but for VPN, the user’s information is public, and the supervision of users can also be realized.

2.3 configuration description of nginx

#gzip  on;
    #Each service is a server
    server {
        #The listening port is repeatable
        listen       80;
        #The monitored domain name cannot be duplicate
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;
        #Start reverse proxy
        location / {
            #Keyword indicates the file directory of the reverse proxy
            root   html;
            #Represents the default access path
            index  index.html index.htm;
            #On behalf of the originator URL
            proxy_pass   http://www.$$$$$
        }

2.4 download and installation of nginx

Understanding of implementing reverse proxy with nginx

2.4.1 Windows:

matters needing attention:
① Try not to include Chinese and spaces in the installation directory
② When the nginx server starts, a daemon will be created.
③ When shutting down the nginx server, first shut down the daemon
Understanding of implementing reverse proxy with nginx
Test: the installation is completed by requesting localhost: 80 and displaying the welcome page!
Understanding of implementing reverse proxy with nginx

2.4.2 Linux:

① Linux can be installed online:
Command:[ [email protected] src]# wgethttp://nginx.org/download/ngi…
Understanding of implementing reverse proxy with nginx
② Decompress
Command:[ [email protected] src]# tar -xvf nginx-1.19.2.tar.gz
Understanding of implementing reverse proxy with nginx
③ Management documents
Command:[ [email protected] src]# mv nginx-1.19.2.tar.gz soft/
Command:[ [email protected] src]# mv nginx-1.19.2 nginx/
Understanding of implementing reverse proxy with nginx
④ Edit nginx
Command:[ [email protected] nginx]# ./configure
Understanding of implementing reverse proxy with nginx
⑤ Installing nginx
Command:[ [email protected] nginx]# make install
Understanding of implementing reverse proxy with nginx
⑥ Check nginx installation directory
Command:[ [email protected] nginx]# whereis nginx
Understanding of implementing reverse proxy with nginx
test
Enter the nginx installation directory / usr / local / nginx / SBIN
① Start nginx
Command:[ [email protected] sbin]# ./nginx
[ [email protected] Sbin]#. / nginx – s stop close
[ [email protected] Sbin]#. / nginx – s reload restart
Understanding of implementing reverse proxy with nginx