在Debian上配置Nginx以支持HTTP/2和ALPN(应用层协议协商)需要几个步骤。以下是一个基本的指南:
安装Nginx: 如果你还没有安装Nginx,可以使用以下命令来安装它:
sudo apt update
sudo apt install nginx
安装OpenSSL: ALPN需要OpenSSL的支持。Debian的默认仓库中的OpenSSL版本可能不支持ALPN,所以你可能需要添加一个额外的仓库来安装支持ALPN的OpenSSL版本。
sudo apt install software-properties-common
sudo add-apt-repository ppa:ondrej/php
sudo apt update
sudo apt install openssl
编译Nginx with ALPN支持: 由于Debian的默认Nginx包可能不包含ALPN支持,你需要从源代码编译Nginx,并在编译时启用ALPN。
# 安装编译Nginx所需的依赖包
sudo apt install build-essential libpcre3 libpcre3-dev zlib1g-dev openssl libssl-dev
# 下载Nginx源代码
wget http://nginx.org/download/nginx-1.21.6.tar.gz
tar -zxvf nginx-1.21.6.tar.gz
cd nginx-1.21.6
# 配置Nginx编译选项,包括ALPN
./configure --with-http_ssl_module --with-http_v2_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-http_slice_module --with-mail --with-mail_ssl_module --with-file-aio --with-http_v3_module
# 编译并安装Nginx
make
sudo make install
请注意,你可能需要根据你的需求调整./configure命令中的选项。
配置Nginx:
编辑Nginx配置文件,通常位于/usr/local/nginx/conf/nginx.conf,来启用SSL和HTTP/2。
server {
listen 443 ssl http2;
server_name example.com www.example.com;
ssl_certificate /path/to/your/fullchain.pem;
ssl_certificate_key /path/to/your/privkey.pem;
# 其他配置...
}
确保将/path/to/your/fullchain.pem和/path/to/your/privkey.pem替换为你的SSL证书和私钥的实际路径。
重启Nginx: 为了使配置生效,你需要重启Nginx。
sudo /usr/local/nginx/sbin/nginx -s stop
sudo /usr/local/nginx/sbin/nginx
验证ALPN支持:
你可以使用openssl命令来验证Nginx是否正确地协商了HTTP/2。
openssl s_client -connect example.com:443 -nextprotoneg
如果Nginx配置正确并且支持ALPN,你应该能在输出中看到h2,表示HTTP/2协议被选中。
请注意,上述步骤中的版本号(例如nginx-1.21.6)可能会随着时间而变化。你应该从Nginx官方网站下载最新的稳定版本。此外,如果你不熟悉编译过程,可以考虑使用预编译的Nginx包或者寻找第三方提供的支持ALPN的Nginx包。