A
A
Alex Raven2020-05-02 13:42:24
Nginx
Alex Raven, 2020-05-02 13:42:24

How to get $_SERVER['HTTPS'] and $_SERVER['SERVER_PORT'] values ​​on nginx?

Hello. I need advice on setting up nginx, since I'm not an expert in it (this is with Apache, I'm on "you"). The server configuration was done by a previous developer who is no longer available.

The problem is this. I need to define in PHP the protocol by which the script is called. The value of the _SERVER["HTTPS"] variable is not defined (no value), and for some reason _SERVER["SERVER_PORT"] is always 80, even if the page is requested via HTTPS.

Googling, I found an instruction where it says to add server { ... } to the section

fastcgi_param  HTTPS 'on';
    fastcgi_param HTTPS $fastcgi_param_https_variable;
    fastcgi_param SERVER_PORT $server_port;


but it didn't help, even though before that SERVER_PORT and HTTPS were not present at all in the _SERVER array. Who faced a similar problem? The server is a Docker container in Amazon Web Services. It works with Load balancer, maybe that's the problem?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Eugene, 2020-05-02
@alexraven

I will assume that your workflow is now something like this:
AWS *LB (I assume ALB if your colleagues don’t like ELB so much that they haven’t moved yet) receives traffic, terminates https and sends it to NGINX in decrypted form. It can also accept HTTP
container NGINX serves for internal traffic balancing or, possibly, hosts applications. It always listens to HTTP
In the general case, the container and the application do not need to know how the traffic came inside the network - ALB takes care of HTTPS and then the application should work regardless of which port it came from outside and whether the traffic was encrypted.
This raises a logical question: why did your application need to know what protocol the traffic comes from? But that's your problem.
Specifically, you need to either process or redirect further to the application with NGINX (if NGINX is used as an internal LB) the X-Forwarded-Proto and X-Forwarded-Port
headers https://docs.aws.amazon.com/elasticloadbalancing/l...
Here they confirm that the same headers are created for ALB
How to do it on NGINX - the second part of the question.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question