升级nginx以支持http2

本文详细介绍了一键升级脚本的使用方法,并介绍了HTTP/2相比于HTTP/1.x的主要优势,包括二进制格式传输、头压缩、多路复用等特性。此外,还提供了在CentOS 7环境下部署HTTP/2的具体步骤,包括升级nginx和OpenSSL的详细过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一键升级脚本

https://github.com/whisshe/upHttp2.git

HTTP/2 的优势

相比 HTTP/1.x,HTTP/2 在底层传输做了很大的改动和优化:

  1. HTTP/2 采用二进制格式传输数据,而非 HTTP/1.x 的文本格式。二进制格式在协议的解析和优化扩展上带来更多的优势和可能。
  2. HTTP/2 对消息头采用 HPACK 进行压缩传输,能够节省消息头占用的网络的流量。而 HTTP/1.x 每次请求,都会携带大量冗余头信息,浪费了很多带宽资源。头压缩能够很好的解决该问题。
  3. 多路复用,直白的说就是所有的请求都是通过一个 TCP 连接并发完成。HTTP/1.x 虽然通过 pipeline 也能并发请求,但是多个请求之间的响应会被阻塞的,所以 pipeline 至今也没有被普及应用,而 HTTP/2 做到了真正的并发请求。同时,流还支持优先级和流量控制。
  4. Server Push:服务端能够更快的把资源推送给客户端。例如服务端可以主动把 JS 和 CSS 文件推送给客户端,而不需要客户端解析 HTML 再发送这些请求。当客户端需要的时候,它已经在客户端了。

升级HTTP2必要条件

  • 1.10.0版本以上的nginx基于1.0.2以上版本的openssl编译
  • 必须支持https

软件安装位置

  • openssl
    1. 编译位置
      /usr/local/openssl
    2. 软链接位置
      /usr/bin/openssl
      /usr/include/openssl
  • nginx
    1. 编译及配置文件位置
      /etc/nginx

