# Storing $server\_addr in a variable VS using it directly in location block

**URL:** <https://www.ruby-forum.com/t/storing-server-addr-in-a-variable-vs-using-it-directly-in-location-block/242680>\
**Category:** NGINX\
**Created:** [July 15, 2015, 3:47am UTC](https://www.ruby-forum.com/t/storing-server-addr-in-a-variable-vs-using-it-directly-in-location-block/242680 "2015-07-15T03:47:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![musicdenotation](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ea666f/32.png) [@musicdenotation](https://www.ruby-forum.com/u/musicdenotation)\
**Post date:** [July 15, 2015, 3:47am UTC](https://www.ruby-forum.com/t/storing-server-addr-in-a-variable-vs-using-it-directly-in-location-block/242680/1 "2015-07-15T03:47:46Z")

</div>

According to the documentation getting the value of $server\_addr to set  
a  
response header makes a system call, and can impact performance  
negativelyset $ip $server\_addr;

```
server {
    location /health {
        add_header Backend $server_addr;
        return 200;
    }
}

```

Would the following be a better solution, and eliminate the system call  
on  
every request?

```
server {
    set $ip $server_addr;

    location /health {
        add_header Backend $ip always;
        return 200;
    }
}

```

Posted at Nginx Forum:

> **[Storing $server\_addr in a variable VS using it directly in location block](https://forum.nginx.org/read.php?2,260304,260304#msg-260304)**
>
> According to the documentation getting the value of $server\_addr to set a response header makes a system call, and can impact performance negativelyset $ip $server\_addr; server { location /health { add\_header Backend $server\_addr;

---

<div class="post-metadata">

**Author:** ![justink101](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a587f6/32.png) [@justink101](https://www.ruby-forum.com/u/justink101)\
**Post date:** [July 15, 2015, 1:08pm UTC](https://www.ruby-forum.com/t/storing-server-addr-in-a-variable-vs-using-it-directly-in-location-block/242680/2 "2015-07-15T13:08:34Z")

</div>

On Tuesday 14 July 2015 21:46:58 justink101 wrote:

> ```
> }
> 
> ```

No, it wouldn’t. The “set” directive also is executed on every request.

wbr, Valentin V. Bartenev

---

<div class="post-metadata">

**Author:** ![justink101](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a587f6/32.png) [@justink101](https://www.ruby-forum.com/u/justink101)\
**Post date:** [July 15, 2015, 1:45pm UTC](https://www.ruby-forum.com/t/storing-server-addr-in-a-variable-vs-using-it-directly-in-location-block/242680/3 "2015-07-15T13:45:57Z")

</div>

## Valentin V. Bartenev Wrote:

> On Tuesday 14 July 2015 21:46:58 justink101 wrote:
> 
> > According to the documentation getting the value of $server\_addr to  
> > set a  
> > response header makes a system call, and can impact performance  
> > negativelyset $ip $server\_addr;

> No, it wouldn’t. The “set” directive also is executed on every  
> request.

Via map as well ? (geo would suffer as well then)

Posted at Nginx Forum:  
[http://forum.nginx.org/read.php?2,260304,260320#msg-260320](http://forum.nginx.org/read.php?2,260304,260320#msg-260320)

---

<div class="post-metadata">

**Author:** ![justink101](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a587f6/32.png) [@justink101](https://www.ruby-forum.com/u/justink101)\
**Post date:** [July 15, 2015, 2:27pm UTC](https://www.ruby-forum.com/t/storing-server-addr-in-a-variable-vs-using-it-directly-in-location-block/242680/4 "2015-07-15T14:27:25Z")

</div>

On Wednesday 15 July 2015 07:45:34 itpp2012 wrote:

> Via map as well ? (geo would suffer as well then)

All variables work on a per requests basis, no exceptions.

wbr, Valentin V. Bartenev
