🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
* proxy_pass指令 static01 └── cuffs └── css └── test01.jpg static01cuffs └── css └── test01.jpg proxy └── cuffs └── css └── test01.jpg cuffs └── css └── test01.jpg 第一种绝对路径: ~~~ location /proxy/ { proxy_pass http://10.0.0.1:8080/; } ~~~ 当访问http://test.51yuki.cn/proxy/cuffs/css/test01.jpg 时,nginx匹配到proxy路径,把请求转给10.0.0.1:8080服务,实际请求的路径为http://10.0.0.1:8080/cuffs/css/test01.jpg 第二种相对路径 ~~~ location /proxy/ { proxy_pass http://10.0.0.1:8080; } ~~~ 当访问http://test.51yuki.cn/proxy/cuffs/css/test01.jpg 时,nginx匹配到/proxy/路径,把请求转发给10.0.0.1:8080服务,实际请求代理服务器的路径为http://10.0.0.1:8080/proxy/cuffs/css/test01.jpg 此时nginx会把匹配的proxy也代理给代理服务器 注意: location /bbs/ { proxy_pass http://192.168.20.139:8085/; } 表示当访问http://test.51yuki.cn/bbs/index.html 代理服务器映射到http://192.168.20.139:8085/ 第三种:当前后路径不一致 ~~~ location /proxy/ { proxy_pass http://10.0.0.1:8080/static01/; } ~~~ 当访问 http://127.0.0.1/proxy/cuffs/css/test01.jpg时, nginx匹配到/proxy/路径,把请求转发给10.0.0.1:8080服务,实际请求代理服务器的路径http://10.0.0.1:8080/static01/cuffs/css/test01.jpg 注意: 如果static01后面的/ 不输入的话,就会造成实际请求代理服务器的路径为http://10.0.0.1:8080/static01cuffs/css/toosimple.txt 第四种:模式匹配 ~~~ location ~* \.(jpg|png|gif|jpeg)$ { proxy_pass http://192.168.20.139:8085; } ~~~ 注意:这个proxy_pass后面不能带有斜线,否则就判定为语法错误 当访问http://test.51yuki.cn/images/test01.jpg nginx匹配到该规则,就把请求转发给后端192.168.20.139:8085服务器,实际请求代理服务器的路径为 http://192.168.20.139:8085/images/test01.jpg 第五种: URL重写 如果location中有url重写功能,会把重写后的URL地址,附带在后端服务器 * proxy_set_header field value; 功能:允许重新定义或添加字段传递给代理服务器的请求头。该值可以包含文本、变量和它们的组合,proxy_set_header 就是可设置请求头-并将头信息传递到服务器端。 案例: proxy_set_header Host $host:$proxy_port; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Real-PORT $remote_port; ~~~ location /forum/ { proxy_pass http://192.168.20.139:8085/bbs/; proxy_set_header Host $host:$proxy_port; #proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-For $remote_addr; } 服务器端日志 192.168.20.131 - - [11/Feb/2018:14:09:07 +0800] "GET /bbs/ HTTP/1.0" 200 24 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36" "10.2.18.231" 其中10.2.18.231为真实客户端的ip地址,192.168.20.131为代理服务器的内网IP地址 ~~~ * proxy_redirect proxy_redirect redirect replacement; 场景1: ~~~ location / { proxy_pass http://192.168.20.139:8085; proxy_set_header Host $host:$proxy_port; proxy_set_header X-Forwarded-For $remote_addr; 当客户端使用浏览器输入http://test.51yuki.cn/bbs/index.html 会自动跳转到http://test.51yuki.cn:8085/bbs/index.hml ~~~ 遇到上面的情况,我们就可以使用proxy_redirect proxy_redirect http://test.51yuki.cn:8085/ http://test.51yuki.cn/; 发现就可以正常访问 场景2 ~~~ location /test/ { proxy_pass http://10.65.192.xx:8080/; proxy_redirect http://$host/ https://$host:$server_port/test/; } ~~~ 二)proxy代理超时时间 * proxy_connect_timeout 功能:proxy_connect_timeout是和后端建立连接的超时时间。需要记住的是,这个时间不能超过75秒 * proxy_read_timeout 功能:从后端读取数据的响应时间,只是在两个连续的读取操作之间设置超时。 它决定了nginx会等待多长时间来获得请求的响应 * proxy_send_timeout 功能: 这个指定设置了发送请求给upstream服务器的超时时间。超时设置不是为了整个发送期间,而是在两次write操作期间。如果超时后,upstream没有收到新的数据,nginx会关闭连接 案例: proxy_connect_timeout 65s; proxy_send_timeout 65ss; proxy_read_timeout 65s; 三)缓冲相关 nginx从后端服务器获得响应报文后,默认会将其缓存起来,响应包文件被存储在内部缓存中,直到接受完整个报文,然后构建响应报文,发送给客户端,常见指令如下 响应报文: 响应首部,报文主体 * proxy_buffering 功能:负载开启或关闭代理缓冲,默认为on * proxy_buffer_size ~~~ Syntax: proxy_buffer_size size; Default: proxy_buffer_size 4k|8k; Context: http, server, location ~~~ 功能:负责设置接收第一部分响应报文的缓冲的大小 * proxy_buffer ~~~ Syntax: proxy_buffers number size; Default: proxy_buffers 8 4k|8k; Context: http, server, location ~~~ 功能: 负责接收第二部分响应报文的缓冲个数以及大小 * proxy_temp_path ~~~ Syntax: proxy_temp_path path [level1 [level2 [level3]]]; Default: proxy_temp_path proxy_temp; Context: http, server, location ~~~ 功能:该指令用于配置磁盘上的一个文件路径,该文件用于临时存放代理服务器的大体积响应数据 案例: proxy_temp_path /nginx/proxy_web/spool/proxy_temp 1 2; /nginx/proxy_web/spool/proxy_temp/1/10/00000100101 * proxy_max_temp_file_size 功能:该指令用于配置所有临时文件的总体积大小,存放在磁盘上的临时文件大小不能超过该配置值,这避免了响应数据过大造成磁盘空间不足的问题 四)缓存相关的 * proxy_cache_path 语法: ~~~ Syntax: 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]; Default: — Context: http ~~~ 功能:设置缓存数据存储的位置(这个参数可以定义多个) 案例: proxy_cache_path /data/nginx/cache levels=1:2 keys_zone=one:10m; * proxy_cache_min_uses 功能:某一个响应报文被响应了多少次才被缓存下来 * proxy_cache_use_stale ~~~ Syntax: proxy_cache_use_stale error | timeout | invalid_header | updating | http_500 | http_502 | http_503 | http_504 | http_403 | http_404 | off ...; Default: proxy_cache_use_stale off; Context: http, server, location ~~~ 确定在与代理服务器通信期间发生错误时,在哪些情况下可以使用过时的高速缓存响应 * proxy_cache_valid ~~~ Syntax: proxy_cache_valid [code ...] time; Default: — Context: http, server, location ~~~ 功能: 设置不同响应代码的缓存时间 案例: proxy_cache_valid 200 302 10m; proxy_cache_valid 301 1h; proxy_cache_valid any 1m; * proxy_cache_bypass ~~~ Syntax: proxy_cache_bypass string ...; Default: — Context: http, server, location ~~~ 功能:定义哪些情况下,不要从缓存获取响应 案例: proxy_cache_bypass $cookie_nocache $arg_nocache$arg_comment; proxy_cache_bypass $http_pragma $http_authorization; * proxy_cache_key ~~~ Syntax: proxy_cache_key string; Default: proxy_cache_key $scheme$proxy_host$request_uri; Context: http, server, location ~~~ 功能: 定义缓存的键 案例: proxy_cache_key $scheme$proxy_host$uri$is_args$args; 缓存案例01: proxy_cache_path /data/nginx/cache/ levels=1:2:2 keys_zone=mycache:128m; (在http段定义缓存) server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; index index.html index.htm; } (在location段调用缓存) location /bbs/ { proxy_cache mycache; proxy_cache_methods GET HEAD; proxy_cache_key $scheme$proxy_host$uri$is_args$args; proxy_cache_min_uses 2; proxy_cache_use_stale error timeout invalid_header http_500 http_502 http_503 http_504; proxy_cache_valid 200 10m; proxy_cache_valid 301 302 5m; proxy_cache_valid any 1m; proxy_cache_bypass $cookie_nocache $arg_nocache$arg_comment $http_pragma $http_authorization; proxy_pass http://192.168.20.139:8085/bbs/; proxy_set_header Host $host:$proxy_port; proxy_set_header X-Forwarded-For $remote_addr; } 创建目录并授权 [root@proxy01 conf]# mkdir -pv /data/nginx/cache && chown -R www.www /data/nginx/cache 然后客户端浏览器访问,并查看代理服务器 http://test.51yuki.cn/bbs/ [root@proxy01 02]# pwd /data/nginx/cache/5/cd/02 [root@proxy01 02]# ll total 4 -rw-------. 1 www www 626 Feb 11 16:45 fe43e48c0935b141246e846c5c002cd5 缓存案例02:配置缓存修剪案例 http://soft.51yuki.cn/ngx_cache_purge-master.zip 配置如下: location ~ /purge(/.*) { auth_basic "allow purge cache data"; auth_basic_user_file "/opt/.htpasswd"; allow 10.2.18.0/24; allow 127.0.0.1; deny all; proxy_cache_purge mycache $host$1$is_args$args; } htpasswd -c -m /opt/.htpasswd tom (期间需要安装httpd服务) ~~~ 浏览器访问:http://test.51yuki.cn/purge/bbs/a.htm Successful purge Key : test.51yuki.cn/bbs/a.html Path: /data/nginx/cache/c/9a/5e/36e85e58edcd96369da181c0eab5e9ac nginx/1.12.2 ~~~