Redirection in Nginx

Rob Wilkerson asked:

I’m setting up a simple virtual host to emulate a few specific responses that, in production, will come from a single URL. By way of an example, the production URL might look like this:

It actually returns text in .ini format. I’ve set up several test files in my webroot at /info/(legacy|new)/(daily|monthly)/device.htm that will test several different responses and I’d like to set up redirection so that I can redirect the same-ish url to the appropriate resource. The test URL might look something like this:

What I’d like to do is extract the appropriate values and serve up (in this example)


What Chrome, at least, is doing instead is attempting to download something called info.bin.

Here’s my location block:

location ~ ^/(?<device>[^/]+)/(?<software>[^/]+)/(?<plan>[^/]+)/getdeviceinfo/info.bin$ {
  alias /opt/dev/hughesnet-modem-simulator/info/$software/$plan;
  try_files $uri $uri $device.htm

I’ve also tried setting the alias value to /opt/dev/hughesnet-modem-simulator/info/$software/$device.htm in lieu of try_files. I know I’ve just borked up the syntax, but I’m not sure where I’ve gone wrong.

Any insight would be much appreciated.



My current location block:

location ~ ^/(?<device>[^/]+)/(?<software>[^/]+)/(?<plan>[^/]+)/getdeviceinfo/info.bin$ {
  types {}
  default_type text/plain;
  alias /opt/dev/project-root/info/$software/$plan/$device.htm;

My answer:

.bin is mapped by default to the MIME type application/octet-stream, which is why the browser tries to download it.

To fix this, override the MIME type mapping within your location block:

location /.... {
    types { }
    default_type text/plain;
    # the rest of your stuff

View the full question and answer on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.