升级所有游戏服务器的nginx和OpenSSL

  • 升级openssl
    1. 将旧版本的openssl备份

      mv /usr/bin/openssl /usr/bin/openssl.old
      mv /usr/include/openssl /usr/include/openssl.old
    2. 下载1.1版本的openssl,并解压编译

      wget https://www.openssl.org/source/openssl-1.1.0g.tar.gz
      tar zxvf openssl-1.1.0g.tar.gz
      cd openssl-1.1.0g/
      ./config --prefix=/usr/local/openssl shared zlib
      make depend
      make && make install
      ln -s /usr/local/openssl/bin/openssl /usr/bin/openssl
      ln -s /usr/local/openssl/include/openssl /usr/include/openssl(首选)
      cd /usr/local/openssl/lib
      ln -s libssl.so.1.1 libcrypto.so.1.1 /lib/x86_64-linux-gnu/
      echo /usr/local/openssl/lib >> /etc/ld.so.conf
    3. 验证openssl版本信息

      openssl version
      if [[ `openssl version |awk '{print $2}'` == 1.1.0g ]];then
      echo ok
      fi
  • nginx升级(源码编译)
    原配置文件不会丢失

    1. 下载源码包

      wget http://nginx.org/download/nginx-1.12.2.tar.gz

    2. 解压编译安装

      编译安装 nginx 时,–with-openssl 参数默认只支持OpenSSL的源代码,不支持已编译好的 OpenSSL。可以在nginx的解压目录下修改auto/lib/openssl/conf
      sed "s/.openssl\///" c
      将文件中的.openssl去掉,就可以支持编译之后的openssl路径了

      CORE_INCS="$CORE_INCS $OPENSSL/.openssl/include"
      CORE_DEPS="$CORE_DEPS $OPENSSL/.openssl/include/openssl/ssl.h"
      CORE_LIBS="$CORE_LIBS $OPENSSL/.openssl/lib/libssl.a"
      CORE_LIBS="$CORE_LIBS $OPENSSL/.openssl/lib/libcrypto.a"

      正式编译
      “`
      ./configure –prefix=/etc/nginx \
      –sbin-path=/usr/sbin/nginx \
      –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 \
      –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-threads \
      –with-stream \
      –with-stream_ssl_module \
      –with-http_slice_module \
      –with-mail \
      –with-mail_ssl_module \
      –with-file-aio \
      –with-http_v2_module \
      –with-openssl=/usr/local/openssl

修改nginx配置

listen 443 ssl  http2;

出现的问题及解决

centos7下openssl version报错openssl: error while loading shared libraries: libssl.so.1.1


echo /usr/local/openssl/lib >> /etc/ld.so.conf # 将openssl编译路径的lib库加入到/etc/ld.so.conf
ldconfig -v|grep libssl # 检查是否添加成功

nginx重启出现错误,nginx: [emerg] mkdir() “/var/cache/nginx/client_temp” failed


ls -l /var/cache/nginx/client_temp#检查该文件夹是否存在和文件夹的权限
mkdir -p /var/cache/nginx/client_temp #文件夹不存在则生成文件夹
chown -R www-data:www-data /var/cache/nginx/client_temp # 将文件夹所有者和组改为nginx的运行用户,具体见nginx.conf

### 配置 NginxHTTP 升级HTTPS 为了将网站从 HTTP 升级HTTPS,需要对 Nginx 进行配置以支持 SSL/TLS 加密连接。以下是详细的步骤和配置说明: #### 1. 获取 SSL 证书 首先需要获取一个有效的 SSL 证书。可以通过购买商业证书或使用免费的 Let's Encrypt 证书来实现。证书通常包括两个文件: - 证书文件(`.pem` 或 `.crt`) - 私钥文件(`.key`) 可以将这些文件上传到服务器上的指定目录,例如 `/usr/local/nginx/conf/cert/` [^1]。 #### 2. 修改 Nginx 配置文件 编辑 Nginx 的主配置文件 `nginx.conf` 或站点配置文件,通常位于 `/usr/local/nginx/conf/` 目录下。添加或修改以下内容: ```nginx server { # 配置HTTPS的默认访问端口为443 listen 443 ssl; # 填写证书绑定的域名 server_name yourdomain.com; # 网站根目录 root /usr/share/nginx/html; index index.html index.htm; # 指定SSL证书和私钥路径 ssl_certificate cert/your-cert.pem; ssl_certificate_key cert/your-cert.key; # 设置SSL会话超时时间 ssl_session_timeout 5m; # 设置加密套件 ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4; # 设置使用的TLS协议版本 ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3; # 优先使用服务器的加密套件 ssl_prefer_server_ciphers on; location / { # Web网站程序存放目录 root /usr/share/nginx/html; index index.html index.htm; } } ``` #### 3. 配置 HTTP 自动跳转 HTTPS 为了让所有 HTTP 请求自动重定向到 HTTPS,可以在配置文件中添加一个新的 `server` 块,监听 80 端口,并通过 `rewrite` 指令进行重定向: ```nginx server { listen 80; # 填写证书绑定的域名 server_name yourdomain.com; # 将所有HTTP请求通过rewrite指令重定向到HTTPS rewrite ^(.*)$ https://$host$1 permanent; location / { index index.html index.htm; } } ``` #### 4. 重启 Nginx 服务 完成配置后,保存文件并重新加载 Nginx 配置以使更改生效: ```bash sudo nginx -s reload ``` #### 5. 验证 HTTPS 配置 打开浏览器,访问 `https://yourdomain.com`,确保能够正常加载页面并且地址栏显示锁形图标,表示连接已加密。 ### WebSocket 配置注意事项 如果网站使用了 WebSocket,在升级HTTPS 后,WebSocket 也需要升级为 WSS(WebSocket Secure)。此时可能需要在 Nginx 中配置代理以正确转发 WSS 请求: ```nginx location /socket.io/ { proxy_pass http://backend-server:8080; # 指向实际的后端服务地址 proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "Upgrade"; proxy_set_header X-real-ip $remote_addr; proxy_set_header X-Forwarded-For $remote_addr; } ``` 此配置确保 WebSocket 请求能够正确地通过 Nginx 代理到后端服务 [^2]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

whisshe

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值