nginx configuration proxy ip access https domain name configuration

Table of contents

problem background

solution

Forward proxy:

 reverse proxy:

 In a word, the difference between forward and reverse:


problem background

In some units or institutions, access to the Internet interface needs to be accessed through a designated server, then we need to access the external domain name through the proxy ip and port.

Example: How to access the interface of the domain name https://api.elecredit.com/ by specifying ip and port    ?

solution

Access through nginx proxy.

Let's first understand the forward proxy and reverse proxy of nginx:

  • Forward proxy :

If the Internet outside the LAN is imagined as a huge resource pool, the client in the LAN needs to access the Internet through a proxy server. This proxy service is called a forward proxy. The following is the schematic diagram of the forward proxy .

Due to the working environment, the daily work can only be limited to the LAN of the unit. If you want to access the Internet, what should you do? This requires the use of forward proxies.

insert image description here

  •  reverse proxy :

Look at the schematic diagram below and it will be clear at a glance. In fact, the client is unaware of the proxy, because the client can access without any configuration, we only need to send the request to the reverse proxy server, and the reverse proxy server selects the target server to obtain the data, and returns it to the client At this time, the reverse proxy server and the target server are the same server to the outside world, and the proxy server address is exposed, while the real server IP address is hidden.

 The difference between forward proxy and reverse proxy is, in one sentence: If our client uses it itself, it is a forward proxy. If it is used on the server and the user has no perception, it is a reverse proxy.

 Next, let's see how we can access the https service on the external network through the LAN

nginx configuration:

server {
        listen       8088;
        server_name  172.16.2.239;
	
	location / {
		proxy_send_timeout 600;
		proxy_read_timeout 600;
		proxy_connect_timeout 600;
		proxy_redirect off;
		#proxy_set_header Host $host;
		proxy_set_header Host api.elecredit.com;
		proxy_set_header X-Real-IP $remote_addr;
		proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
		proxy_pass https://api.elecredit.com:443/;
	        add_header Access-Control-Allow-Origin *;
	}
    }

Here, port 8088 is monitored, and server_name is the local IP address;

proxy_set_header Host api.elecredit.com;

The above line of configuration means to put the host field in the header of the original http request into the forwarded request.

In this way, we have configured it, and we can   request the Internet interface address through  http://172.16.2.239:8088/ .

 In a word, the difference between forward and reverse:

The proxy object of the forward proxy is the client, and the proxy object of the reverse proxy is the server. The proxy server standing on the side of the client is a forward proxy, and the proxy server standing on the side of the original server is a reverse proxy. Nginx can set proxy service through proxy_pass.


Life is too short to refuse introversion. I learned programming from Brother Fei , a young man who is dedicated to the sun, born towards the sun, and strives to grow upwards. Come on guys...

Committed to sharing experience in avoiding pitfalls in actual work scenarios, no routines, pure dry goods.

Brother Aifei, there is nothing wrong, Brother Fei will take you to the high speed, rush...


Guess you like

Origin blog.csdn.net/weixin_36754290/article/details/130339836