Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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 0.0.0.0. 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 = '0.0.0.0';
alert(remote_addr);

Config File for Nginx:

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;
}

When I try the request, I still see 0.0.0.0 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 

Update

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
add comment

1 Answer

up vote 1 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).

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

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
add comment

Your Answer

 
discard

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.