Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

To start, here's what I am trying to do: I have a JavaScript file that contains a variable for the remote address of the client. By default the variable is set to As Nginx is sending the file I would like to replace the 0's with the $remote_addr variable in Nginx. So basically here's what I've got:

JavaScript Test File:

remote_addr = '';

Config File for Nginx:

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

  autoindex on;

When I try the request, I still see in the returned file. I've tried also using simply "Hello World" instead of $remote_addr and still see my default value.

The output of nginx -V is as follows:

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 


I am still having the issue, but have updated my Nginx config. I did a curl of the JS file and see that Nginx is setting the Content-Type header to application/x-javascript so I added sub_filter_types application/x-javascript

share|improve this question
up vote 2 down vote accepted

Are you sure that your ".js" file has "text/html" mime-type?

syntax: sub_filter_types mime-type ...;

default: sub_filter_types text/html;

context: http, server, location

Enables string replacement in responses with the specified MIME types in addition to “text/html”. The special value “*” matches any MIME type (0.8.29).


share|improve this answer
Sorry, I forgot to update my question that I added in sub_filter_types of application/javascript and application/x-javascript. – Dave Long Feb 13 '13 at 13:39
Turns out I was spelling application wrong in my mime type. – Dave Long Feb 13 '13 at 13:42

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.