安装版本大于等于 1.0.2 的OpenSSL


wget https://www.openssl.org/source/openssl-3.2.1.tar.gz
tar xzvf openssl-3.2.1.tar.gz

cd openssl-3.2.1

./config

make

make test

sudo make install

sudo ldconfig /usr/local/lib64/

openssl version

# if old version persists
sudo mv /usr/bin/openssl /root/
sudo ln -s /usr/local/ssl/bin/openssl /usr/bin/openssl

# move new source to /usr/lcoal for nginx conf
cd ..
sudo mv openssl-3.2.1 /usr/local

重新编译下载 nginx 以支持 ALPN

可以通过这里查找最新的 nginx version (通过年份搜索,比如 2024


sudo apt update

sudo apt-get perl install libpcre3 libpcre3-dev libperl-dev libxml2-dev libxslt-dev libgd-dev libgeoip-dev -y

# 编译的时候需要
git clone https://github.com/nginx/njs

wget "https://nginx.org/download/nginx-1.26.0.tar.gz"

tar -zxvf nginx-1.26.0.tar.gz

cd nginx-1.26.0

如果之前安装了 nginx 的话,需要卸载掉:


# 移除Nginx及其配置文件以及与其相关的包
sudo apt-get purge nginx

# 如果希望保留配置文件,可以使用以下命令, 这将仅移除Nginx软件包,而保留配置文件
sudo apt-get remove nginx

接下来,编译 nginx,你可以去掉不想要的 modules,同时,请注意,如果你的 openssl 版本变了的话,请修改 --with-openssl 的路径:


./configure --prefix=/etc/nginx\
 --user=nginx --group=nginx\
 --sbin-path=/usr/sbin/nginx\
 --modules-path=/usr/lib64/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\
 --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_image_filter_module=dynamic\
 --with-http_geoip_module=dynamic\
 --with-http_perl_module=dynamic\
 --with-http_ssl_module --with-openssl=/usr/local/openssl-3.2.1\
 --add-dynamic-module=../njs/nginx\
 --with-threads --with-stream --with-stream_ssl_module\
 --with-http_slice_module\
 --with-mail --with-mail_ssl_module\
 --with-file-aio\
 --with-ipv6 --with-http_v2_module\
 --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic'	

make

sudo make install

nginx -V

添加域名配置

/etc/nginx/conf.d 创建你的域名文件,如 example.com.conf:


server {
    listen 80;
    server_name example.com;

    location /.well-known/acme-challenge/ {
        root /var/www/certbot;
    }

    location / {
        proxy_pass http://localhost:8080;
    }
}

请在域名服务器配置域名到此服务器的解析,并开启 80443 这两个端口。这里可以测试一下,是不是通了。

申请并配置免费 Https 证书

接下来,安装 certbot,申请 https 证书。


sudo apt update

sudo apt install python3-certbot-nginx -y

sudo apt install certbot -y

# your domain
sudo certbot --nginx -d example.com

  1. 输入好自己的邮箱
  2. 选择 Redirect,certbot 会自动帮你配置好 https

添加 Http2 支持

/etc/nginx/conf.d/example.com.conf 中,在 listen 443 ssl 加上 http2 即可完成。


server {
    //...
    listen 443 ssl http2;
    //...
}