Miscellaneous | Using Nginx for Reverse Proxy


01 Forward proxy and reverse proxy

Forward Proxy and Reverse Proxy are both proxy servers in computer networks, used for data transmission between clients and servers.

  • Forward proxy
    A forward proxy server proxies the client to send requests to the target server.
    When a client sends a request, the request is first sent to the forward proxy server, which then forwards it to the target server. Forward proxy servers can be used to hide the client's true identity, bypass network restrictions, or provide caching and other functions. In the case of forward proxy, the target server does not know that the real source of the request is the client, only that the request comes from the proxy server.
  • Reverse proxy
    The reverse proxy server is a proxy server that receives client requests.
    When a client sends a request, the request is first sent to the reverse proxy server, which then forwards it to the target server on the backend. Reverse proxy servers can be used for load balancing, security enhancement, caching of static content, SSL encryption and other functions. In the case of a reverse proxy, the client does not know which server the request is processed by, only that the request is sent to the proxy server. .

In summary, a forward proxy hides the identity of the client and sends requests on behalf of the client, while a reverse proxy hides the identity of the server and receives requests on behalf of the server.

02 Implement reverse proxy through Nginx

This article takes Nginx on Linux as an example to explain how to configure Nginx's reverse proxy settings.

2.1 Install Nginx

There are two ways to install nginx, one is to install it directly on the server, and the other is to install it using docker.
Direct installation: Linux Supplements | Install nginx on CentOS7 Linux and set it to start automatically at boot (steps).
This article uses docker for installation, which is simple and convenient. You need to install docker and docker-compose first.

2.2 Write nginx configuration file

# 进入一个自定义的文件夹 此处以root目录为例
cd ~

# 新建一个nginx.conf配置文件
vim nginx.conf

First, paste in all the default configuration of nginx.conf, and then add content under http > server.

...
http {
	...
    server {
        listen 80;										# 监听80端口
        server_name www.xxx.com;						# 监听的域名 通常是与本机绑定的域名 也可以写ip地址
        location / {
            proxy_pass http://www.baidu.com;			# 代理的网站 以百度为例
            proxy_set_header Host $host;				# 设置请求头(可选)
			proxy_set_header X-Real-IP $remote_addr;	# 设置真实请求的ip(可选)
        }
    }
}

After setting this up, access to this server (www.xxx.com) will be reverse-proxyed to the target website (www.baidu.com).
Although this server has only one IP, it can be bound to multiple domain names (such as subdomain names). During configuration, you only need to configure a few more servers. The ports can be the same, the domain names can be different, and the proxy websites can be different.

2.3 Write docker-compose (optional)

If it is started in docker mode, you can choose to start nginx through docker-compose.

# 文件名为docker-compose.yml
version: '3.3'
services:
    nginx:
        volumes:
            - '/root/nginx.conf:/etc/nginx/nginx.conf'
            - '/root/ssl:/etc/ssl/certificates'		# (可选)开启https的情况下才配置
            - '/root/html:/usr/share/nginx/html'	# (可选)有页面的情况下才配置
        network_mode: host							# 使用宿主机的网络 也可自定义网络映射
        image: 'nginx:stable-alpine-slim'			# 拉取的镜像 这里选择最轻量的版本

2.4 Launch and access

In docker-compose.ymlthe path where it is located, use the startup command to start.

# 启动nginx
docker-compose up -d

# 关停容器
docker-compose down

# 查看容器日志
docker logs <容器id或容器名>

Just visit http://www.xxx.com locally.
The configuration in this article can also be used for port forwarding. The principle is the same, but it is just set by the proxy server http://127.0.0.1:端口号.

Guess you like

Origin blog.csdn.net/xuzhongyi103/article/details/131297942