010.Nginx薄厚代理
一 Nginx代理
1.1 Nginx代理概述
1.2 Nginx代理模式
- 正向代理(forward proxy)
- 反向代理(reverse proxy)
- 透明代理
1.3 正向代理
1.4 反向代理
1.5 透明代理
1.6 常见代理软件
二 代理配置项
2.1 配置语法
- proxy_buffer_size:设置缓冲区大小(内存页大小)
- proxy_buffers:设置缓冲区数量和大小(内存页数量和大小)
- proxy_busy_buffers_size:设置最大缓冲区大小
- proxy_hide_header:设置隐藏头信息字段;
- proxy_set_body:设置请求体返回信息。
- proxy_hide_header:设置隐藏头信息字段;
- proxy_set_body:设置请求体返回信息。
三 配置正向代理
3.1 正向代理配置
1 [root@proxy ~]# vi /etc/nginx/conf.d/reverse.conf2 server{
3 resolver 8.8.8.8; #配置DNS解析IP地址
4 resolver_timeout 30s; #超时时间(5秒)
5 listen 8080;
6 access_log /var/log/nginx/reverse.access.log main;
7 error_log /var/log/nginx/reverse.error.log warn;
8 location / {
9 proxy_pass http://$http_host$request_uri; #配置正向代理参数
10 proxy_set_header Host $http_host; #解决如果URL中带"."后Nginx 503错误
11 proxy_buffers 256 4k; #配置缓存大小
12 proxy_max_temp_file_size 0; #关闭磁盘缓存读写减少I/O
13 proxy_connect_timeout 30; #代理连接超时时间
14 proxy_cache_valid 200 302 10m;
15 proxy_cache_valid 301 1h;
16 proxy_cache_valid any 1m; #配置代理服务器缓存时间
17 }
18 }
1 [root@proxy ~]# nginx -t -c /etc/nginx/nginx.conf #检查配置文件2 [root@proxy ~]# nginx -s reload #重载配置文件
四 反向代理配置
4.1 环境预设
主机 | 作用 | 备注 |
proxy2.odocker.com | 代理服务器 | 反向代理服务器 |
www.landiannews.com | 原始服务器 | 模拟原始服务器 |
4.2 配置反向代理
1 [root@proxy ~]# vi /etc/nginx/conf.d/forward.conf2 server {
3 listen 80;
4 server_name forward.linuxds.com;
5 access_log /var/log/nginx/forward.access.log main;
6 error_log /var/log/nginx/forward.error.log warn;
7 location / {
8 proxy_pass https://www.landiannews.com/;
9indexindex.html;
10 proxy_redirect off;
11# proxy_set_header Host $host;
12 proxy_set_header X-Real-IP $remote_addr;
13 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
14 client_max_body_size 10m; #允许客户端请求的最大单文件字节数
15 client_body_buffer_size 128k; #缓冲区代理缓冲用户端请求的最大字节数
16 proxy_connect_timeout 300; #nginx跟后端服务器连接超时时间(代理连接超时)
17 proxy_send_timeout 300; #后端服务器数据回传时间(代理发送超时)
18 proxy_read_timeout 300; #连接成功后,后端服务器响应时间(代理接收超时)
19 proxy_buffer_size 4k; #设置代理服务器(nginx)保存用户头信息的缓冲区大小
20 proxy_buffers 4 32k; #proxy_buffers缓冲区,网页平均在32k以下的话,这样设置
21 proxy_busy_buffers_size 64k; #高负荷下缓冲大小(proxy_buffers*2)
22 proxy_temp_file_write_size 64k; #设定缓存文件夹大小,大于这个值,将从upstream服务器传
23 }
24 }
1 [root@proxy ~]# nginx -t -c /etc/nginx/nginx.conf #检查配置文件2 [root@proxy ~]# nginx -s reload #重载配置文件
4.3 测试反向代理
4.4 其他代理配置语句
五 四层代理配置
5.1 四层代理
5.2 环境预设
IP:端口 | 后端RS | 备注 |
172.24.10.21:8888(nginx01) | 172.24.10.22:81(nginx02) 172.24.10.23:81(nginx03) | 反向代理81端口Web |
172.24.10.21:2222(nginx01) | 172.24.10.24:22(nginx04) | 反向代理ssh |
1 [root@nginx02 ~]# mkdir /usr/share/nginx/rs/2 [root@nginx02 ~]# echo '<h1>Rs172.24.10.22-81</h1>' > /usr/share/nginx/rs/index.html
3 [root@nginx02 ~]# cat > /etc/nginx/conf.d/rs.conf <<EOF
4 server {
5 listen 81;
6 server_name 172.24.10.22;
7 location / {
8 root /usr/share/nginx/rs;
9indexindex.html;
10 access_log /var/log/nginx/rs.access.log main;
11 error_log /var/log/nginx/rs.error.log warn;
12 }
13 }
14 EOF
1 [root@nginx02 ~]# nginx -t -c /etc/nginx/nginx.conf #检查配置文件2 [root@nginx02 ~]# nginx -s reload #重载配置文件
1 [root@nginx03 ~]# mkdir /usr/share/nginx/rs/2 [root@nginx03 ~]# echo '<h1>Rs172.24.10.23-81</h1>' > /usr/share/nginx/rs/index.html
3 [root@nginx03 ~]# cat > /etc/nginx/conf.d/rs.conf <<EOF
4 server {
5 listen 81;
6 server_name 172.24.10.23;
7 location / {
8 root /usr/share/nginx/rs;
9indexindex.html;
10 access_log /var/log/nginx/rs.access.log main;
11 error_log /var/log/nginx/rs.error.log warn;
12 }
13 }
14 EOF
1 [root@nginx02 ~]# nginx -t -c /etc/nginx/nginx.conf #检查配置文件2 [root@nginx02 ~]# nginx -s reload #重载配置文件
5.3 配置四层代理
1 [root@nginx01 ~]# cat >> /etc/nginx/nginx.conf << EOF2 stream {
3 upstream myweb {
4 server 172.24.10.22:81 weight=5 max_fails=1 fail_timeout=10s;
5 server 172.24.10.23:81;
6 }
7 upstream myssh {
8 hash $remote_addr consistent;
9 server 172.24.10.24:22;
10 }
11
12 server {
13 listen 8888;
14 proxy_connect_timeout 2s;
15 proxy_timeout 900s;
16 proxy_pass myweb;
17 }
18
19 server {
20 listen 2222;
21 proxy_connect_timeout 2s;
22 proxy_timeout 900s;
23 proxy_pass myssh;
24 }
25 }
26 EOF
1 [root@proxy ~]# nginx -t -c /etc/nginx/nginx.conf #检查配置文件2 [root@proxy ~]# nginx -s reload #重载配置文件
5.4 确认验证
六 按类型反向代理配置
6.1 环境预设
主机 | IP | 备注 |
nginx01 | 172.24.10.21 | 反向代理服务器 |
nginx02 | 172.24.10.22 | chrmoe类型资源 firefox类型资源 |
nginx03 | 172.24.10.23 | iPhone类型资源 android类型资源 |
nginx04 | 172.24.10.24 | IE类型资源 |
1 [root@nginx02 ~]# mkdir /usr/share/nginx/basebrowser/2 [root@nginx02 ~]# echo '<h1>Chrmoe-172.24.10.22</h1>' > /usr/share/nginx/basebrowser/index01.html
3 [root@nginx02 ~]# echo '<h1>Firefox-172.24.10.22</h1>' > /usr/share/nginx/basebrowser/index02.html
1 [root@nginx02 ~]# cat > /etc/nginx/conf.d/basebrowser.conf <<EOF2 server {
3 listen 88;
4 server_name 172.24.10.22;
5 location / {
6 root /usr/share/nginx/basebrowser;
7indexindex01.html;
8 access_log /var/log/nginx/chrmoebrowser.access.log main;
9 error_log /var/log/nginx/chrmoebrowser.error.log warn;
10 }
11 }
12 server {
13 listen 89;
14 server_name 172.24.10.22;
15 location / {
16 root /usr/share/nginx/basebrowser;
17indexindex02.html;
18 access_log /var/log/nginx/firefoxbrowser.access.log main;
19 error_log /var/log/nginx/firefoxbrowser.error.log warn;
20 }
21 }
22 EOF
1 [root@nginx03 ~]# mkdir /usr/share/nginx/cellphone/2 [root@nginx03 ~]# echo '<h1>iPhone-172.24.10.23</h1>' > /usr/share/nginx/cellphone/index01.html
3 [root@nginx03 ~]# echo '<h1>Android-172.24.10.23</h1>' > /usr/share/nginx/cellphone/index02.html
1 [root@nginx03 ~]# cat > /etc/nginx/conf.d/cellphone.conf <<EOF2 server {
3 listen 88;
4 server_name 172.24.10.23;
5 location / {
6 root /usr/share/nginx/cellphone;
7indexindex01.html;
8 access_log /var/log/nginx/iphone.access.log main;
9 error_log /var/log/nginx/iphone.error.log warn;
10 }
11 }
12 server {
13 listen 89;
14 server_name 172.24.10.23;
15 location / {
16 root /usr/share/nginx/cellphone;
17indexindex02.html;
18 access_log /var/log/nginx/android.access.log main;
19 error_log /var/log/nginx/android.error.log warn;
20 }
21 }
22 EOF
1 [root@nginx04 ~]# mkdir /usr/share/nginx/iebrowser/2 [root@nginx04 ~]# echo '<h1>IE Browser-172.24.10.24</h1>' > /usr/share/nginx/iebrowser/index.html
1 [root@nginx04 ~]# cat > /etc/nginx/conf.d/iebrowser.conf <<EOF2 server {
3 listen 88;
4 server_name 172.24.10.24;
5 location / {
6 root /usr/share/nginx/iebrowser;
7indexindex.html;
8 access_log /var/log/nginx/iebrowser.access.log main;
9 error_log /var/log/nginx/iebrowser.error.log warn;
10 }
11 }
12 EOF
1 [root@nginx02 ~]# nginx -t -c /etc/nginx/nginx.conf #检查配置文件2 [root@nginx02 ~]# nginx -s reload #重载配置文件
3 [root@nginx03 ~]# nginx -t -c /etc/nginx/nginx.conf #检查配置文件
4 [root@nginx03 ~]# nginx -s reload #重载配置文件
5 [root@nginx04 ~]# nginx -t -c /etc/nginx/nginx.conf #检查配置文件
6 [root@nginx04 ~]# nginx -s reload #重载配置文件
1 [root@client ~]# curl 172.24.10.22:892 <h1>Firefox-172.24.10.22</h1>
3 [root@client ~]#
4 [root@client ~]# curl 172.24.10.22:88
5 <h1>Chrmoe-172.24.10.22</h1>
6 [root@client ~]# curl 172.24.10.22:89
7 <h1>Firefox-172.24.10.22</h1>
8 [root@client ~]# curl 172.24.10.23:88
9 <h1>iPhone-172.24.10.23</h1>
10 [root@client ~]# curl 172.24.10.23:89
11 <h1>Android-172.24.10.23</h1>
12 [root@client ~]# curl 172.24.10.24:88
13 <h1>IE Browser-172.24.10.24</h1>
6.2 反向代理配置
1 [root@nginx01 ~]# mkdir /usr/share/nginx/type/2 [root@nginx01 ~]# echo '<h1>Type-172.24.10.21</h1>' > /usr/share/nginx/type/index.html
1 [root@nginx01 ~]# vi /etc/nginx/conf.d/type.conf2 upstream chrome {
3 server 172.24.10.22:88;
4 }
5 upstream firefox {
6 server 172.24.10.22:89;
7 }
8 upstream iphone {
9 server 172.24.10.23:88;
10 }
11 upstream android {
12 server 172.24.10.23:89;
13 }
14 upstream iebrowser {
15 server 172.24.10.24:88;
16 }
17
18 server {
19 listen 80;
20 server_name type.linuxds.com;
21 access_log /var/log/nginx/type.access.log main;
22 error_log /var/log/nginx/type.error.log warn;
23 proxy_set_header Host $host;
24 proxy_set_header X-Real-IP $remote_addr;
25 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
26 client_max_body_size 10m; #允许客户端请求的最大单文件字节数
27 client_body_buffer_size 128k; #缓冲区代理缓冲用户端请求的最大字节数
28 proxy_connect_timeout 300; #nginx跟后端服务器连接超时时间(代理连接超时)
29 proxy_send_timeout 300; #后端服务器数据回传时间(代理发送超时)
30 proxy_read_timeout 300; #连接成功后,后端服务器响应时间(代理接收超时)
31 proxy_buffer_size 4k; #设置代理服务器(nginx)保存用户头信息的缓冲区大小
32 proxy_buffers 4 32k; #proxy_buffers缓冲区,网页平均在32k以下的话,这样设置
33 proxy_busy_buffers_size 64k; #高负荷下缓冲大小(proxy_buffers*2)
34 proxy_temp_file_write_size 64k; #设定缓存文件夹大小,大于这个值,将从upstream服务器传
35 location / {
36 root /usr/share/nginx/type/;
37indexindex.html;
38
39if ($http_user_agent ~* "chrome"){
40 proxy_pass http://chrome;
41 }
42if ($http_user_agent ~* "firefox"){
43 proxy_pass http://firefox;
44 }
45if ($http_user_agent ~* "android"){
46 proxy_pass http://android;
47 }
48if ($http_user_agent ~* "iphone"){
49 proxy_pass http://iphone;
50 }
51if ($http_user_agent ~* "MSIE"){
52 proxy_pass http://iebrowser;
53 }
54 }
55 }
1 [root@nginx01 ~]# nginx -t -c /etc/nginx/nginx.conf #检查配置文件2 [root@nginx01 ~]# nginx -s reload #重载配置文件
6.3 确认验证
七 反向代理及缓存
7.1 环境预设
主机 | IP | 备注 |
nginx01 | 172.24.10.21 | 反向代理及缓存服务器 |
nginx02 | 172.24.10.22 | 后端RS01 |
nginx03 | 172.24.10.23 | 后端RS01 |
1 [root@nginx02 ~]# mkdir /usr/share/nginx/cache/2 [root@nginx02 ~]# echo '<h1>Cache-172.24.10.22</h1>' > /usr/share/nginx/cache/index.html
3
1 [root@nginx02 ~]# cat > /etc/nginx/conf.d/cache.conf <<EOF2 server {
3 listen 90;
4 server_name 172.24.10.22;
5 location / {
6 root /usr/share/nginx/cache;
7indexindex.html;
8 access_log /var/log/nginx/cache.access.log main;
9 error_log /var/log/nginx/cache.error.log warn;
10 }
11 }
12 EOF
1 [root@nginx02 ~]# ll /usr/share/nginx/cache2 total 16K
3 -rw-r--r-- 1 root root 28 Jun 23 22:33 index.html
4 -rw-r--r-- 1 root root 11K Jun 23 22:34 nginx.jpg #上传一张测试图片
5 [root@nginx02 ~]# nginx -t -c /etc/nginx/nginx.conf #检查配置文件
6 [root@nginx02 ~]# nginx -s reload #重载配置文件
1 [root@nginx03 ~]# mkdir /usr/share/nginx/cache/2 [root@nginx03 ~]# echo '<h1>Cache-172.24.10.23</h1>' > /usr/share/nginx/cache/index.html
1 [root@nginx03 ~]# cat > /etc/nginx/conf.d/cache.conf <<EOF2 server {
3 listen 90;
4 server_name 172.24.10.23;
5 location / {
6 root /usr/share/nginx/cache;
7indexindex.html;
8 access_log /var/log/nginx/cache.access.log main;
9 error_log /var/log/nginx/cache.error.log warn;
10 }
11 }
12 EOF
1 [root@nginx03 ~]# ll /usr/share/nginx/cache2 total 16K
3 -rw-r--r-- 1 root root 28 Jun 23 22:33 index.html
4 -rw-r--r-- 1 root root 11K Jun 23 22:34 nginx.jpg #上传一张测试图片
5 [root@nginx03 ~]# nginx -t -c /etc/nginx/nginx.conf #检查配置文件
6 [root@nginx03 ~]# nginx -s reload #重载配置文件
7.2 配置代理及缓存
1 [root@nginx01 ~]# mkdir -p /data/cache #创建缓存目录2 [root@nginx01 ~]# mkdir -p /data/cache_temp #创建缓存临时目录
1 [root@nginx01 ~]# vi /etc/nginx/nginx.conf #追加如下代码日志记录2 ……
3 log_format main '[$remote_addr]-[$remote_user]-[$time_local]-["$request"]'
4 '[$status]-[$body_bytes_sent]-["$http_referer"]'
5 '["$http_user_agent"]-["$http_x_forwarded_for"]';
6 '[$upstream_addr]-[$upstream_status]-[$request_time]-[$upstream_response_time]'
7 ……
1 [root@nginx01 ~]# vi /etc/nginx/conf.d/cache.conf2 upstream cache {
3 server 172.24.10.22:90;
4 server 172.24.10.23:90;
5 }
6
7 proxy_temp_path /data/cache_temp;
8 proxy_cache_path /data/cache levels=1:2 keys_zone=mycache:20m max_size=10g inactive=60m use_temp_path=off;
9
10 server {
11 listen 80;
12 server_name cache.linuxds.com;
13 access_log /var/log/nginx/cache.access.log main;
14 error_log /var/log/nginx/cache.error.log warn;
15
16 location / {
17 expires 3d;
18 add_header Nginx-Cache "$upstream_cache_status"; #增加一个头信息
19
20 proxy_cache mycache; #调用定义的cache zone
21 proxy_pass http://cache; #配置反向代理
22 proxy_set_header Host $host;
23 proxy_set_header X-Real-IP $remote_addr;
24 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
25
26 proxy_cache_valid 200 302 304 2h; #200和302及304头信息过期时间为2小时
27 proxy_cache_valid any 10m; #其他过期时间10分钟
28 proxy_cache_key $host$request_uri$uri$is_args$args; #定义缓存的key
29 proxy_cache_bypass $cookie_nocache $arg_comment; #配置不缓存
30 proxy_no_cache $arg_nocache; #配置不缓存
31 proxy_cache_lock on;
32 proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504; #一个服务报错请求下一个
33 }
34 location ~ /purge(/.*) {
35 allow 127.0.0.1;
36 allow 172.24.10.0/24;
37 deny all;
38 proxy_cache_purge mycache $1$is_args$args;
39 }
40 }
1 [root@nginx01 ~]# nginx -t -c /etc/nginx/nginx.conf #检查配置文件2 [root@nginx01 ~]# nginx -s reload #重载配置文件
- MISS:未命中缓存,即在缓存中找不到响应,因此从原始服务器获取响应。然后缓存响应;
- HIT:命中缓存,响应将直接来自有效的缓存;
- EXPIRED:缓存已经过期,响应包含来自原始服务器的新内容;
- STALE:命中了陈旧的缓存,因为源服务器未正确响应但proxy_cache_use_stale已配置。
- UPDATING:内容陈旧,因为条目当前正在更新以响应先前的请求,并且proxy_cache_use_stale updating已配置;
- REVALIDATED:Nginx验证了陈旧的内容依然有效;
- BYPASS:响应是从原始服务器获得。
7.3 确认验证
1 [root@client ~]# curl -I http://cache.linuxds.com/nginx.jpg
以上是 010.Nginx薄厚代理 的全部内容, 来源链接: utcz.com/a/57692.html