1、Docker配置
services:
nginx: #服务名,udp443是http3所用
container_name: nginx
image: nginx:1.31.2
privileged: true
environment:
TZ: Asia/Shanghai
ports:
- 80:80/tcp
- 443:443/tcp
- 443:443/udp
volumes:
- ./config/nginx/nginx.conf:/etc/nginx/nginx.conf
- ./data/nginx/:/usr/share/nginx/html/
- ./log/nginx/:/var/log/nginx/
- ./config/nginx/ssl:/etc/nginx/ssl
restart: always
mysql: #服务名
container_name: mysql #容器名
image: mysql:8.4.9
privileged: true
environment:
MYSQL_ROOT_PASSWORD: 200412aaAA@#
MYSQL_ROOT_HOST: '%'
TZ: Asia/Shanghai
ports:
- "33306:3306"
volumes:
- ./config/mysql/my.cnf:/etc/mysql/my.cnf
- ./init/mysql/:/docker-entrypoint-initdb.d/ #初始化sql脚本数据
- ./data/mysql/data/:/var/lib/mysql/
- ./data/mysql/mysql-files/:/var/lib/mysql-files/ #mysql8需要这个配置
- ./log/mysql/:/var/log/mysql/
command:
--character-set-server=utf8mb4
--collation-server=utf8mb4_unicode_ci
restart: always #开机启动
2、Ngin配置
server {
listen 80;
server_name www.baidu.com baidu.com;
return 301 https://$server_name$request_uri; # 所有HTTP请求重定向到HTTPS,$server_name只能取到第一个,需要www的放最前面
}
server {
listen 443 ssl;
server_name baidu.com;
ssl_stapling off;
ssl_certificate /etc/nginx/ssl/aipuoucert.pem; # 证书自动安装的路径
ssl_certificate_key /etc/nginx/ssl/aipuoukey.pem; # 证书自动安装的路径
return 301 https://www.aipuou.com$request_uri; # 不带www请求重定向到www
}
server {
listen 443 quic reuseport; # HTTP3
listen [::]:443 quic reuseport; # IPV6
listen 443 ssl;
listen [::]:443 ssl;
http2 on;
server_name www.baidu.com;
# 缓存时这2个要放在location外面
root /usr/share/nginx/html/aipuou;
index index.html index.htm;
ssl_certificate /etc/nginx/ssl/aipuoucert.pem; # 证书自动安装的路径
ssl_certificate_key /etc/nginx/ssl/aipuoukey.pem; # 证书自动安装的路径
ssl_protocols TLSv1.2 TLSv1.3;
#add_header X-Server-Block "aipuou-www" always;
add_header Alt-Svc 'h3=":443"; ma=86400' always;
location ~* \.(avif|webp|png|jpg|jpeg|gif|svg|ico|css|js|woff2)$ {
try_files $uri =404;
#add_header X-Server-Block "aipuou-www-static" always;
#add_header X-Static-Cache "hit" always;
add_header Cache-Control "public, max-age=2592000, immutable" always;
add_header Alt-Svc 'h3=":443"; ma=86400' always;
access_log off;
}
location / {
#root /usr/share/nginx/html/aipuou;
#index index.html index.htm;
try_files $uri $uri/ /index.html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
3、测试(如果配置没问题,执行docker compose exec nginx nginx -t && docker compose exec nginx nginx -s reload也不生效,再重建容器测试:docker compose up -d --force-recreate nginx)
# 查看http3是否生效(出现alt-svc: h3=":443"; ma=86400即可)
# curl -I --http2 https://www.aipuou.com
HTTP/2 200
server: nginx/1.31.2
date: Tue, 07 Jul 2026 01:09:12 GMT
content-type: text/html
content-length: 22832
last-modified: Mon, 06 Jul 2026 13:42:48 GMT
vary: Accept-Encoding
etag: "6a4bb0d8-5930"
alt-svc: h3=":443"; ma=86400
accept-ranges: bytes
# 查看缓存是否生效(出现cache-control: max-age=2592000代表生效,x-server-block: aipuou-www-static和x-static-cache: hit仅用于测试时使用)
# curl -I https://www.aipuou.com/assets/hero-shenzhen-cooperation.webp
HTTP/2 200
server: nginx/1.31.2
date: Tue, 07 Jul 2026 02:55:42 GMT
content-type: image/webp
content-length: 98850
last-modified: Mon, 06 Jul 2026 10:30:45 GMT
etag: "6a4b83d5-18222"
expires: Thu, 06 Aug 2026 02:55:42 GMT
cache-control: max-age=2592000
x-server-block: aipuou-www-static
x-static-cache: hit
cache-control: public, max-age=2592000, immutable
alt-svc: h3=":443"; ma=86400
accept-ranges: bytes
4、结论:nginx1.31.2下测试开启http3后速度反而更加慢,实际采用http2+缓存效果更理想