当前位置: 首页 > 后端技术 > PHP

斯威夫特-Swoft官网HTTP2实践

时间:2023-03-29 15:10:08 PHP

日期:2018-3-813:50:03标题:Swoft|Swoft官网HTTP2实践Swoft1.0正式上线,Swoft也迎来了属于自己的里程碑,star数正式突破1k。Swoft官网作为项目团队开发服务读者的重要渠道,也迎来了属于自己的重大更新:重构,升级到Swoft1.0,全站实现HTTP2。本文首先介绍Swoft官网的HTTP2实践。资源由nginx托管,http2业务代码启用swoft执行。设置SwooleHttpServer使用HTTP2协议。实现HTTP2很简单:nginxenableHTTP2SwoftenableHTTP2nginx+Swoft。HTTP2模块(module)#-V:showversionandconfigureoptionsthenexit/var/www#nginx-V#新版nginx默认开启HTTP2:--with-http_v2_modulenginxversion:nginx/1.13.8builtbygcc6.2.120160822(Alpine6.2.1)使用OpenSSL1.0.2n构建2017年12月7日TLSSNI支持启用配置参数:--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/日志/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_modulenginx打开HTTP2配置示例,示例可以在我的开源项目-docker中查看:#http2server{listen80;服务器名称www.daydaygo.top;#强制HTTP请求跳转到HTTPSrewrite^(.*)$https://${server_name}$1permanent;}server{#开启HTTP2listen443sslhttp2default_server;服务器名称www.daydaygo.top;#证书极简设置sslon;ssl_certificatedaydaygo.top.crt;ssl_certificate_keydaydaygo.top.key;根/var/www/https_test;索引index.phpindex.html;位置/{}位置~\.php${fastcgi_passfpm:9000;fastcgi_paramSCRIPT_FILENAME$document_root$fastcgi_script_name;包括fastcgi_params;}}Swoft开启HTTP2Swoole开启HTTP2可以参考Swoft提供的Dockerfile#Debian系统Linuxapt-getinstall-ylibssl-devlibnghttp2-dev#Swoole添加编译参数./configure--enable-async-redis--enable-mysqlnd--enable-coroutine--enable-openssl--enable-http2Swoft在配置中启用HTTP2,参考.env.examplefile#defaultconfigurationOPEN_HTTP2_PROTOCOL=falseSSL_CERT_FILE=/path/to/ssl_cert_fileSSL_KEY_FILE=/path/to/ssl_key_file#OpenHTTP2:这里是把证书放在项目resource/目录下OPEN_HTTP2_PROTOCOL=trueSSL_CERT_FILE=@res/ssl/ssl_cert_fileSSL_KEY_FILE=@res/ssl/ssl_key_filenginx使用nginx和Swift使用,类似nginx+fpm的配置,代码示例可以参考我的开源项目-docker#swoft-siteserver{listen80;server_nameswoft.daydaygo.top;#强制HTTP请求跳转到HTTPSrewrite^(.*)$https://${server_name}$1permanent;}server{#打开HTTP2listen443sslhttp2;server_nameswoft.daydaygo.top;#证书最小配置sslon;ssl_certificate1_swoft.daydaygo.top_bundle.crt;ssl_certificate_key2_swoft。daydaygo.top.key;root/var/www/swoole/swoft-offcial-site/public;索引index.phpindex.html;error_log/var/log/nginx/swoft-site.error.log;access_log/var/log/nginx/swoft-site.access.log;#nginx转发请求到swoftlocation/{proxy_set_headerX-Real-IP$remote_addr;proxy_set_header主机$host;proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for;代理集_标头连接“保持活动状态”;proxy_passhttps://swoft:9501;}location~\.php(.*)${proxy_passhttps://swoft:9501;}#nginx托管静态文件位置~*\.(js|map|css|png|jpg|jpeg|gif|ico|ttf|woff2|woff)${expiresmax;}}好处:域名证书申请简单指南确保您了解一些关于域名的基础知识:为什么要使用它们域名?什么是子域名?为什么需要备案域名?什么是域名证书?如果你对这些不熟悉,建议先申请一个域名来体验一下。域名。比如我自己的域名.daydaygo.top,那么我可以设置任意子域名,比如www.daydaygo.top,test.www.daydaygo.top。如果是单域名证书,那么我每个子域名都需要一个证书,泛域名证书可以对我所有的子域名都有效。域名证书由相关机构颁发,一般需要花钱购买。既然是福利,这里有两种免费好用的方式:动动鼠标,获取证书,腾讯云-申请免费的单域名证书,最后等待免费的泛域名证书,让我们加密泛域名证书单域名证书实践腾讯云-免费申请单域名证书:https://console.qcloud.com/ssl整个过程只需要动动鼠标:去腾讯官网云端申请配置域名解析并验证域名归属,然后下载证书并在nginx中进行配置。详细教程请参考腾讯云官方文档。但注意:证书有效期为1年,同一域名最多只能申请20个证书通配符域名证书实践Let'sEncrypt终于支持通配符证书了:https://www.jianshu.com/p/c5c。..Let'sEncrypt在免费域名证书领域小有名气,现在终于支持通配符证书了。但是按照上面博客上的教程,还是很麻烦的。虽然有波折,但由于使用了docker作为开发环境,在尝试各种解决方案时没有遇到太大的障碍。这里记录下最后使用成功的那个方法:来自官网教程:ACMEv2ProductionEnvironment&Wildcards:切记:必须使用ACMEv2compatibleclient才能访问这个端点ACMEv2CompatibleClients:结合以上博客,在certbot官网选择certbot,选择nginx+centos7环境,会出现教程使用自带的docker开发环境-centos进行练习#安装certbotyuminstallcertbot-nginx#稍微修改教程中的命令certbotcertonly-d*.daydaygo.top--manual--preferred-challengesdns--serverhttps://acme-v02.api.letsencrypt.org/目录后一路确认,最后添加配置域名解析,验证域名归属,大功告成![root@e6be50c34c81www]#ll/etc/letsencrypt/live/daydaygo.top/total4-rw-r--r--1rootroot543Mar1616:48READMElrwxrwxrwx1rootroot36Mar1616:48cert.pem->../../archive/daydaygo.top/cert1.pemlrwxrwxrwx1rootroot37Mar1616:48chain.pem->../../archive/daydaygo.top/chain1.pemlrwxrwxrwx1rootroot41Mar1616:48fullchain.pem->../../archive/daydaygo.top/fullchain1.pemlrwxrwxrwx1rootroot39Mar1616:48privkey.pem->../../archive/daydaygo.top/privkey1.pem查看README,获取的证书与nginx配置的对应关系如下:ssl_certificate->fullchain1.pemssl_certificate_key->privkey1.pemcertbot也可以配置crontab自动更新证书。按照官方教程配置的过程还是比较曲折的。简单记录一下吧。希望对你有帮助:我喜欢用alpinelinux,所以直接用自己的docker开发环境-alpine安装certbot:apkaddcertbot,但是执行后报错泛域名baiduzhi不是supported,第一个出现的文章是Let'sEncrypt官方消息,我发现里面的url和教程的url不一样。没仔细看还以为是url错误。其实这个新闻我早就看到了,url是预放的时候的url。继续阅读Let'sEncrypt官方消息,在评论中看到官方url发布的消息,这是上面教程中提到的链接,所以我知道使用的certbot版本不对:Certbot(Certbot>=0.22.0)另一个错误的尝试是使用certbot-auto,根据报错发现运行需要依赖python+gugeas,于是尝试使用自己的docker开发环境-python,但是pipinstallpython-gugeas一直报错,解决软件依赖无果蛮有意思的。推荐书籍:TuringCommunity-HTTP/2BasicTutorial别因为环境限制了自己的能力,投身docker的怀抱吧

最新推荐
猜你喜欢