版本支持
目前http2只支持https的網站,openssl版本必須在1.0.2e以上的版本,nginx版本必須在1.9.5版本以上,而且需要編譯參數支持:with-http_v2_module、with-http_ssl_module。
openssl
查看openssl版本
<code>openssl version/<code>
如果系統默認的openssl版本不夠支持,可以直接下載源碼,不需要安裝,只要在nginx編譯的時候,指定到對應的目錄。
下載源碼
<code>wget -c https://www.openssl.org/source/openssl-1.1.1d.tar.gz/<code>
下載PCRE
正則支持,也只是需要源碼即可,不需要安裝二進制。
<code>wget -c https://ftp.pcre.org/pub/pcre/pcre-8.43.tar.gz/<code>
下載Zlib
壓縮支持,也只是需要源碼即可,不需要安裝二進制。
<code>wget -c http://zlib.net/zlib-1.2.11.tar.gz/<code>
下載nginx
<code>wget -c http://nginx.org/download/nginx-1.16.1.tar.gz/<code>
解壓
<code>tar zxvf nginx-1.16.1.tar.gz/<code>
<code>tar zxvf zlib-1.2.11.tar.gz/<code>
<code>tar zxvf pcre-8.43.tar.gz/<code>
<code>tar zxvf openssl-1.1.1d.tar.gz/<code>
編譯安裝
<code>cd nginx-1.16.1/<code>
<code>./configure --prefix=/opt/nginx --with-stream --with-mail --with-http_geoip_module --with-http_ssl_module --with-mail_ssl_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-openssl=../openssl-1.1.1d --with-zlib=../zlib-1.2.11 --with-pcre=../pcre-8.43 --with-http_v2_module/<code>
<code>make/<code>
<code>make install/<code>
配置Server
直接在listen配置上加上http2的關鍵字即可開啟。
<code>server {/<code>
<code> server_name opcai.top;/<code>
<code> listen 443 ssl http2;/<code>
<code> #證書(公鑰.發送到客戶端的)/<code>
<code> ssl_certificate /opt/nginx/keys/server.crt;/<code>
<code> #私鑰,/<code>
<code> ssl_certificate_key /opt/nginx/keys/server.key;/<code>
<code> access_log logs/opcai.log;/<code>
<code> location /static/ {/<code>
<code> expires 1d;/<code>
<code> alias /data/web/static/;/<code>
<code> }/<code>
<code> add_header Access-Control-Allow-Headers $http_access_control_request_headers;/<code>
<code> location / {/<code>
<code> proxy_set_header Host $http_host;/<code>
<code> proxy_set_header X-Real-IP $remote_addr;/<code>
<code> proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;/<code>
<code> proxy_set_header X-Forwarded-Proto $scheme;/<code>
<code> proxy_pass http://192.168.1.100:8081;/<code>
<code> }/<code>
<code> }/<code>
檢查並啟動服務
<code>/opt/nginx/sbin/nginx -t /<code>
<code>/opt/nginx/sbin/nginx/<code>
閱讀更多 linux運維菜 的文章