Answers: <p>The <code>SERVER_ADDR</code> is a FastCGI variable that is being set inside the <code>fastcgi_params</code> file as</p>
  <pre class="lang-none prettyprint-override"><code>fastcgi_param  SERVER_ADDR        $server_addr;
  </code></pre>
  <p>The <a href="https://nginx.org/en/docs/http/ngx_http_core_module.html#var_server_addr" rel="nofollow noreferrer"><code>$server_addr</code></a> nginx variable description says it contains an address of the server which accepted a request, which is obviously an nginx server address. If you want to determine an address of upstream server that is chosen to handle the request, you can try to pass an additional FastCGI variable to your PHP-FPM backend using the <a href="https://nginx.org/en/docs/http/ngx_http_upstream_module.html#var_upstream_addr" rel="nofollow noreferrer"><code>$upstream_addr</code></a> nginx internal variable value, e.g.</p>
  <pre class="lang-none prettyprint-override"><code>fastcgi_param  UPSTREAM_ADDR  $upstream_addr;
  </code></pre>
  <p>and check its value via the <code>$_SERVER['UPSTREAM_ADDR']</code> array item value (I didn't check if this would work, so if you'd check it, any positive or negative feedback is welcome).</p>
  <p>Alternatively you can check <a href="https://stackoverflow.com/a/17053582/7121513">this</a> StackOverflow answer.</p>
  
