Nginxreplace模块

首先,我要做的是:我有一个包含客户端远程地址variables的JavaScript文件。 默认情况下,variables设置为0.0.0.0 。 当Nginx正在发送文件时,我想用Nginx中的$ remote_addrvariablesreplace0。 所以基本上这是我得到的:

JavaScripttesting文件:

 remote_addr = '0.0.0.0'; alert(remote_addr); 

Nginx的configuration文件:

 location /path/to/my.js { alias /real/path/to/my.js sub_filter "0.0.0.0" $remote_addr; sub_filter_once off; sub_filter_types application/x-javascript; autoindex on; } 

当我尝试请求时,仍然在返回的文件中看到0.0.0.0。 我已经尝试使用简单的“Hello World”而不是$ remote_addr,仍然可以看到我的默认值。

nginx -V的输出如下:

 nginx version: nginx/1.2.7 built by gcc 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5) configure arguments: --prefix=/opt/nginx --add-module=/home/vagrant/nginx/echo-nginx-module-0.42 --with-http_sub_module --with-pcre=/home/vagrant/nginx/pcre-8.32 

更新

我仍然有问题,但已更新我的Nginxconfiguration。 我做了一个JS文件的curl,看到Nginx的内容types头设置为application/x-javascript所以我添加了sub_filter_types application/x-javascript

你确定你的“.js”文件有“text / html”MIMEtypes吗?

语法: sub_filter_types mime-type …;

默认: sub_filter_types text / html ;

上下文: http,服务器,位置

除了“text / html”之外,还允许使用指定的MIMEtypes进行stringreplace。 特殊值“*”匹配任何MIMEtypes(0.8.29)。

@ http://nginx.org/r/sub_filter_types