AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 819722
Accepted
vgrinko
vgrinko
Asked: 2016-12-09 22:55:23 +0800 CST2016-12-09 22:55:23 +0800 CST 2016-12-09 22:55:23 +0800 CST

如何在 Nginx 中为 400 和 500 proxy_pass 后端响应设置自定义标头?

  • 772

我需要根据 proxy_pass 后端响应代码的响应设置不同的自定义响应标头值。

我尝试了许多不同的方法,但仍然无法弄清楚如何做到这一点。

location /mypath {
    #for 200,301,302,etc "good" responses from 127.0.0.1:8080 I need to set value 60
    add_header X-MyCustomHeader 60;

    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-Host $host;
    proxy_set_header X-Forwarded-Server $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_pass http://127.0.0.1:8080;

    #for 404,403 responses from 127.0.0.1:8080 I need to set X-MyCustomHeader=5
    #for 500 responses from 127.0.0.1:8080 I need to set X-MyCustomHeader=1
}

任何帮助表示赞赏。

http nginx reverse-proxy proxypass backend
  • 1 1 个回答
  • 3414 Views

1 个回答

  • Voted
  1. Best Answer
    vgrinko
    2016-12-13T18:37:52+08:002016-12-13T18:37:52+08:00

    我想出了以下解决方案。它解决了以下问题:

    1. 根据来自 proxy_pass 后端的响应代码设置 HTTP 响应标头
    2. 允许从后端服务器 404 页面内容,但仍添加标题。

    已知问题:

    1. 从技术上讲,如果它是 404,它将调用后端两次。对我来说这不是问题,因为我正在缓存 404 响应和一些小 ttl。但对其他人来说可能是。

    我的来源:

    location /mypath/ {
        add_header X-MyCustomHeader 60;
    
        include /etc/nginx/proxy_config.conf; #some proxy_pass headers
        proxy_pass http://127.0.0.1:8080;
        proxy_intercept_errors on;
        error_page 500 502 503 504 /50x.html;
        error_page 400 403 404 =404 /mypath/errors/404.html;
    }
    
    # custom 50x fallback page for /mypath/, server from the nginx itself
    location /50x.html {
        add_header X-MyCustomHeader 1;
    
        root html;
    }
    
    # custom 40x fallback page for /mypath/, served from the backend - hack
    location /mypath/errors/404.html  {
        add_header X-MyCustomHeader 5;
    
        include /etc/nginx/proxy_config.conf; #some proxy_pass headers
        proxy_pass http://127.0.0.1:8080;
        proxy_intercept_errors off;
    }
    

    将此问题视为参考

    而这个

    这种方法有什么问题/担忧吗?

    • 2

相关问题

  • 设置 http 代理以使用 Web 界面?

  • 无法连接到 Ubuntu Desktop 中的端口 80 或 VMWare Workstation 6.52 中的 Server 9.04

  • IIS 7.5 (Windows 7) - HTTP 错误 401.3 - 未经授权

  • 为什么有些网站的网址中没有“www”就无法显示?[关闭]

  • Tomcat 6 HTTP 日志滚动和清除

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve