从文件添加动态响应头 [英] Add dynamic response headers from a file

查看:98
本文介绍了从文件添加动态响应头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在nginx.conf中添加静态响应标头的方式:

location /some-path/ {
  add_header X-Some-Static-Header "some static value";
}

是否可以添加带有动态值的响应标头?此值应从文件,环境变量或类似的外部位置提取.

我正在尝试添加"X-App-Version"标头,该标头将从文件中读取.部署Web应用程序的新版本时,将使用新版本号更新此文件.最好,nginx应该立即开始提供新版本号,而无需重新启动/重新加载.

这怎么办?

解决方案

在更新版本号时,似乎没有一种方法可以简单地更改配置文件而无需这样做.就是说,如果您可以重新启动/重新加载,那么您所要求的自动化就不那么困难了.

如果您正在使用git(或实际上是任何VCS),则可以使用提交挂钩来触发简单的shell脚本,以查找并替换配置文件中的行,运行nginx -t -c /etc/nginx/nginx.conf,然后重新启动服务器. /p>

我希望有一个现成的NGINX模块可以完成您要执行的任务,所以我将其放在待办事项列表中,但是对于大多数用例来说,这应该是一个可以接受的技巧.

This is how I'm adding a static response header in my nginx.conf:

location /some-path/ {
  add_header X-Some-Static-Header "some static value";
}

Is there a way to add a response header with a dynamic value? This value should be pulled in from a file, or an environment variable, or some similar external place.

I'm trying to add a "X-App-Version" header, which is to be read from a file. When a new version of the web application is deployed, this file will be updated with the new version number. Preferably, nginx should immediately start serving up the new version number, without a restart/reload.

How can this be done?

解决方案

It doesn't look like there's a way to do this without simply changing the config file when you update the version number. That said, what you're asking for shouldn't be too difficult to automate if you can live with a restart/reload.

If you're using git, (or really any VCS,) you could use commit hooks to trigger a simple shell script to find and replace the line in the config file, run nginx -t -c /etc/nginx/nginx.conf, and restart the server.

I wish there was an existing NGINX module to do what you're asking, so I'm putting that on my todo list, but for most use cases this should probably be a reasonably acceptable hack.

这篇关于从文件添加动态响应头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