【教程】利用nghttpx使squid3支持HTTP/2
Tofloor
poster avatar
u855697021
deepin
2016-03-15 17:42
Author
本帖最后由 u855697021 于 2016-3-15 09:56 编辑

利用nghttpx使squid3支持HTTP/2,实现加密代理访问。(话只能说到这份上了)
首先,任何SSL连接都需要一个域名,所以需要设法申请一个。
不想花钱的可以申.tk等免费域名,想花钱更安稳些的,godaddy神马的都可以。

步骤:(服务器端,以Debian为例)
1. 下载Let's Encrypt Client:
  1. git clone https://github.com/letsencrypt/letsencrypt
Copy the Code

2. 为你的域名申请SSL证书(或者可以使用已有的证书):(注意在执行这一步前,确保你申请的域名指向了当前服务器的IP地址)
  1. cd letsencrypt
  2. sudo ./letsencrypt-auto certonly --standalone --email [你的邮箱] -d [你的域名] --agree-tos
Copy the Code
(如果服务器同时正在运行网站的话,则需要用webroot方式验证,
这种情况请参阅Let's Encrypt文档:https://letsencrypt.readthedocs.org/en/latest/)
证书颁发后,客户端会显示证书保存的位置,记下文件夹的路径。

3. 安装squid3与nghttp2:
  1. sudo apt-get install squid3 nghttp2
Copy the Code
(如果debian源未收录nghttp2的话,则需要从https://nghttp2.org/下载安装包)

4. 配置squid3(即编辑/etc/squid3/squid.conf),这个是我个人的配置:
  1. # [HTTP2 Proxy Backend]
  2. http_access allow localhost
  3. http_port 127.0.0.1:8000
  4. # No cache and log
  5. cache deny all
  6. access_log none
  7. # Prefer IPv4 sites
  8. dns_v4_first on
  9. # No via header
  10. via off
  11. # No x-forwarded-for header
  12. forwarded_for delete
Copy the Code
5. 建立文件/etc/nghttpx/nghttpx.conf,写入配置如下:
  1. frontend=*,1443(需要在其它端口上运行的请自行修改)
  2. backend=127.0.0.1,8000
  3. private-key-file=[证书文件夹路径]/privkey.pem
  4. certificate-file=[证书文件夹路径]/fullchain.pem
  5. http2-proxy=yes
  6. tls-proto-list=TLSv1.2
Copy the Code
6. 运行squid3,启动squid服务。
7. 运行nohup nghttpx >/dev/null 2>&1 &,启动nghttpx服务。

(客户端,需要使用Chrome或Opera访问该代理服务)
1. 下载Proxy SwitchyOmega:https://github.com/FelisCatus/Sw ... 17/SwitchyOmega.crx
2. 打开chrome://extensions或者opera://extensions,将下载的插件拖进去。(注意不能直接双击安装,这是Chrome等的限制)
3. 点击SwitchyOmega图标,进入选项页面,编辑代理设置。
新建一个代理,代理方式为HTTPS,服务器地址为你的域名,端口为你指定的端口(这里是1443),然后注意保存设置。
4. 点击SwitchyOmega图标,切换到刚才创建的代理,即可使用指定代理上网。

注意:
1. 每三个月需要更新一次Let's Encrypt的证书,不然Chrome可能提示证书无效。
2. Chrome 45(或者使用45版Chrome内核的浏览器)无法使用此方法,这是45版内核的bug所致。这种情况下请更新或更换浏览器。

Reply Favorite View the author
All Replies
avatar
u855697021
deepin
2016-03-16 01:55
#1
自顶帖...
Reply View the author
avatar
176******20
deepin
2016-03-16 02:36
#2
看起来很高深的样子
Reply View the author
avatar
joyk3000
deepin
2016-03-17 14:16
#3
不错,学习了
Reply View the author
avatar
u855697021
deepin
2016-03-27 21:28
#4
这几天为了让nghttpx能够随时启停和开机启动,写了一个服务脚本如下:
  1. #! /bin/sh

  2. ### BEGIN INIT INFO
  3. # Provides:          nghttpx
  4. # Required-Start:    $remote_fs
  5. # Required-Stop:     $remote_fs
  6. # Default-Start:     2 3 4 5
  7. # Default-Stop:      0 1 6
  8. # Short-Description: Nghttpx HTTP/2 proxy daemon
  9. # Description:       Nghttpx HTTP/2 proxy daemon
  10. ### END INIT INFO

  11. # Path
  12. PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin

  13. # Variables
  14. proc=nghttpx
  15. srv_name=${proc}
  16. rt_dir=/tmp/proxy
  17. user=nobody
  18. pid_file=${rt_dir}/${proc}.pid

  19. # Start service
  20. start() {
  21.     _status
  22.     if [ $? -eq 0 ]; then
  23.         return 0
  24.     fi

  25.     # Run daemon process
  26.     mkdir -p $rt_dir
  27.     nohup nghttpx --pid-file=$pid_file --user=$user >/dev/null 2>&1 &
  28. }

  29. # Stop service
  30. stop() {
  31.     _status
  32.     if [ $? -ne 0 ]; then
  33.         return 0
  34.     fi

  35.     # Kill daemon process
  36.     kill $(cat $pid_file)
  37.     rm $pid_file
  38. }

  39. # Restart service
  40. restart() {
  41.     stop
  42.     start
  43. }

  44. # Check status (Internal function)
  45. _status() {
  46.     if [ -e $pid_file ]; then
  47.         if ps $(cat $pid_file) >/dev/null 2>&1; then
  48.             return 0
  49.         fi
  50.     fi
  51.     return 1
  52. }

  53. # Check status
  54. status() {
  55.     _status
  56.     if [ $? -eq 0 ]; then
  57.         echo "Service $srv_name running."
  58.         exit 0
  59.     else
  60.         echo "Service $srv_name not running."
  61.         exit 1
  62.     fi
  63. }

  64. # Service operations
  65. case $1 in
  66.     start|stop|restart|status)
  67.         $1;;
  68.     # Show usage
  69.     *)
  70.         echo "Usage: $0 {start|stop|status|restart}";;
  71. esac
Copy the Code

把以上脚本写入/etc/init.d/nghttpx,
然后用update-rc.d或者chkconfig加入到开机启动项中就可以实现HTTP/2代理开机自启动了。
(貌似Squid也需要写一个服务脚本wrap一下,不过方法应该是类似的)
Reply View the author