8

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?

millisami
  • 9,931
  • 15
  • 70
  • 112

4 Answers4

10

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;
  }
}
Alexander Azarov
  • 12,971
  • 2
  • 50
  • 54
  • This new header, can it somehow be accessed by javascript without using an XHR request? – Chris May 22 '13 at 10:10
  • 1
    I don't know, I'm not an expert in JavaScript (OP wanted this approach). IMHO it's more trivial for JS code to simply check query string of the document's location. – Alexander Azarov May 22 '13 at 10:49
1

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.

Community
  • 1
  • 1
Alex Butenko
  • 3,664
  • 3
  • 35
  • 54
  • Well, I looked at http://serverfault.com/questions/459888/nginx-specify-custom-headers-in-rewritten-location-blocks But it has two different things, 1. Focused on Single domain 2. Other is that its not dealing with custom or referrer headers. Ain't it? – millisami May 15 '13 at 11:24
1

Your redirect is missing one thing, the redirect type/code, you should add permanent at the end of your rewrite line, I'm not sure what's the default redirect code if not explicitly mentioned.

rewrite ^/app/(.*)$ http://app.newexample.com/$1 permanent;

An even better way is using return

location /app {
    return 301 $scheme://app.newexample.com$request_uri;
}

Adding a get parameter as mentioned above would also be a reliable way to do it, you can easily set a session ( flash ) and redirect again to the page it self but after removing the appended get parameter.

EDIT: Redirecting doesn't send referrer header, if the old domain is still working you could put a simple php file that does the redirect with a header call.

header("Location: http://app.newexample.com")
Mohammad AbuShady
  • 40,884
  • 11
  • 78
  • 89
1

One possible solution without any headers would be to check the document.referrer property:

if (document.referrer.indexOf("http://app.example.com") === 0) {
   alert("We moved!");
}

Using a 301 will set the referrer to the old page. If the referrer doesn't start with the old page url, it was not directed by that page. Maybe a bit quick n dirty, but should work.

Chris
  • 486
  • 2
  • 11