搭建HTTP/2 代理

2018年9月29日 | 分类: 翻墙相关 | 标签: ,

这个月又抢到了内部的100元腾讯云代金券,听说最近流行HTTP/2 代理,快速折腾一下,主要内容都是参考这篇文章:《使用 nghttpx 搭建 HTTP/2 代理》

证书

HTTP/2 代理其实也就是HTTPS 代理了,首先需要一个证书,可以自己签,太麻烦了。腾讯云和阿里云都提供了免费的DV 证书。搞一个。

因为域名托管在DNSPOD,在腾讯云申请都无需验证,挺快的,会给一个压缩包,打开里面Nginx 子目录,拿到crt 和key 备用。

nghttpx

nghttpx 可以对外提供HTTP/2 服务,将请求转换成HTTP/1.X 转发给后端,相当于一个中间人。

安装

aptitude install nghttp2

不懂为啥ubuntu 的包名是nghttp2,然后包含三个包:

nghttp2-client
nghttp2-proxy
nghttp2-server

编辑配置文件 /etc/nghttpx/nghttpx.conf

frontend=0.0.0.0,443
backend=127.0.0.1,3128
private-key-file=/root/ssl/2_xxxxx.fangpeishi.com.key
certificate-file=/root/ssl/1_xxxxx.fangpeishi.com_bundle.crt
http2-proxy=yes
errorlog-syslog=yes
workers=1

add-x-forwarded-for=no

no-via=yes
no-ocsp=yes
tls-proto-list=TLSv1.2
ciphers=ECDHE+AES128

我是抄的上文文章中的配置,先跑起来再说。。

systemctl restart nghttpx

squid

squid 作为nghttpx 后端的透明代理,不过记住要让它监听在本地,别暴露在公网上。

apt-get install squid

编辑配置文件/etc/squid/squid.conf:

http_port 127.0.0.1:3128

cache deny all
access_log none

dns_v4_first on

via off

forwarded_for delete

auth_param basic program /usr/lib/squid/basic_ncsa_auth  /etc/squid/passwd
auth_param basic casesensitive off
acl auth_user proxy_auth REQUIRED
http_access allow auth_user

也是抄的教程中的配置。。先跑起来再说。鉴权那段下文说明。

systemctl restart squid

鉴权

暴露在公网被其他人乱跑流量总不太好,在Squid 上加一个简单的HTTP Auth。

安装 htpasswd 工具:

apt-get install apache2-utils
#-c 创建文件
htpasswd  -c  /etc/squid/passwd [用户名]

# 添加其他用户
htpasswd /etc/squid/passwd [用户名]

/etc/squid/squid.conf 添加配置,再重启即可:

auth_param basic program /usr/lib/squid/basic_ncsa_auth  /etc/squid/passwd
auth_param basic casesensitive off
acl auth_user proxy_auth REQUIRED
http_access allow auth_user

更多关于squid 的配置需求,直接参考官方文档,应该没有啥是squid 做不到了。

BBR

再顺便升下内核,开启下 BBR,听说有奇效?不过我没有做对比。。直接开启了。

参考文章:《使用标准方式在 Ubuntu 16.04 下启用 TCP 拥塞控制之 BBR》

简明步骤,来着上面这篇文章:

安装内核

apt-get install linux-generic-hwe-16.04

reboot 之后,检查内核是不是> 4.9

uname -a 

加载模块,打开参数:

modprobe tcp_bbr
echo "tcp_bbr" | tee -a /etc/modules-load.d/modules.conf

echo "net.core.default_qdisc=fq" | tee -a /etc/sysctl.conf
echo "net.ipv4.tcp_congestion_control=bbr" | tee -a /etc/sysctl.conf
sysctl -p

最后验证一下:

sysctl net.ipv4.tcp_congestion_control

返回结果必须是:

net.ipv4.tcp_congestion_control = bbr

使用

搞完之后,验证一下,Chrome 安装SwitchyOmega 插件,新建一份配置,协议选择HTTPS,记得填入HTTP Auth验证信息。然后选中启用,打开 whatismyip 看看IP 是不是变了。在不同的平台上要使用,找不同的客户端支持吧。

原文:https://fangpeishi.com/http2_proxy.html

  1. iGFW
    2018年10月11日09:48
  2. 2018年9月30日22:13

    http2代理用gost 更快,多合一,证书国内这个不安全还是用 Let’s Encrypt