sword

frps利用nginx反代缓存,http加速
由于frp的http和https,都是从用户的穿透服务中完整输出数据的,这对于一些使用frp的用户,网络比较差/上...
扫描右侧二维码阅读全文
21
2018/01

frps利用nginx反代缓存,http加速

由于frp的http和https,都是从用户的穿透服务中完整输出数据的,这对于一些使用frp的用户,网络比较差/上传低,打开自己的这些服务,要加载大半天的。
我们可以使用nginx的反代缓存,把frp用户的http和https中的静态资源缓存到服务器本地,从而减少frp用户本身的网络资源请求访问,直接略过大部分,从而在服务器加速。
效果是拔群的!

需要在frps服务端的服务器上安装使用nginx
注意:仅适用http https 协议网站

SSH
新建缓存目录
mkdir -pv /home/nginx/cache
赋予权限
chmod -R 777 /home/nginx/cache

在nginx.conf中http{}里添加以下参数

 proxy_cache_path /home/nginx/cache levels=1:2 keys_zone=frp_cache:50m max_size=5g inactive=3d;

server
{
    listen 80;
    server_name your.nat.ee;
    resolver 223.5.5.5 208.67.222.222 8.8.8.8 valid=300s;
    resolver_timeout 10s;
    add_header X-Cache $upstream_cache_status;
    
    #ERROR-PAGE-START  错误页配置,可以注释、删除或修改
    proxy_intercept_errors on;
    error_page 404 https://www.nat.ee/error_page/404.html;
    #ERROR-PAGE-END
    
    location / {
           proxy_pass http://127.0.0.1:8080;
           proxy_redirect http://$host/ http://$http_host/;
           proxy_set_header X-Real-IP $remote_addr;
           proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
           proxy_set_header Host $host;
           proxy_hide_header X-Powered-By;
           proxy_ignore_headers Cache-Control;
           proxy_hide_header Cache-Control;
           proxy_ignore_headers Set-Cookie;
           proxy_hide_header Set-Cookie;
           limit_conn perip 5;
           limit_rate 128k;
       }
       location ~ .*\.(jpg|jpeg|gif|png|svg|css|scss|js|ico|xml|woff|woff2|ttf|otf|eot)$ {
           resolver 127.0.0.1;
           proxy_pass http://127.0.0.1:8080;
           proxy_redirect http://$host/ http://$http_host/;
           proxy_set_header X-Real-IP $remote_addr;
           proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
           proxy_set_header Host $host;
           proxy_cache frp_cache;
           proxy_cache_use_stale updating;
           proxy_cache_key $uri$is_args$args;
           proxy_cache_valid 200 206 301 302 304 3d;
           proxy_cache_lock on;
           proxy_cache_lock_timeout 5s;
           proxy_cache_use_stale updating error timeout invalid_header http_500 http_502;
           proxy_http_version 1.1;
           proxy_ignore_headers Cache-Control;
           proxy_hide_header Cache-Control;
           proxy_ignore_headers Set-Cookie;
           proxy_hide_header Set-Cookie;
           expires 3d;
           limit_conn perip 6;
           limit_rate 128k;
        }
}

(jpg|jpeg|gif|png|svg|css|scss|js|ico|xml|woff|woff2|ttf|otf|eot)为需要进行缓存的静态资源,你可以添加或者修改。

proxy_cache_valid为服务器缓存,其中200 206 301 302 304为HTTP状态码(http://tool.chinaz.com/pagestatus/)针对状态码缓存,而最后面的 3d 为缓存过期时间,当用户没有在这个有效时间内访问到这个资源,则会过期清除,直到用户重新访问到这个资源则重新缓存。
expires 为访问用户本地缓存
d 天数 h 小时 m 分钟 s 秒

http://127.0.0.1:8080;的8080端口为你frps.ini服务端配置文件vhost_http_port = 8080端口

server_name your.nat.ee; 这里填写对应域名。

也支持泛解析,例如:server_name *.nat.ee;需要域名已经泛解析才行。

配置成功后,并且访问目标网站,让其进行缓存,在/home/nginx/cache目录里会生成多个缓存目录和文件。

至于,手动清除缓存方法。

到/home/nginx/cache文件夹删除所有缓存文件。
或者给你的nginx重新编译,添加ngx_cache_purge模块
官方网站:http://labs.frickle.com/nginx_ngx_cache_purge/
github:https://github.com/FRiCKLE/ngx_cache_purge/
参数:

location ~ /purge(/.*) {
              allow all;
              proxy_cache_purge frp_cache $uri$is_args$args;
    }

具体用法和编译教程,自己网上搜索。

版权属于:NAT.EE

原文链接:https://www.nat.ee/server/151.html

Last modification:December 2nd, 2018 at 08:52 am
If you think my article is useful to you, please feel free to appreciate

Leave a Comment