date: 2018-3-8 13:50:03
title: Swoft| Swoft官网全站 HTTP2 实践php
Swoft1.0正式来袭, Swoft 也迎来本身的一个里程碑, star数正式突破 1k. Swoft官网做为项目组服务开发者们的重要渠道, 也迎来了本身的一次重大更新:css
本篇先介绍 Swoft官网全站 HTTP2 实践html
先来一张 Swoft 官网 效果图镇楼:python
要实现 HTTP2 很是简单:mysql
首先查看 nginx 中是否开启了 HTTP2 module(模块)linux
# -V: show version and configure options then exit /var/www # nginx -V # 新版 nginx 默认开启了 HTTP2: --with-http_v2_module nginx version: nginx/1.13.8 built by gcc 6.2.1 20160822 (Alpine 6.2.1) built with OpenSSL 1.0.2n 7 Dec 2017 TLS SNI support enabled configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-http_auth_request_module --with-http_xslt_module=dynamic --with-http_image_filter_module=dynamic --with-http_geoip_module=dynamic --with-threads --with-stream --with-stream_ssl_module --with-stream_ssl_preread_module --with-stream_realip_module --with-stream_geoip_module=dynamic --with-http_slice_module --with-mail --with-mail_ssl_module --with-compat --with-file-aio --with-http_v2_module
nginx 开启 HTTP2 配置示例, 能够在个人开源项目-docker中查看到示例:nginx
# http2 server { listen 80; server_name www.daydaygo.top; # 将 HTTP 请求强制跳转到 HTTPS rewrite ^(.*)$ https://${server_name}$1 permanent; } server { # 开启 HTTP2 listen 443 ssl http2 default_server; server_name www.daydaygo.top; # 证书极简设置 ssl on; ssl_certificate daydaygo.top.crt; ssl_certificate_key daydaygo.top.key; root /var/www/https_test; index index.php index.html; location / {} location ~ \.php$ { fastcgi_pass fpm:9000; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }
Swoole 开启 HTTP2, 能够参考 Swoft 提供的 Dockerfilegit
# Debian系Linux apt-get install -y libssl-dev libnghttp2-dev # Swoole 添加编译参数 ./configure --enable-async-redis --enable-mysqlnd --enable-coroutine --enable-openssl --enable-http2
Swoft 配置中开启 HTTP2, 参考 .env.example 文件github
# 默认配置 OPEN_HTTP2_PROTOCOL=false SSL_CERT_FILE=/path/to/ssl_cert_file SSL_KEY_FILE=/path/to/ssl_key_file # 开启 HTTP2: 这里是将证书放到项目 resource/ 目录下 OPEN_HTTP2_PROTOCOL=true SSL_CERT_FILE=@res/ssl/ssl_cert_file SSL_KEY_FILE=@res/ssl/ssl_key_file
nginx 配合 Swoft 使用, 相似 nginx+fpm
配置便可, 代码示例能够参考 个人开源项目-dockerredis
# swoft-site server { listen 80; server_name swoft.daydaygo.top; # 将 HTTP 请求强制跳转到 HTTPS rewrite ^(.*)$ https://${server_name}$1 permanent; } server { # 开启 HTTP2 listen 443 ssl http2; server_name swoft.daydaygo.top; # 证书极简配置 ssl on; ssl_certificate 1_swoft.daydaygo.top_bundle.crt; ssl_certificate_key 2_swoft.daydaygo.top.key; root /var/www/swoole/swoft-offcial-site/public; index index.php index.html; error_log /var/log/nginx/swoft-site.error.log; access_log /var/log/nginx/swoft-site.access.log; # nginx 转发请求给 swoft location / { proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Connection "keep-alive"; proxy_pass https://swoft:9501; } location ~ \.php(.*)$ { proxy_pass https://swoft:9501; } # nginx 托管静态文件 location ~* \.(js|map|css|png|jpg|jpeg|gif|ico|ttf|woff2|woff)$ { expires max; } }
先确认你知道关于域名的几个基础知识:
若是这些都不熟悉, 建议申请一个域名体验一下.
域名证书分为 2 种: 单域名证书 泛域名证书, 区别来自于 什么是子域名. 好比我拥有域名 .daydaygo.top
, 那么我能够设置任意子域名, 好比 www.daydaygo.top
, test.www.daydaygo.top
. 若是是单域名证书, 那么我每个子域名都须要一个证书, 泛域名证书则能够对我全部的子域名生效.
域名证书由相关机构发放, 通常须要花钱购买. 既然是 福利, 这里介绍 2 个免费好用的途径:
腾讯云-申请免费单域名证书: https://console.qcloud.com/ssl
全程只须要动动鼠标便可:
而后下载证书, 配置到 nginx 中便可. 详细教程请参考腾讯云官方文档.
不过要注意:
Let's Encrypt 终于支持通配符证书了: https://www.jianshu.com/p/c5c...
Let's Encrypt
在免费域名证书领域算是 家喻户晓, 如今终于支持 通配符证书 了. 不过按照上面 blog 的教程, 非常一番折腾. 虽然一波三折, 可是得益于本身使用 docker 做为开发环境, 在尝试各类解决方案时, 都没有太大阻碍.
这里记录下来最终成功使用的一种方式:
nginx+centos7
环境, 出现教程# 安装 certbot yum install certbot-nginx # 稍微修改教程中的命令 certbot certonly -d *.daydaygo.top --manual --preferred-challenges dns --server https://acme-v02.api.letsencrypt.org/directory
以后一路确认, 最后添加 配置域名解析验证域名全部权, 大功告成!
[root@e6be50c34c81 www]# ll /etc/letsencrypt/live/daydaygo.top/ total 4 -rw-r--r-- 1 root root 543 Mar 16 16:48 README lrwxrwxrwx 1 root root 36 Mar 16 16:48 cert.pem -> ../../archive/daydaygo.top/cert1.pem lrwxrwxrwx 1 root root 37 Mar 16 16:48 chain.pem -> ../../archive/daydaygo.top/chain1.pem lrwxrwxrwx 1 root root 41 Mar 16 16:48 fullchain.pem -> ../../archive/daydaygo.top/fullchain1.pem lrwxrwxrwx 1 root root 39 Mar 16 16:48 privkey.pem -> ../../archive/daydaygo.top/privkey1.pem
查看 README
, 所得证书与 nginx 配置对应关系以下:
ssl_certificate -> fullchain1.pem ssl_certificate_key -> privkey1.pem
certbot
还能够配置 crontab 来 自动更新证书, 按照 官方教程 配置便可
折腾的过程颇为一波三折, 简单记录一下, 但愿能给你们帮助:
apk add certbot
, 然而执行后报错不支持泛域名Let's Encrypt
官方新闻, 发现里面的 url 和教程的 url 不一样, 没细看下 觉得是 url 错误, 其实看到的这篇新闻比较早, url 是预发布时的 urlLet's Encrypt
官方新闻, 评论中看到正式 url 放出的新闻, 这就是上面教程中提到的连接, 从而知道使用的 certbot 版本不对: Certbot (Certbot >= 0.22.0)
certbot-auto
, 根据报错发现运行须要依赖 python + gugeas
, 因而又尝试使用本身的 docker 开发环境 - python 来尝试, 但 pip install python-gugeas
时一直报错, 解决软件依赖无果对技术保持好奇并敢于尝试新技术, 实在是一件颇为有趣的事.
荐书: 图灵社区 - HTTP/2基础教程不要由于环境, 限制了你的能力, 投入 docker 的怀抱吧