关于Frp的加速方案:Nginx反向代理缓存加速HTTP/HTTPS

由于frp的httphttps,都是从用户的服务中完整输出数据的,这对于一些使用frp的用户,网络比较差/上传低,打开自己的这些服务,要加载大半天的。

我们可以使用nginx的反代缓存,把frp用户的httphttps中的静态资源缓存到服务器本地,从而减少frp用户本身的网络资源请求访问,直接略过大部分,从而在服务器加速。效果是拔群的!

nginx反向代理缓存配置

1、新建缓存目录

mkdir -pv /home/nginx/cache

2、赋予权限

chmod -R 777 /home/nginx/cache

3、在nginx.confhttp{}里添加以下参数

proxy_cache_path /home/nginx/cache levels=1:2 keys_zone=frp_cache:100m max_size=5g inactive=30d; server { listen 80; server_name moewah.com; #charset koi8-r; #access_log /var/log/nginx/host.access.log main; location / { proxy_pass http://127.0.0.1:8680; #反代http用127.0.0.1 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://127.0.0.1:8680; #反代http用127.0.0.1 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; #缓存成功 X-Cache-STatus 就是HIT,读取数据没缓存就是 MISS add_header X-Cache '$upstream_cache_status from $host'; } } server{ listen 443 ssl; server_name *.moewah.com; ssl_certificate /root/.acme.sh/moewah.com/fullchain.cer; ssl_certificate_key /root/.acme.sh/moewah.com/moewah.com.key; ssl_trusted_certificate /root/.acme.sh/moewah.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; proxy_pass https://$host:8643; #反代https通过域名访问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; #反代https通过域名访问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; #缓存成功 X-Cache-STatus 就是HIT,读取数据没缓存就是 MISS add_header X-Cache '$upstream_cache_status from $host'; } }

配置说明

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

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

http://127.0.0.1:8680;8080端口为你frp.ini配置文件vhost_http_port = 8680端口
https://$host: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无法正常处理请求报502错误。

因此以下2个配置很重要:

举个栗子:

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

proxy_pass中不能写成IP的形式。

举一个我的栗子:

假如我内网服务器192.168.2.99上部署着2个服务,elasticsearchkibana,端口分别是92005601,我现在想要把这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.moewah.com local_ip=192.168.2.99 local_port=9200 use_encryption=true use_compression=true [kb] type=https custom_domains=kb.moewah.com local_ip=192.168.2.99 local_port=5601 use_encryption=true use_compression=true

如果502错误问题依旧,请直接将$host用域名写死,例如将proxy_pass https://$host:8643;写成proxy_pass https://abc.com:8643;

文章参考:

相关推荐

原文链接:https://www.moewah.com/archives/1266.html

© 版权声明
THE END
喜欢就支持一下吧
点赞14 分享