Nginx#Nginx as a reverse proxy server

Forward proxy and reverse proxy

The difference between forward proxy and reverse proxy is that the proxy objects are different
  .    The proxy object in the forward proxy is the client, and the proxy and client belong to the same LAN. The
  proxy object in the transparent reverse proxy is the server, proxy It belongs to the same LAN as the server, transparent to the client
  Insert picture description here

Reverse proxy syntax

反向代理配置方法:

代理
Syntax: 	proxy_pass URL;				   #代理的后端服务器URL
#URL我给谁做代理,我就把那一台机器的域名写过来
#有一台机器没有外网ip,我有外网ip我可以代理它,相当于一个跳板
Default: 	—
#默认_默认没有代理,就是上面的语法的默认值
Context: 	location, if in location, limit_except
#context哪些地方能作用于它,location可以,把语法写在location模块里面
#这里一条代码,就算是完成了反向代理配置,下面的语法就是反向代理的一些配置

缓冲区
#缓冲作用,我代理的服务器给每秒给我传递一点数据,我给客户端每秒传递一点数据,这样搞得我很累啊,于是我先把数据存下来,存到一定量的时候传递给客户端
Syntax:     proxy_buffering on | off;
Default:    proxy_buffering on;			   #缓冲开关
Context: 	http, server, location
#proxy_buffering开启的情况下,nignx会把后端返回的内容先放到缓冲区当中,然后再返回给客户端
(边收边传,不是全部接收完再传给客户端)#Nginx 全局配置中的 tcp_nopush 的作用就是 数据包会累计到一定大小之后才会发送 。而 tcp_nodelay 是尽快发送数据,所以若你启用了 buffer,建议关闭 tcp_nodelay。

Syntax:   	proxy_buffer_size size;
Default: 	proxy_buffer_size 4k|8k;	   #缓冲区大小
Context: 	http, server, location

Syntax: 	proxy_buffers number size;
Default: 	proxy_buffers 8 4k|8k;		   #缓冲区数量
Context: 	http, server, location

Syntax:    	proxy_busy_buffers_size size;
Default: 	proxy_busy_buffers_size 8k|16k;#忙碌的缓冲区大小控制同时传递给客户端的buffer数量
Context: 	http, server, location


头信息
Syntax: 	proxy_set_header field value;
Default: 	proxy_set_header Host $proxy_host;		#设置真实客户端地址
            proxy_set_header Connection close;
Context: 	http, server, location


超时
Syntax: 	proxy_connect_timeout time;
Default: 	proxy_connect_timeout 60s;				#链接超时
Context: 	http, server, location

Syntax: 	proxy_read_timeout time;
Default: 	proxy_read_timeout 60s;
Context: 	http, server, location


Syntax: 	proxy_send_timeout time; #nginx进程向fastcgi进程发送request的整个过程的超时时间,fastcgi进程把我不能处理的数据交给java或者php
Default: 	proxy_send_timeout 60s;
Context: 	http, server, location


#buffer 工作原理
1. 所有的proxy buffer参数是作用到每一个请求的。每一个请求会安按照参数的配置获得自己的buffer。proxy buffer不是global而是 request的。

2. proxy_buffering 是为了开启response buffering of the proxied server,开启后proxy_buffers和proxy_busy_buffers_size参数才会起作用。

3. 无论proxy_buffering是否开启,proxy_buffer_size(main buffer)都是工作的,proxy_buffer_size所设置的buffer_size的作用是用来存储upstream端response的header。

4. 在proxy_buffering 开启的情况下,Nginx将会尽可能的读取所有的upstream端传输的数据到buffer,直到proxy_buffers设置的所有buffer们 被写满或者数据被读取完(EOF)。此时nginx开始向客户端传输数据,会同时传输这一整串buffer们。同时如果response的内容很大的话,Nginx会接收并把他们写入到temp_file里去。大小由proxy_max_temp_file_size控制。如果busy的buffer 传输完了会从temp_file里面接着读数据,直到传输完毕。

