在nginx重写中发送额外的标头 [英] Sending extra header in nginx rewrite
问题描述
现在,我正在使用以下nginx
配置将应用的域从app.example.com
迁移到app.newexample.com
:
Right now, I am migrating the domain of my app from app.example.com
to app.newexample.com
using the following nginx
config:
server {
server_name app.example.com;
location /app/ {
rewrite ^/app/(.*)$ http://app.newexample.com/$1;
}
}
我需要显示一个弹出横幅,以通知用户域名迁移.
我想基于referrer
或app.newexample.com
I need to show-up a popup-banner to notify the user of the domain name migration.
And I want to this based upon the referrer
or some-kind-of-other-header at app.newexample.com
但是我如何在上面的rewrite
上附加一个额外的标头,以便javascript仅在存在该标头时才会检测到该标头并显示横幅,因为直接进入app.newexample.com
的用户不应看到该弹出式窗口-横幅?
But how can I attach an extra header on the above rewrite
so that the javascript would detect that header and show the banner only when that header is present coz the user going directly at app.newexample.com
should not see that popup-banner?
推荐答案
问题是,当您重写"为具有协议和主机名的URI(在您的情况下为http://app.newexample.com/
)时,Nginx会发出合理的HTTP重定向(我猜代码将是301,也就是永久重定向".这仅使您有两种机制可以将任何信息传输到新URL的处理程序:
The thing is that, when you "rewrite" into URI having protocol and hostname (that is http://app.newexample.com/
in your case), Nginx issues fair HTTP redirect (I guess the code will be 301 aka "permanent redirect"). This leaves you only two mechanisms to transfer any information to the handler of new URL:
- cookie
- URL本身
由于您要将用户重定向到新域,因此Cookie不再可用.但是,即使是在公共域中,我也会选择URL来传输此类信息,例如
Since you are redirecting users to the new domain, cookie is no-go. But even in the case of a common domain I would choose URL to transfer this kind of information, like
server_name app.example.com;
location /app/ {
rewrite ^/app/(.*)$ http://app.newexample.com/$1?from_old=yes;
}
这使您可以在Nginx或浏览器(使用JavaScript)中进行处理.您甚至可以做您想做的事情,在新的应用服务器Nginx配置中为JavaScript发出一个特殊的HTTP标头:
This gives you the freedom to process at either Nginx or in a browser (using JavaScript). You may even do what you wanted intially, issuing a special HTTP header for JavaScript in new app server Nginx configuration:
server_name app.newexample.com;
location /app {
if ($arg_from_old) {
add_header X-From-Old-Site yes;
}
}
这篇关于在nginx重写中发送额外的标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!