1

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.

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?

1 Answer 1

2

The SERVER_ADDR is a FastCGI variable that is being set inside the fastcgi_params file as

fastcgi_param  SERVER_ADDR        $server_addr;

The $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 nginx internal variable value, e.g.

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 StackOverflow answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.