Answers: CC BY-SA 4.0
Answers: 2022-06-16T17:13:46.150Z
Answers: 2022-06-16T17:13:46.150Z
Answers: Ivan Shatsky
Body: <p>I am trying to set up an Nginx server for the purpose of load balancing between 4 Php-fpm servers. You can access my docker configuration files from <a href="https://github.com/sajjadhesami/sampleDockerNginxLoadBalancer" rel="nofollow noreferrer">this Github repository</a>.</p>
  <p>My containers' network configurations are as below:</p>
  <pre><code>&quot;Containers&quot;: {
              &quot;17e57fd1126d99bcd0cba4dde930181b0b240cea54e6d7f9b31bce3dbda416a6&quot;: {
                  &quot;Name&quot;: &quot;my_app_container_3&quot;,
                  &quot;EndpointID&quot;: &quot;d71c4f7965c51f1202c518172f8ef04b8d6304200000c61b8651b8d72b7a78ed&quot;,
                  &quot;MacAddress&quot;: &quot;02:42:ac:12:00:04&quot;,
                  &quot;IPv4Address&quot;: &quot;172.18.0.4/16&quot;,
                  &quot;IPv6Address&quot;: &quot;&quot;
              },
              &quot;256e89ae5bcd4acaed1c621075fa4541af781731300dfc742b1a5f9a719dd87a&quot;: {
                  &quot;Name&quot;: &quot;my_app_container_2&quot;,
                  &quot;EndpointID&quot;: &quot;1491bec10a1f56a146ddb78da186c59bebda7d8bcd2a580414827987ce3d1964&quot;,
                  &quot;MacAddress&quot;: &quot;02:42:ac:12:00:03&quot;,
                  &quot;IPv4Address&quot;: &quot;172.18.0.3/16&quot;,
                  &quot;IPv6Address&quot;: &quot;&quot;
              },
              &quot;814a7eb51e9a8ed7b2231dc6e33cb23f497224228d3cb9abfa36ae1eb8766ae7&quot;: {
                  &quot;Name&quot;: &quot;phpmyadmin_container&quot;,
                  &quot;EndpointID&quot;: &quot;c6db79bd7775e1fc684ffc083d92ffe0d503695333dff5cd265af567a4ce625f&quot;,
                  &quot;MacAddress&quot;: &quot;02:42:ac:12:00:07&quot;,
                  &quot;IPv4Address&quot;: &quot;172.18.0.7/16&quot;,
                  &quot;IPv6Address&quot;: &quot;&quot;
              },
              &quot;83daac9add189960af858bf7a16fc2b6b1592268f10da37ae87babe67c36b9d5&quot;: {
                  &quot;Name&quot;: &quot;container_mysql&quot;,
                  &quot;EndpointID&quot;: &quot;6bb1f812020a445d3210611d66febccb45b49b887e8c1f176310fe7ef01c3d18&quot;,
                  &quot;MacAddress&quot;: &quot;02:42:ac:12:00:02&quot;,
                  &quot;IPv4Address&quot;: &quot;172.18.0.2/16&quot;,
                  &quot;IPv6Address&quot;: &quot;&quot;
              },
              &quot;ac0f65cc4948578a7a056d2a27e4771bccb94ce08da87f262edba423a1d3caa0&quot;: {
                  &quot;Name&quot;: &quot;nginx_container&quot;,
                  &quot;EndpointID&quot;: &quot;4fb3adf6ad5a1f6ad0ee8af816386fe731087230a651140f6b714071e3df74ee&quot;,
                  &quot;MacAddress&quot;: &quot;02:42:ac:12:00:08&quot;,
                  &quot;IPv4Address&quot;: &quot;172.18.0.8/16&quot;,
                  &quot;IPv6Address&quot;: &quot;&quot;
              },
              &quot;cb17b123ff02035f23fcf44895db0dfea8d1337bf3278a71392abd153c0a07a1&quot;: {
                  &quot;Name&quot;: &quot;my_app_container_4&quot;,
                  &quot;EndpointID&quot;: &quot;e03291091e859e08c7c5a7c78ae188e0a3cac117abe8cf351f81fbe8d63d12e0&quot;,
                  &quot;MacAddress&quot;: &quot;02:42:ac:12:00:06&quot;,
                  &quot;IPv4Address&quot;: &quot;172.18.0.6/16&quot;,
                  &quot;IPv6Address&quot;: &quot;&quot;
              },
              &quot;f7fcf98941d12cd5d7fda36f77413d8278e129d5769daa1e6cdc400f342a44b1&quot;: {
                  &quot;Name&quot;: &quot;my_app_container_1&quot;,
                  &quot;EndpointID&quot;: &quot;bc82184499842d089473049dcd0e22bdb8b777bc38e11072f58efdff73407946&quot;,
                  &quot;MacAddress&quot;: &quot;02:42:ac:12:00:05&quot;,
                  &quot;IPv4Address&quot;: &quot;172.18.0.5/16&quot;,
                  &quot;IPv6Address&quot;: &quot;&quot;
              }
          },
  </code></pre>
  <p>In my Php file, I have a simple line of code to show the IP address of the server:</p>
  <pre><code>&lt;h1&gt;This request is handeled by the server working on &lt;?php echo $_SERVER['SERVER_ADDR'] ?&gt;&lt;/h1&gt;
  </code></pre>
  <p>Now that my PHP interpreters are running on different containers, I expect to see different IP addresses whenever I refresh the page. Interestingly though, it always prints out the IP of the Nginx server despite the fact that the code is being interpretted in another container.</p>
  <p>Could you explain to me why it is so?</p>
  
