# Setting variable and using HttpSubModule

**URL:** <https://www.ruby-forum.com/t/setting-variable-and-using-httpsubmodule/208730>\
**Category:** NGINX\
**Created:** [June 25, 2011, 6:39pm UTC](https://www.ruby-forum.com/t/setting-variable-and-using-httpsubmodule/208730 "2011-06-25T18:39:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [June 25, 2011, 6:39pm UTC](https://www.ruby-forum.com/t/setting-variable-and-using-httpsubmodule/208730/1 "2011-06-25T18:39:45Z")

</div>

I’m trying to insert google analytics code into an app by using the  
directions at [Module ngx\_http\_sub\_module](http://wiki.nginx.org/HttpSubModule).

In so doing, I first tried to set a variable as follows:

set $analytics = “var \_gaq = \_gaq || []; \_gaq.push([‘\_setAccount’,  
‘UA-xxxxxx-xx’]); \_gaq.push([‘\_trackPageview’]); (function() { var  
ga = document.createElement(‘script’); ga.type = ‘text/javascript’;  
ga.async = true; ga.src = (‘https:’ == document.location.protocol ?  
‘[https://ssl](https://ssl)’ : ‘[http://www](http://www)’) + ‘.google-analytics.com/ga.js’; var s  
= document.getElementsByTagName(‘script’)[0];  
s.parentNode.insertBefore(ga, s); })();”;

to get the text into the variable but of course that generates an error:

nginx: [emerg] invalid number of arguments in “set” directive in  
/usr/local/etc/nginx/sites-enabled/mysite.com:12.

Searching Google I found a few threads about trying to place the  
contents of a file into a variable but there didn’t seem to be a way.

I also tried simply adding the text into the “sub\_filter” line but that  
also threw an error:

nginx: [emerg] unexpected “\_” in  
/usr/local/etc/nginx/sites-enabled/mysite.com:16

Using the following:

server {  
…

sub\_filter   
‘’;  
sub\_filter\_once on;

…  
}

resulted in the following:

jim$ curl [http://mysite.com/somepage](http://mysite.com/somepage)

…

...

Putting the path in quotes changed nothing other than that portion being  
in quotes.

## Obviously I’m doing something wrong. Any help would be appreciated.

Jim O.

---

<div class="post-metadata">

**Author:** ![joviyach](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7bcc69/32.png) [@joviyach](https://www.ruby-forum.com/u/joviyach)\
**Post date:** [June 25, 2011, 10:00pm UTC](https://www.ruby-forum.com/t/setting-variable-and-using-httpsubmodule/208730/2 "2011-06-25T22:00:04Z")

</div>

Hello!

On Sat, Jun 25, 2011 at 12:39:19PM -0400, Jim O. wrote:

> = document.getElementsByTagName(‘script’)[0];  
> s.parentNode.insertBefore(ga, s); })();";
> 
> to get the text into the variable but of course that generates an error:
> 
> nginx: [emerg] invalid number of arguments in “set” directive in  
> /usr/local/etc/nginx/sites-enabled/mysite.com:12.

Syntax is

```
set $variable "value";

```

Using “=” is a syntax error.

> **[GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki...](https://github.com/nginxinc/nginx-wiki)**
>
> ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com

> Searching Google I found a few threads about trying to place the  
> contents of a file into a variable but there didn’t seem to be a way.
> 
> I also tried simply adding the text into the “sub\_filter” line but that  
> also threw an error:
> 
> nginx: [emerg] unexpected “\_” in  
> /usr/local/etc/nginx/sites-enabled/mysite.com:16

Probably some other syntax error. Please make sure you’ve quoted  
text properly.

Somthing like this works fine here:

sub\_filter “”  
"";

> …  
> Putting the path in quotes changed nothing other than that portion being  
> in quotes.

There result is exactly as configured, it’s not clear what you expect  
here.

Maxim D.

---

<div class="post-metadata">

**Author:** ![joviyach](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7bcc69/32.png) [@joviyach](https://www.ruby-forum.com/u/joviyach)\
**Post date:** [June 25, 2011, 11:06pm UTC](https://www.ruby-forum.com/t/setting-variable-and-using-httpsubmodule/208730/3 "2011-06-25T23:06:46Z")

</div>

On 6/25/11 3:59 PM, Maxim D. wrote:

> > ‘UA-xxxxxx-xx’]); \_gaq.push([‘\_trackPageview’]); (function() { var
> 
> Syntax is
> 
> ```
> set $variable "value";
> 
> ```
> 
> Using “=” is a syntax error.
> 
> [GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com · GitHub](http://wiki.nginx.org/HttpRewriteModule#set)

Thanks Maxim. My goof. That solved it.

> > Searching Google I found a few threads about trying to place the  
> > contents of a file into a variable but there didn’t seem to be a way.

[…]

> > …  
> > Putting the path in quotes changed nothing other than that portion being  
> > in quotes.
> 
> There result is exactly as configured, it’s not clear what you expect here.

That’s what I found out. 😛 I did some digging before I asked for help  
here and I found  
[http://www.lowendtalk.com/questions/7846/automatically-reference-ga-codes-from-external-file-using-nginx-substitution-module](http://www.lowendtalk.com/questions/7846/automatically-reference-ga-codes-from-external-file-using-nginx-substitution-module)  
which had that configuration and was obviously _wrong_. However that is  
where I got the idea to at try it.

Again, thanks for the help.

–  
Jim O.

---

<div class="post-metadata">

**Author:** ![joviyach](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7bcc69/32.png) [@joviyach](https://www.ruby-forum.com/u/joviyach)\
**Post date:** [June 26, 2011, 12:12am UTC](https://www.ruby-forum.com/t/setting-variable-and-using-httpsubmodule/208730/4 "2011-06-26T00:12:38Z")

</div>

Hello!

On Sat, Jun 25, 2011 at 05:06:23PM -0400, Jim O. wrote:

[…]

> > > }  
> > > in quotes.
> > 
> > There result is exactly as configured, it’s not clear what you expect here.
> 
> That’s what I found out. 😛 I did some digging before I asked for help  
> here and I found

[http://www.lowendtalk.com/questions/7846/automatically-reference-ga-codes-from-external-file-using-nginx-substitution-module](http://www.lowendtalk.com/questions/7846/automatically-reference-ga-codes-from-external-file-using-nginx-substitution-module)

> which had that configuration and was obviously _wrong_. However that is  
> where I got the idea to at try it.

It’s not “wrong”, it just references GA code in an external  
javascript file which will be loaded by browser as a separate  
resource. There is some downsides of this aproach (browser will  
have to load another javascript resource), but it’s not wrong.

Maxim D.
