一、ngx_http_proxy_module模块:(可通过不同的location实现动静分离)
转发请求至另一台主机
proxy_pass URL;
注意:proxy_pass后面路径不带uri时,会将location的uri传递(附加)给后端主机
server {
server_name HOSTNAME;
location /uri/ {
proxy_pass http://host[:port]; 注意:最后没有/
}
}
上面示例:http://HOSTNAME/uri --> http://host/uri ,功能类似 root
如果上面示例中有 /,即:http://host[:port]/ 此方式较少使用
意味着:http://HOSTNAME/uri --> http://host/ 即置换,功能类似 alias
案例1:实现简单的反向代理
vim test.conf
server {
listen 80 ;
server_name www.magedu.net;
root /data/ssl;
location / {
proxy_pass http://192.168.43.103 ;
}
}
server {
listen 9001;
server_name www.apix.com ;
location / {
proxy_pass http://192.168.43.103 ;
}
}
[root@centos7 ~]# systemctl restart httpd
[root@centos7 ~]# echo 192.168.43.103 > /var/www/html/index.html
[root@centos7 ~]# curl 192.168.43.103
192.168.43.103
[root@centos6 ~]# curl www.apix.com:9001
192.168.43.103
二、proxy_set_header field value;
设定转发往后端主机的请求报文的请求首部的值;
Context: http, server, location
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
请求报文的标准格式如下:
X-Forwarded-For: client1, proxy1, proxy2
案例2:RS日志中显示客户端IP
nginx服务器
vim /etc/nginx/conf.d/test.conf
server {
listen 80 ;
server_name www.magedu.net;
root /data/ssl;
location / {
proxy_pass http://192.168.43.103 ;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
vim /etc/httpd/conf/httpd.conf
# LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "\"%{X-Forwarded-For}i\" %h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
tail -f /var/log/httpd/access_log
"192.168.43.100" 192.168.43.101 - - [25/Jul/2020:10:43:26 +0800] "GET / HTTP/1.0" 200 15 "-" "curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.27.1 zlib/1.2.3 libidn/1.18 libssh2/1.4.2"
三、启用用于反向代理的缓存功能(proxy_cache_path)
1、主配置文件中配置
proxy_cache_path path [levels=levels] [use_temp_path=on|off] keys_zone=name:size [inactive=time] [max_size=size] [manager_files=number] [manager_sleep=time] [manager_threshold=time] [loader_files=number] [loader_sleep=time] [loader_threshold=time] [purger=on|off] [purger_files=number] [purger_sleep=time] [purger_threshold=time];
2、在http配置定义缓存信息
proxy_cache_path /var/cache/nginx/proxy_cache #定义缓存保存路径,proxy_cache会自动创建
levels=1:2:2 #定义缓存目录结构层次,1:2:2 可以生成24x28x2^8=1048576个目录 ###keys_zone=proxycache:20m #指内存中缓存的大小,主要用于存放key和metadata(如:使用次数)
inactive=120s; #缓存有效时间
max_size=1g; #最大磁盘占用空间,磁盘存入文件内容的缓存空间最大值
3、server中调用缓存配置
(1)proxy_cache zone | off; 默认off
指明调用的缓存,或关闭缓存机制;Context:http, server, location
(2)proxy_cache_key string;
缓存中用于“键”的内容
默认值:proxy_cache_key proxy_host$request_uri;
(3)proxy_cache_valid [code ...] time;
定义对特定响应码的响应内容的缓存时长
定义在http{...}中
示例:
proxy_cache_valid 200 302 10m;
proxy_cache_valid 404 1m;
案例3:启用反向代理的缓存功能
vim /etc/nginx/nginx.conf
http {
proxy_cache_path /var/cache/nginx/proxy_cache levels=1:2:2 keys_zone=proxycache:20m inactive=120s max_size=1g;(proxycache:20m 指内存中缓存的大小,主要用于存放key和metadata,max_size=1g 指磁盘存入文件内容的缓存空间最大值)
}
mkdir /var/cache/nginx
vim /etc/nginx/conf.d/test.conf
server {
listen 80 ;
server_name www.magedu.net;
root /data/ssl;
proxy_cache proxycache;
proxy_cache_key $request_uri;
proxy_cache_valid 200 302 301 1h;
proxy_cache_valid any 1m;
location / {
proxy_pass http://192.168.43.103 ;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
结果如下
[root@centos7 conf.d]# tree /var/cache/nginx/
/var/cache/nginx/
└── proxy_cache
└── 9
└── 7d
└── cc
└── 6666cd76f96956469e7be39d750cc7d9