Tags: <docker><networking><nginx><php><ip>
Title: Why does php-fpm show nginx's IP while they are on different containers?
UserDisplayName: NewUser
ThreadText: # Why does php-fpm show nginx's IP while they are on different containers?
  
  (Asked by: username_0 on 2022-06-14)
  
  I am trying to set up an Nginx server for the purpose of load balancing between 4 Php-fpm servers. You can access my docker configuration files from [this Github repository](https://github.com/sajjadhesami/sampleDockerNginxLoadBalancer).
  
  My containers' network configurations are as below:
  
  ```
  "Containers": {
              "17e57fd1126d99bcd0cba4dde930181b0b240cea54e6d7f9b31bce3dbda416a6": {
                  "Name": "my_app_container_3",
                  "EndpointID": "d71c4f7965c51f1202c518172f8ef04b8d6304200000c61b8651b8d72b7a78ed",
                  "MacAddress": "02:42:ac:12:00:04",
                  "IPv4Address": "172.18.0.4/16",
                  "IPv6Address": ""
              },
              "256e89ae5bcd4acaed1c621075fa4541af781731300dfc742b1a5f9a719dd87a": {
                  "Name": "my_app_container_2",
                  "EndpointID": "1491bec10a1f56a146ddb78da186c59bebda7d8bcd2a580414827987ce3d1964",
                  "MacAddress": "02:42:ac:12:00:03",
                  "IPv4Address": "172.18.0.3/16",
                  "IPv6Address": ""
              },
              "814a7eb51e9a8ed7b2231dc6e33cb23f497224228d3cb9abfa36ae1eb8766ae7": {
                  "Name": "phpmyadmin_container",
                  "EndpointID": "c6db79bd7775e1fc684ffc083d92ffe0d503695333dff5cd265af567a4ce625f",
                  "MacAddress": "02:42:ac:12:00:07",
                  "IPv4Address": "172.18.0.7/16",
                  "IPv6Address": ""
              },
              "83daac9add189960af858bf7a16fc2b6b1592268f10da37ae87babe67c36b9d5": {
                  "Name": "container_mysql",
                  "EndpointID": "6bb1f812020a445d3210611d66febccb45b49b887e8c1f176310fe7ef01c3d18",
                  "MacAddress": "02:42:ac:12:00:02",
                  "IPv4Address": "172.18.0.2/16",
                  "IPv6Address": ""
              },
              "ac0f65cc4948578a7a056d2a27e4771bccb94ce08da87f262edba423a1d3caa0": {
                  "Name": "nginx_container",
                  "EndpointID": "4fb3adf6ad5a1f6ad0ee8af816386fe731087230a651140f6b714071e3df74ee",
                  "MacAddress": "02:42:ac:12:00:08",
                  "IPv4Address": "172.18.0.8/16",
                  "IPv6Address": ""
              },
              "cb17b123ff02035f23fcf44895db0dfea8d1337bf3278a71392abd153c0a07a1": {
                  "Name": "my_app_container_4",
                  "EndpointID": "e03291091e859e08c7c5a7c78ae188e0a3cac117abe8cf351f81fbe8d63d12e0",
                  "MacAddress": "02:42:ac:12:00:06",
                  "IPv4Address": "172.18.0.6/16",
                  "IPv6Address": ""
              },
              "f7fcf98941d12cd5d7fda36f77413d8278e129d5769daa1e6cdc400f342a44b1": {
                  "Name": "my_app_container_1",
                  "EndpointID": "bc82184499842d089473049dcd0e22bdb8b777bc38e11072f58efdff73407946",
                  "MacAddress": "02:42:ac:12:00:05",
                  "IPv4Address": "172.18.0.5/16",
                  "IPv6Address": ""
              }
          },
  
  ```
  
  In my Php file, I have a simple line of code to show the IP address of the server:
  
  ```
  <h1>This request is handeled by the server working on <?php echo $_SERVER['SERVER_ADDR'] ?></h1>
  
  ```
  
  Now that my PHP interpreters are running on different containers, I expect to see different IP addresses whenever I refresh the page. Interestingly though, it always prints out the IP of the Nginx server despite the fact that the code is being interpretted in another container.
  
  Could you explain to me why it is so?
  
  **Tags:** docker, networking, nginx, php, ip
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2022-06-16. Score: 2 | Accepted answer
  
  The `SERVER_ADDR` is a FastCGI variable that is being set inside the `fastcgi_params` file as
  
  ```none
  fastcgi_param  SERVER_ADDR        $server_addr;
  
  ```
  
  The [`$server_addr`](https://nginx.org/en/docs/http/ngx_http_core_module.html#var_server_addr) nginx variable description says it contains an address of the server which accepted a request, which is obviously an nginx server address. If you want to determine an address of upstream server that is chosen to handle the request, you can try to pass an additional FastCGI variable to your PHP-FPM backend using the [`$upstream_addr`](https://nginx.org/en/docs/http/ngx_http_upstream_module.html#var_upstream_addr) nginx internal variable value, e.g.
  
  ```none
  fastcgi_param  UPSTREAM_ADDR  $upstream_addr;
  
  ```
  
  and check its value via the `$_SERVER['UPSTREAM_ADDR']` array item value (I didn't check if this would work, so if you'd check it, any positive or negative feedback is welcome).
  
  Alternatively you can check [this](https://stackoverflow.com/a/17053582/7121513) StackOverflow answer.
  
  
  ---
  
  