Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending extra header in nginx rewrite

Tags:

nginx

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;
    }
}

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

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?

like image 655
Autodidact Avatar asked May 12 '13 08:05

Autodidact


2 Answers

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 itself

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;
}

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;
  }
}
like image 86
Alexander Azarov Avatar answered Jan 03 '23 06:01

Alexander Azarov


A similar problem was discussed here. You can try to use a third-party module HttpHeadersMore (I didn't try it myself). But even if it does not work at all, with the help of this module you can do absolutely everything. Example is here.

like image 24
Alex Butenko Avatar answered Jan 03 '23 06:01

Alex Butenko