0
点赞
收藏
分享

微信扫一扫

代理-01

钟罗敏 2022-08-26 阅读 30


一、代理

1.什么是代理

代理相当与中介

2.没有代理

客户端和Nginx服务端,都是直接连接的

请求
客户端 - -------- Nginx服务端
响应
3.有代理

在互联网请求里面,为了安全客户端往往无法直接向服务端发起请求,就需要用到代理服务,来实现客户端和服务端通信,如下图所示

代理-01_linux

二、Nginx代理服务常见模式

Nginx作为代理服务,按照应用场景模式进行总结,分为
1.正向代理
2.反向代理

#正向代理

正向代理,(内部上网) 客户端 <—> 代理 -> 服务端


# 反向代理

反向代理,用于公司集群架构中,客户端 -> 代理 <—> 服务端

# 正反向代理的区别

1.区别在于形式上服务的”对象”不一样
2.正向代理代理的对象是客户端,为客户端服务
3.反向代理代理的对象是服务端,为服务端服务

代理-01_客户端_02


代理-01_linux_03

三、Nginx代理服务支持协议

1.代理支持的协议

代理-01_nginx_04

2.反向代理使用协议

代理-01_linux_05

3.模块总结

# 反向代理模式 Nginx****配置模块

http、websocket、https、tomcat(java程序) ngx_http_proxy_module
fastcgi(php程) ngx_http_fastcgi_module
uwsgi(python) ngx_http_uwsgi_module
grpc(go程序)golang ngx_http_v2_module

四、nginx代理实践

环境准备

主机 IP 主机角色

web01 10.0.0.7 web服务端

lb01 10.0.0.5 代理


# 2.代理语法 模块

http://nginx.org/en/docs/http/ngx_http_proxy_module.html

3.配置web01的nginx

[root@web01 ~]# vim /etc/nginx/conf.d/linux12.proxy.com.conf
server {
listen 80;
server_name linux12.proxy.com;

location / {
root /mm/proxy;
index index.html;
}
}

4.web01编写一个网站

[root@web01 ~]# mkdir /mm/proxy
[root@web01 ~]# echo "web01 web01 web01 wbe01 ...." > /mm/proxy/index.html
[root@web01 ~]# chown -R www.www /mm/

5.访问测试

1.配置hosts
10.0.0.7 linux12.proxy.com

2.重启
[root@web01 ~]# systemctl restart nginx

3.访问
linux12.proxy.com

6.配置代理lb01

1.安装nginx
2.配置nginx
3.创建用户并把nginx改一下 user www
4.配置nginx代理
[root@lb01 ~]# vim /etc/nginx/conf.d/linux12.proxy.com.conf
server {
listen 80;
server_name linux12.proxy.com;

location / {
proxy_pass http://172.16.1.7:80;
}
}

7.检查nginx并重启代理nginx
[root@lb01 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@lb01 ~]# systemctl restart nginx

8.访问页面测试
1.配置hosts
10.0.0.5 linux12.proxy.com

2.访问测试

结果:访问的页面不是我们要的内容,返回了web端第一个配置文件的内容

#原因:代理请求web服务端时,没有使用域名,使用了IP,匹配时没有匹配到server_name,所以直接返回默认的第一个配置文件

9.配置代理携带域名访问web端

[root@lb01 ~]# vim /etc/nginx/conf.d/linux.proxy.com.conf
server {
listen 80;
server_name linux.proxy.com;

location / {
proxy_pass http://172.16.1.7:80;
proxy_set_header Host $http_host;
}
}

10.重启后再次访问

[root@lb01 ~]# systemctl restart nginx

再次访问http://linux12.proxy.com/,得到想要的内容

五、Nginx代理常用优化参数

1.添加发往后端服务器的请求头信息

Syntax:    proxy_set_header field value;
Default: proxy_set_header Host $http_host;
proxy_set_header Connection close;
Context: http, server, location

# 用户请求的时候HOST的值是linux.proxy.com, 那么代理服务会像后端传递请求的还是linux.proxy.com
proxy_set_header Host $http_host;
# 将$remote_addr的值放进变量X-Real-IP中,$remote_addr的值为客户端的ip
proxy_set_header X-Real-IP $remote_addr;
# 客户端通过代理服务访问后端服务, 后端服务通过该变量会记录真实客户端地址
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

2.代理到后端的TCP连接、响应、返回等超时时间

#nginx代理与后端服务器连接超时时间(代理连接超时)
Syntax: proxy_connect_timeout time;
Default: proxy_connect_timeout 60s;
Context: http, server, location

#nginx代理等待后端服务器的响应时间
Syntax: proxy_read_timeout time;
Default: proxy_read_timeout 60s;
Context: http, server, location

#后端服务器数据回传给nginx代理超时时间
Syntax: proxy_send_timeout time;
Default: proxy_send_timeout 60s;
Context: http, server, location

3.proxy_buffer代理缓冲区

#nignx会把后端返回的内容先放到缓冲区当中,然后再返回给客户端,边收边传, 不是全部接收完再传给客户端
Syntax: proxy_buffering on | off;
Default: proxy_buffering on;
Context: http, server, location

#设置nginx代理保存用户头信息的缓冲区大小
Syntax: proxy_buffer_size size;
Default: proxy_buffer_size 4k|8k;
Context: http, server, location

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

4.配置代理优化文件

[root@lb01 ~]# vim /etc/nginx/proxy_params 
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 20s;
proxy_read_timeout 20s;
proxy_send_timeout 20s;
proxy_buffering on;
proxy_buffer_size 8k;
proxy_buffers 8 8k;

5.代理调用优化文件

[root@lb01 ~]# vim /etc/nginx/conf.d/linux.proxy.com.conf 
server {
listen 80;
server_name linux.proxy.com;

location / {
proxy_pass http://10.0.0.7:80;
include proxy_params;
}
}


举报

相关推荐

0 条评论