Nginx反向代理缓存加速Frp HTTP/HTTPS实战指南
说明
由于frp的http和https,都是从用户的服务中完整输出数据的,这对于一些使用frp的用户,网络比较差/上传低,打开自己的这些服务,要加载大半天的。
我们可以使用nginx的反代缓存,把frp用户的http和https中的静态资源缓存到服务器本地,从而减少frp用户本身的网络资源请求访问,直接略过大部分,从而在服务器加速。效果是拔群的!
nginx反向代理缓存配置
1、新建缓存目录
mkdir -pv /home/nginx/cache2、赋予权限
chmod -R 777 /home/nginx/cache3、在nginx.conf中http{}里添加以下参数
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_portvhost_http_port = 8680vhost_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个服务,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.0bind_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_portvhost_http_port = 8680vhost_https_port = 8643然后通过上边配置的nginx进行代理转发请求到frp上。
frpc.ini的配置如下:
[es]type=httpscustom_domains=es.moewah.comlocal_ip=192.168.2.99local_port=9200use_encryption=trueuse_compression=true
[kb]type=httpscustom_domains=kb.moewah.comlocal_ip=192.168.2.99local_port=5601use_encryption=trueuse_compression=true补充
如果502错误问题依旧,请直接将$host用域名写死,例如将proxy_pass https://$host:8643;写成proxy_pass https://abc.com:8643;
文章参考:
推荐文章
基于标签匹配 · 智能推荐支持与分享
如果这篇文章对你有帮助,欢迎分享给更多人或赞助支持!
喵斯基部落