内网客户机通过代理访问互联网,通常要设置代理服务器地址和端口。
反向代理
模块:ngx_http_proxy_module
语法:
代理
Syntax: proxy_pass_ URL; 代理的后端服务器URL
Default: _
Context: location,if in location,limit_except
缓冲区
Syntax: proxy_buffering on | off;
Default: proxy_buffering on | off; # 缓冲开关
Context: http,server,location
proxy_buffering 开启的情况下,nginx会把后端返回的内容先放到缓冲区当中,然后再返回给客户端(边收边传,不是全部接收完在传给客户端读)
Syntax: proxy_buffersnumber size ;
Default: proxy_buffer_size 4k|8k; # 缓冲区数量
Context: http,server,location
Syntax: proxy_buffer_size 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_connect_timeout 60s;
Context: http,server,location
Syntax: proxy_send_timeout time ; # nginx进程向fastcgi进程发送request的整个过程的超时时间
Default: proxy_send_timeout 60s;
Context: http,server,location
IP | servername |
---|---|
192.168.200.184 | nginx1 |
192.168.200.186 | nginx2 |
两台服务器分别安装好nginx
1.nginx1 启动网站(内容)
yum install -y nginx
systemctl start nginx
[root@nginx1 html]# cat /usr/share/nginx/html/index.html
Hello World!
[root@nginx1 html]#
2.nginx2 启动代理程序
yum install -y nginx
systemctl start nginx
[root@nginx2 html]# vim /etc/nginx/conf.d/default.conflocation / {proxy_pass http://192.168.200.184:80;proxy_redirect default;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 60;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;# 启动代理程序可以注释掉下面的内容#expires 24h;#root /usr/share/nginx/html;#index index.html index.htm;# root /app;# random_index on;}
[root@nginx2 html]# systemctl restart nginx
3.结果:开启代理之后访问192.168.200.184 和192.168.200.186 的内容一样的
4.观察nginx1服务器的日志
[root@nginx1 html]# cat /var/log/nginx/access.log
192.168.200.186 - - [09/Feb/2023:00:18:24 +0800] "GET /test.jpg HTTP/1.0" 200 350627 "http://192.168.200.186/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36" "192.168.200.1"
访问成功,记录了客户机的IP和代理服务器的IP
缓存类型
网页缓存(公网)CDN
数据库缓存 memcache redis
网页缓存 nginx-proxy
客户端缓存 浏览器缓存
模块:ngx_http_proxy_module
语法:
缓存开关
Syntax: proxy_cache zone | off;
Default: proxy_cache off;
Context: http,server,location
代理缓存
Syntax: proxy_cache_path path [levels=levels] keys_zone=name:size[inactive=time][max_size=size];
Default: _
Context: http
example: proxy_cache_path /data/nginx/cache levels=1:2 keys_zone=one:10m;
缓存维度
Syntax: proxy_cache_key string; #定义缓存唯一key,通过唯一key来进行hash存取,缓存文件名
Default: proxy_cache_key schemeschemeschemeproxy_host$request_uri;
Context: http,server,location
缓存过期
Syntax: proxy_cache_valid [code…] time;
Default: _
Context: http,server,location
proxy_cache_valid 200 302 10m;
proxy_cache_valid 404 1m;
1.延续代理实验环境
2.设置nginx2为缓存服务器
添加如下配置
[root@nginx2 html]# vim /etc/nginx/nginx.conf
http {proxy_cache_path /data/nginx/cache levels=1:2 keys_zone=proxy_cache:10m max_size=10g inactive=60m use_temp_path=off;
}
[root@nginx2 html]# vim /etc/nginx/conf.d/default.conflocation / {proxy_pass http://192.168.200.184:80;proxy_redirect default;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 60;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;proxy_cache proxy_cache; # proxy_cache 使用名为对应的缓存配置proxy_cache_valid 200 304 12h; # 对httpcode为200...的缓存12小时proxy_cache_valid any 10m; #设置不同响应码的缓存时间,除了上面的,其他的存10分钟proxy_cache_key $host$uri$is_args$args; # proxy_cache_key $host$uri 定义缓存唯一key,通过唯一key来进行hash存取add_header Nginx-Cache "$upstream_cache_status"; # add_header:缓存命中情况如何在http头中体现,以及在nginx日志中查看 proxy_cache_path 缓存文件路径proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504; # 出现502~504或错误,会跳过此服务器访问下一台服务器# 启动代理程序可以注释掉下面的内容#expires 24h;#root /usr/share/nginx/html;#index index.html index.htm;# root /app;# random_index on;}[root@nginx1 ~]# touch test01.txt
[root@nginx1 ~]# touch test02.txt
[root@nginx1 ~]# md5sum test01.txt
d41d8cd98f00b204e9800998ecf8427e test01.txt
[root@nginx1 ~]# md5sum test02.txt
d41d8cd98f00b204e9800998ecf8427e test02.txt
[root@nginx1 ~]# [root@nginx2 data]# mkdir -p /data/nginx/cache
[root@nginx2 data]# cd /data/nginx/cache
[root@nginx2 cache]# ls
[root@nginx2 cache]# systemctl restart nginx
3.使用PC客户机,再次访问nginx2服务器
4.通过PC客户机浏览器开发者功能,观察是否命中缓存。
命中:hit
未命中:miss
提示:新创建的网页文件,初次访问均为miss
nginx缓存工作原理
负载均衡部分讲解
上一篇:【PR】零基础快速入门教程
下一篇:后勤管理系统—服务台管理功能