原文链接

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

背景描述

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

nginx反向代理缓存配置

新建缓存目录

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:100m max_size=5g inactive=30d;
resolver 8.8.8.8;
server {
    listen       80;
    server_name aiuyo.com;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        proxy_pass http://$host:8680;
        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;
    }

        location ~* \.(jpg|jpeg|gif|png|svg|css|scss|js|ico|xml|woff|woff2|ttf|otf|eot)$ {
                proxy_pass http://$host:8680;
                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_key $uri$is_args$args;
                proxy_cache_valid 200 206 301 302 304 3d;
                expires 30d;
        }
}

server{
        listen 443 ssl;
        ssl on;
        server_name *.aiuyo.com;
        ssl_certificate /root/.acme.sh/aiuyo.com/fullchain.cer;
        ssl_certificate_key  /root/.acme.sh/aiuyo.com/aiuyo.com.key;
        ssl_trusted_certificate /root/.acme.sh/aiuyo.com/ca.cer;

        location / {
                proxy_ssl_server_name on;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header X-Forwarded-Proto $scheme;
                proxy_set_header Host $host:$server_port;
                proxy_pass https://$host:8643; #通过域名访问frp服务
        }

        location ~* \.(jpg|jpeg|gif|png|svg|css|scss|js|ico|xml|woff|woff2|ttf|otf|eot)$ {
                proxy_ssl_server_name on;
                proxy_pass https://$host:8643; #通过域名访问frp服务
                proxy_redirect https://$host/ https://$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_set_header X-Forwarded-proto https;
                proxy_cache frp_cache;
                proxy_cache_key $uri$is_args$args;
                proxy_cache_valid 200 206 301 302 304 3d;
                expires 30d;
        }
}

配置说明

(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/)针对状态码缓存,而最后面的 30d 为缓存过期时间,当用户没有在这个有效时间内访问到这个资源,则会过期清除,直到用户重新访问到这个资源则重新缓存。
expires 为访问用户本地缓存
d 天数 h 小时 m 分钟 s 秒

http://127.0.0.1:8680;的8080端口为你frp.ini配置文件vhost_http_port = 8680端口
http://127.0.0.1:8643;的8443端口为你frp.ini配置文件vhost_https_port = 8643端口
对应的frps.ini配置如下:

# if you want to support virtual host, you must set the http port for listening (optional)
# Note: http port and https port can be same with bind_port
vhost_http_port = 8680
vhost_https_port = 8643

其中的端口可以根据自己的需要修改。

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

对于nginx https代理frp https的理解

我的理解是这样的:
如果使用了自定义域名,frp是根据$host值判断该往哪个内网服务转发请求的,因此转给frp的请求中一定要包含$host,否则frp无法正常处理请求。
因此以下2个配置很重要:

proxy_ssl_server_name on;
proxy_pass https://$host:8643; #通过域名访问frp服务

proxy_pass中不能写成IP的形式。
举一个我的例子,我内网服务器192.168.2.99上部署着2个服务,elasticsearch和kibana,端口分别是9200和5601,我现在想要把这2个服务穿透出去,并配置成https。具体配置如下:
frps.ini中的主要配置:

[common]
# A literal address or host name for IPv6 must be enclosed
# in square brackets, as in "[::1]:80", "[ipv6-host]:http" or "[ipv6-host%zone]:80"
bind_addr = 0.0.0.0
bind_port = 7000
……
# if you want to support virtual host, you must set the http port for listening (optional)
# Note: http port and https port can be same with bind_port
vhost_http_port = 8680
vhost_https_port = 8643

然后通过上边配置的nginx进行代理转发请求到frp上。
frpc.ini的配置如下:

[es]
type=https
custom_domains=es.aiuyo.com
local_ip=192.168.2.99
local_port=9200
use_encryption=true
use_compression=true

[kb]
type=https
custom_domains=kb.aiuyo.com
local_ip=192.168.2.99
local_port=5601
use_encryption=true
use_compression=true
届ける言葉を今は育ててる
最后更新于 2022-09-17