Nginx – How to conditionally add the Access-Control-Allow-Origin header for fonts served through an X-Accel-Redirect in nginx

configurationnginx

I am trying to serve some webfonts through a PHP script that tells nginx to do an internal redirect using X-Accel-Redirect.

As the script serves many types of files, I only wish to add the Access-Control-Allow-Origin header for fonts.

This is a configuration I have tried that works:

location /deploys/ {
  internal;
  alias   /www/deploys/;
  expires 1y;

  add_header Access-Control-Allow-Origin *;
}

The problem with this approach is that ALL files now contain the Access-Control-Allow-Origin header. I only want my fonts to have that header.

So, I tried a conditional:

location /deploys/ {
  internal;
  alias   /www/deploys/;
  expires 1y;

  if ($request_filename ~* ^.?/([^/]?)$)
  {
     set $filename $1; 
  }

  if ($filename ~* ^.*?\.(eot)|(ttf)|(woff)$){
     add_header Access-Control-Allow-Origin *;
  }
}

Unfortunately, this does not work (and I suspect it would be due to the internal redirect using `X-Accel-Redirect).

How does one go about appending the Access-Control-Allow-Oirgin header for some extensions using X-Accel-Redirect?

Best Answer

It looks like alias will change the request file. I did not have time to compile and install the echo module to verify, but using the following config fixed the problem:

location /deploys/ {
  internal;

  if ($request_filename ~* ^.?/([^/]?)$)
  {
     set $filename $1; 
  }

  if ($filename ~* ^.*?\.(eot)|(ttf)|(woff)$){
     add_header Access-Control-Allow-Origin *;
  }

  alias   /www/deploys/;
  expires 1y;
}