惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Security Latest
Security Latest
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Stack Overflow Blog
Stack Overflow Blog
WordPress大学
WordPress大学
N
Netflix TechBlog - Medium
GbyAI
GbyAI
云风的 BLOG
云风的 BLOG
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
宝玉的分享
宝玉的分享
博客园 - 【当耐特】
C
Cyber Attacks, Cyber Crime and Cyber Security
雷峰网
雷峰网
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
T
Threat Research - Cisco Blogs
NISL@THU
NISL@THU
Spread Privacy
Spread Privacy
P
Proofpoint News Feed
J
Java Code Geeks
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
MyScale Blog
MyScale Blog
T
Tor Project blog
P
Proofpoint News Feed
C
CERT Recently Published Vulnerability Notes
P
Privacy & Cybersecurity Law Blog
MongoDB | Blog
MongoDB | Blog
Simon Willison's Weblog
Simon Willison's Weblog
C
Cybersecurity and Infrastructure Security Agency CISA
L
LINUX DO - 热门话题
小众软件
小众软件
G
GRAHAM CLULEY
P
Privacy International News Feed
AWS News Blog
AWS News Blog
Know Your Adversary
Know Your Adversary
P
Palo Alto Networks Blog
人人都是产品经理
人人都是产品经理
S
Schneier on Security
Scott Helme
Scott Helme
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
B
Blog RSS Feed
T
The Exploit Database - CXSecurity.com
Recent Announcements
Recent Announcements
E
Exploit-DB.com RSS Feed
C
CXSECURITY Database RSS Feed - CXSecurity.com
U
Unit 42
The Register - Security
The Register - Security
S
Securelist
Martin Fowler
Martin Fowler
Project Zero
Project Zero
大猫的无限游戏
大猫的无限游戏
Cisco Talos Blog
Cisco Talos Blog

博客园 - DHclly

Aspose.Words 合并单元格的原理、配置方式、代码示例以及常见误区 Gpustack 运行一段时间后出现 Failed to initialize NVML: Unknown Error 解决办法 wps dispimg python 解析实现参考 NVIDIA GPU 计算能力( compute capability,SM version)兼容性查询 Amazon S3 Tools:S3cmd 介绍 wsl 和win主机互相访问 在 X86_64(amd64) 平台上的docker支持打包跨平台的镜像(如arm64) 以图搜图功能介绍 docker 容器调试技巧 open ai sdk 的额外请求头说明 x-stainless 大模型常见的概念 创建软连接的几种方式 基于node.js 的 web server 实现 对个人的警醒 jQuery对象与DOM对象之间的转换方法 【BUG】浏览器控制台提示:net::ERR_INVALID_CHUNKED_ENCODING 200 (OK) 的解决思路 实用浏览器脚本 关于Lambda表达式(箭头函数)的get属性访问器和常规的get属性访问器的差异 转换字符串为二进制编码字符串
nginx 根路径同时代理 http ws sse 三种请求
DHclly · 2024-10-28 · via 博客园 - DHclly

HTTP(HyperText Transfer Protocol):超文本传输协议,是用于在客户端(通常是web浏览器)和服务器之间传输数据的协议。HTTP是Web的基础,用于请求和传输网页、图像、视频等资源。它采用请求-响应模型,支持多种方法(如GET、POST等),并可通过HTTP/1.1和HTTP/2等版本进行优化,以提高性能和用户体验。

WebSocket:WebSocket是一种在单个TCP连接上进行全双工通信的协议。它允许客户端和服务器之间实现实时双向数据传输。WebSocket通过握手过程建立连接,并在一次握手后保持该连接,从而减少了HTTP请求的开销,适合实时应用如在线聊天、股票实时更新等。

SSE(Server-Sent Events):服务器推送事件是一种通过HTTP协议向浏览器推送实时事件的技术。与WebSocket不同,SSE是单向的,数据从服务器流向客户端。它使用 text/event-stream MIME类型,适用于需要实时更新但不需要客户端响应的场合,如实时新闻推送、股票价格更新等。SSE易于使用并支持自动重连。

利用 map 实现

map 定义的就是一个字典(k,v),通过不同key,映射不同value值。

http {
    map $http_content_type $connection_header {
        default "upgrade";
        "text/event-stream" "keep-alive";
    }

    upstream http_backend {
        server 127.0.0.1:80;
    }

    server {
        listen 9123;
        # server_name your_domain_name;

        location / {
            proxy_pass http://http_backend;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection $connection_header;
            proxy_cache off;
            proxy_buffering off;
        }
    }
}

使用 if 和变量实现

if 表达式里面不能直接包含指令 proxy_set_header Connection keep-alive,会报错,但是可以包含变量间接实现。

upstream http_backend {
    server 127.0.0.1:80;
}

server {
    listen 9123;
    # server_name your_domain_name;

    location / {
        set $connection_header "upgrade";

        if ($http_content_type ~* "text/event-stream") {
            set $connection_header "keep-alive";
        }

        proxy_pass http://http_backend;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_header;
        proxy_cache off;
        proxy_buffering off;
    }
}