前言#
最近また Caddy から Nginx に戻りたいと思っていますが、現在 Nginx はデフォルトで HTTP/3 をサポートしていません。
Nginx で HTTP/3 を使用するには、現在 2 つの方法があります。ここでは公式の nginx-quic ブランチを使用します。
準備作業#
環境の準備#
sudo apt update && sudo apt upgrade -y
sudo apt install build-essential libtool libpcre3-dev zlib1g-dev libzstd-dev unzip cmake ninja-build wget git mercurial
ソフトウェアリポジトリの golang のバージョンが古すぎるのを防ぐため、ここでは手動インストールを使用します。
::: banner {info}
ソフトウェアリポジトリの golang バージョンが >=1.18 の場合、直接 apt でインストールできます。
すでに golang 1.18+ がインストールされている場合は、このステップは不要です。
:::
wget https://go.dev/dl/go1.20.3.linux-amd64.tar.gz
rm -rf /usr/local/go && tar -C /usr/local -xzf go1.20.3.linux-amd64.tar.gz
export PATH=$PATH:/usr/local/go/bin
ソースコードの準備#
ソースコードフォルダの作成 & ソースコードの取得#
mkdir nginx-quic-src
cd nginx-quic-src
hg clone -b quic https://hg.nginx.org/nginx-quic
# プラグインの取得
git clone --recurse-submodules https://github.com/google/ngx_brotli.git
git clone --recurse-submodules https://github.com/tokers/zstd-nginx-module.git
boringssl の取得 & コンパイル#
git clone https://github.com/google/boringssl.git
cd boringssl/
mkdir build
cd build
cmake -GNinja ..
ninja
cd ../../
コンパイル & インストール#
コンパイル#
./auto/configure \
--with-http_gzip_static_module \
--with-http_ssl_module --with-http_v2_module \
--with-http_v3_module --with-stream_quic_module \
--with-cc-opt="-I../boringssl/include" --with-ld-opt="-L../boringssl/build/ssl -L../boringssl/build/crypto" \
--add-module="../ngx_brotli" \
--add-module="../zstd-nginx-module"
make
インストール#
sudo make install
デーモンサービスの設定#
cat <<'TEXT' > /etc/systemd/system/nginx.service
[Unit]
Description=nginx - high performance web server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
PrivateTmp=true
[Install]
WantedBy=multi-user.target
TEXT
# 自動起動の設定 (オプション)
sudo systemctl enable nginx.service
設定#
基本設定#
/usr/local/nginx/conf/conf.d/nginx.conf
を作成して編集します。
zstd on;
brotli on;
gzip on;
zstd_static on;
brotli_static on;
gzip_static on;
zstd_types text/plain application/css text/css application/xml text/javascript application/javascript application/x-javascript application/json;
brotli_types text/plain application/css text/css application/xml text/javascript application/javascript application/x-javascript application/json;
gzip_types text/plain application/css text/css application/xml text/javascript application/javascript application/x-javascript application/json;
ssl_protocols TLSv1.2 TLSv1.3;
server {
listen 443 ssl default_server;
listen [::]:443 ssl default_server;
ssl_reject_handshake on;
}
/usr/local/nginx/conf/nginx.conf
を編集します。
#user nobody;
worker_processes auto;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
# Load configs
include /usr/local/nginx/conf/conf.d/*.conf;
include /usr/local/nginx/conf/sites-enabled/*;
}
これにより、Nginx のグローバル設定は /usr/local/nginx/conf/conf.d/nginx.conf
を編集することで行えます。
個々のサイトの設定は /usr/local/nginx/conf/sites-enabled/
下の対応する設定ファイルを編集することで行えます。
以下はサイト設定 /usr/local/nginx/conf/sites-enabled/example.conf
の例です。
server {
listen 443 quic;
listen 443 ssl http2;
listen [::]:443 quic;
listen [::]:443 ssl http2;
server_name example.com;
add_header Alt-Svc 'h3=":443"; ma=86400; h3-29=":443"; h3-28=":443";';
ssl_certificate example.com.cer;
ssl_certificate_key example.com.key;
location / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://localhost:1145;
}
}
ここで server_name
はこの設定ファイルに対応するドメイン名です。
ssl_certificate
と ssl_certificate_key
はあなたの SSL 証明書のパスに置き換えてください。
使用#
上記の設定が完了したら、次のコマンドを実行します。
sudo systemctl start nginx.service
エラーが発生した場合は、設定ファイルを自分で確認してください。
他に問題がある場合は、コメント欄で交流してください。
この記事は Mix Space によって xLog に同期更新されました。
元のリンクは https://blog.rikko.moe/posts/default/nginx-quic