在Nginx中redirect

我正在设置一个简单的虚拟主机来模拟一些特定的响应,在生产中,它将来自一个URL。 举例来说,生产URL可能如下所示:

http://domain.com/getdeviceinfo/info.bin 

它实际上以.ini格式返回文本。 我已经在/info/(legacy|new)/(daily|monthly)/device.htm webroot中设置了几个testing文件,它将testing几个不同的响应,我想设置redirect,这样我就可以redirect同样的url到相应的资源。 testingurl可能如下所示:

 http://devdomain.com/devicename/legacy/monthly/getdeviceinfo/info.bin 

我想要做的是提取适当的值,并提供(在这个例子中)

 /info/legacy/monthly/devicename.htm 

至less,Chrome正在尝试下载一些名为info.bin东西。

这是我的location块:

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

我也尝试将alias值设置为/opt/dev/hughesnet-modem-simulator/info/$software/$device.htm而不是try_files 。 我知道我只是哼了一下这个语法,但是我不确定自己出错的地方。

任何有识之士将不胜感激。

谢谢。

UPDATE

我目前的location块:

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

.bin被默认映射到MIMEtypes的application/octet-stream ,这就是浏览器试图下载它的原因。

要解决此问题,请覆盖您的location块中的MIMEtypes映射:

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

用别名与variables与try_files混合可能是你的问题的原因。 而不是使用别名,只需使用root:

 # Including the /s in the variables makes the try_files a little more efficient location ~ ^(?<device>/[^/]+)(?<software>/[^/]+)(?<plan>/[^/]+)/getdeviceinfo/info.bin$ { # Set the root to the base of all the files root /opt/dev/hughesnet-modem-simulator/info; # construct the file path to append to the root try_files $software$plan$device.htm =404; }