# Check if variable exists in file, Using file contents for variable handling

**URL:** <https://www.ruby-forum.com/t/check-if-variable-exists-in-file-using-file-contents-for-variable-handling/246114>\
**Category:** NGINX\
**Created:** [August 29, 2013, 4:43pm UTC](https://www.ruby-forum.com/t/check-if-variable-exists-in-file-using-file-contents-for-variable-handling/246114 "2013-08-29T16:43:07Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rafa\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/958977/32.png) [@Rafa\_F](https://www.ruby-forum.com/u/Rafa_F)\
**Post date:** [August 29, 2013, 4:43pm UTC](https://www.ruby-forum.com/t/check-if-variable-exists-in-file-using-file-contents-for-variable-handling/246114/1 "2013-08-29T16:43:07Z")

</div>

Is there a way to check a variable against file contents for processing?  
A  
couple scenarios below.

This is used here, but adding multiple agents can get burdensome:  
if ($http\_user\_agent ~ (agent1|agent2|Foo|Wget|Nmap|BadAgent) ) {  
return 403;  
}

I’d like to maintain a file with all the variables, (and custom script  
the  
addition/removal of file entries) like this:  
if ($http\_user\_agent ~ (in.file(…/…/badAgents.txt) ) {  
return 403;  
}

Or using file references for Allow/Deny:  
Allow …/…/whitelist.txt  
Deny …/…/badHosts.txt

Or Checking usernames against a whitelist/blacklist:  
if ( $arg.Username does.not.exist.in(…/…/allowedUsers.txt) )  
if ( $arg.Username exists.in(…/…/blockedUsers.txt) )

Posted at Nginx Forum:

> **[Check if variable exists in file, Using file contents for variable handling](https://forum.nginx.org/read.php?2,242333,242333#msg-242333)**
>
> Is there a way to check a variable against file contents for processing? A couple scenarios below. This is used here, but adding multiple agents can get burdensome: if ($http\_user\_agent ~ (agent1|agent2|Foo|Wget|Nmap|BadAgent) ) { return 403; }

---

<div class="post-metadata">

**Author:** ![spacecwoboy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/db5fbb/32.png) [@spacecwoboy](https://www.ruby-forum.com/u/spacecwoboy)\
**Post date:** [August 29, 2013, 7:42pm UTC](https://www.ruby-forum.com/t/check-if-variable-exists-in-file-using-file-contents-for-variable-handling/246114/2 "2013-08-29T19:42:38Z")

</div>

Hello!

On Thu, Aug 29, 2013 at 10:42:37AM -0400, spacecwoboy wrote:

> if ($http\_user\_agent ~ (in.file(../../badAgents.txt) ) {  
> if ( $arg.Username does.not.exist.in(../../allowedUsers.txt) )  
> if ( $arg.Username exists.in(../../blockedUsers.txt) )

The map module is probably what you are looking for, see here:

> **[Module ngx\_http\_map\_module](http://nginx.org/en/docs/http/ngx_http_map_module.html)**

Additionally, the “include” directive may be usefull, see  
[Core functionality](http://nginx.org/r/include).

–  
Maxim D.

> **[nginx: donation](http://nginx.org/en/donation.html)**