5. 一旦proxy_buffers设置的buffer被写入,直到buffer里面的数据被完整的传输完(传输到客户端),这个buffer将会一直处 在busy状态,我们不能对这个buffer进行任何别的操作。所有处在busy状态的buffer size加起来不能超过proxy_busy_buffers_size,所以proxy_busy_buffers_size是用来控制同时传输到客户端的buffer数量的。

Enable nginx proxy proxy

Two real nginx servers

a, nginx-1 starts the website (content) (as a website server), it doesn’t matter if this is http, as long as it is a server that can be accessed

nginx-1的ip:10.0.105.199
已经编译安装好,检查nginx是否启动是否可以访问

b, nginx-2 start the agent

nginx-2的ip:10.0.105.202
配置nginx的yum源直接yum安装
启动
编辑nginx的配置文件:
[root@nginx-server ~]# vim /etc/nginx/conf.d/default.conf
server {
    
    
    server {
    
    
    listen       80;
    server_name  localhost;

    location / {
    
    
    proxy_pass http://10.0.105.199:80;
    proxy_redirect default;
    proxy_set_header Host $http_host;
    proxy_set_header X-Real-IP $remote_addr;
    #proxy_set_header REMOTE-HOST $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

    proxy_connect_timeout 30;
    proxy_send_timeout 60;
    proxy_read_timeout 60;

    proxy_buffering on;
    proxy_buffer_size 32k;
    proxy_buffers 4 128k;
    proxy_busy_buffers_size 256k;
    proxy_max_temp_file_size 256k;
    }
}
重新加载nginx配置文件
[root@nginx-server ~]# nginx -s reload

Detailed configuration of nginx proxy

proxy_pass :真实服务器的地址,可以是ip也可以是域名和url地址
proxy_redirect :如果真实服务器使用的是的真实IP:非默认端口。则改成IP:默认端口。
proxy_set_header:重新定义或者添加发往后端服务器的请求头
proxy_set_header X-Real-IP :启用客户端真实地址(否则日志中显示的是代理在访问网站)
proxy_set_header X-Forwarded-For:记录代理地址

proxy_connect_timeout::后端服务器连接的超时时间发起三次握手等候响应超时时间
proxy_send_timeout:后端服务器数据回传时间就是在规定时间之内后端服务器必须传完所有的数据
proxy_read_timeout :nginx接收upstream(上游/真实) server数据超时, 默认60s, 如果连续的60s内没有收到1个字节, 连接关闭。像长连接



proxy_buffering on;开启缓存
proxy_buffer_size:proxy_buffer_size只是响应头的缓冲区
proxy_buffers 4 128k; 内容缓冲区域大小
proxy_busy_buffers_size 256k; 从proxy_buffers划出一部分缓冲区来专门向客户端传送数据的地方
proxy_max_temp_file_size 256k;超大的响应头存储成文件

Configure log format

proxy_set_header X-Real-IP 
未配置
Nginxbackend 的日志:记录只有192.168.107.112
配置
Nginxbackend 的日志,记录的有192.168.107.16 192.168.107.107 192.168.107.112

proxy_buffers 的缓冲区大小一般会设置的比较大,以应付大网页。 proxy_buffers当中单个缓冲区的大小是由系统的内存页面大小决定的,Linux系统中一般为4k。 proxy_buffers由缓冲区数量和缓冲区大小组成的。总的大小为number*size。
若某些请求的响应过大,则超过_buffers的部分将被缓冲到硬盘(缓冲目录由_temp_path指令指定), 当然这将会使读取响应的速度减慢, 影响用户体验. 可以使用proxy_max_temp_file_size指令关闭磁盘缓冲.

Note : proxy_pass http:// fill in the address of the nginx-1 server.

d. Use the PC client to access the address of the nginx-2 server
. Enter http://10.0.105.202 in the browser (it can also be the domain name of the nginx-2 server)

Successfully access the nginx-1 server page
e, observe the log of the nginx-1 server (remember to open the log path below, the default is /var/log)

10.0.105.202 - - [27/Jun/2019:15:54:17 +0800] "GET / HTTP/1.0" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36" "10.0.105.207"

10.0.105.202 Proxy server address

10.0.105.207 Client address.

The visit was successful. Record the IP of the client and the IP of the proxy server

Guess you like

Origin blog.csdn.net/kakaops_qing/article/details/109016262