nginx 配置 http3 (quic 协议基于UDP)

文档说明: 只记录关键的地方; 发文时间: 2023-11-19
意义: 使用 HTTP3
要求: nginx 版本大于等于 1.25

关键点

1、允许 443 端口接收 UDP 数据包

2、nginx config 配置选项位于 server 域

3、 在所有的 server 域中,只需要有一个 server 域中配置 reuseport 选项即可

4、打开浏览器控制台,刷新页面,在 调试面板 network 选项 ,protocl 栏 显示 h3 表示开启成功

5、响应头里包含: Alt-Svc: h3=":443"; ma=2592000,h3-29=":443"; ma=2592000

6、关键点如下配置:

7、nginx 版本大于等于 1.25


    listen  443       ssl;
    listen  443      quic reuseport;  
    listen  [::]:443  ssl;
    listen  [::]:443 quic reuseport;
    http2 on;

    add_header Alt-Svc 'h3=":443"; ma=2592000,h3-29=":443"; ma=2592000';

简易配置

version: "3"
services:
    web-server:
        image: nginx:1.25-alpine
        ports:
            - "80:80/tcp"
            - "443:443/tcp"
            - "443:443/udp"
        container_name: nginx-web
        restart: always
        volumes:
            - ./etc/conf.d:/etc/nginx/conf.d/
            - /data/tls:/tls  # https 证书
            - /data/:/data

nginx server 域完整配置

vi ./etc/conf.d/http-proxy.xiaoshuogeng.com.conf


server {
    listen       80;
    listen  [::]:80;
    server_name http-proxy.xiaoshuogeng.com
    ;
    rewrite ^(.*) https://$server_name$1 permanent;
}


server {
    listen  443       ssl;
    listen  443      quic reuseport;
    listen  [::]:443  ssl;
    listen  [::]:443 quic reuseport;
    http2 on;
    
    server_name http-proxy.xiaoshuogeng.com ;


    ssl_certificate     /tls/wildcard.xiaoshuogeng.com.fullchain.pem;
    ssl_certificate_key /tls/wildcard.xiaoshuogeng.com.key.pem;
    ssl_session_timeout 1d;
    ssl_session_cache shared:MozSSL:10m;  # about 40000 sessions
    ssl_session_tickets off;

    ssl_protocols  TLSv1.3;
    ssl_prefer_server_ciphers off;


    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Content-Type-Options "nosniff";

    add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;

    add_header Alt-Svc 'h3=":443"; ma=2592000,h3-29=":443"; ma=2592000';

    # 允许跨域
    add_header Access-Control-Allow-Origin  $http_origin always;
    add_header Access-Control-Allow-Methods 'GET,HEAD,POST,PUT,DELETE,CONNECT,OPTIONS,TRACE,PATCH' always;

    # 预检请求处理
    if ( $request_method = "OPTIONS" ) {
        return 204;
    }

    location / {
        root /data/web/dist/;
        index  index.html index.htm;
    }

 }


配置结果查看

  1. 检测http3 是否开启

参考文档

  1. Support for QUIC and HTTP/3
  2. nginx docs
  3. nginx news
  4. nginx http 通用配置
  5. nginx TLSv1.3配置
  6. nginx解决跨域方式--JSONP跨域 和 CORS跨域解决
  7. nginx端口复用
  8. nginx获得客户端IP地址和使用的端口
  9. nginx容器与php-fpm容器连接方式
  10. nginx 的http_proxy_connect_module模块使用
  11. 基于nginx 自建docker hub 容器缓存加速器,只加速官方镜像
  12. 基于nginx 自建拉取registry.k8s.io、k8s.gcr.io、gcr.io、quay.io、ghcr.io 容器镜像的服务
  13. 自建 docker hub 容器镜像缓存服务和加速服务
  14. 拷贝nginx容器镜像内的文件到容器外
  15. 使用acme.sh自助签发Let's Encrypt 的SSL证书
  16. 快速安装 docker-compose
  17. linux 快速安装docker
posted @ 2023-11-19 02:37  jingjingxyk  阅读(1398)  评论(2编辑  收藏  举报