php forward visitor ip to nginx - php

i am trying to send real visitor ip to nginx from php
this is the situation
server A - exmaple.com/a.php
server B - example/file.txt
when access exmaple.com/a.php it download file.txt located on server b
but nginx logs show server A ip as requested download, i guess that correct cause the file.txt downloaded via a.php located on server A
so how can i send the ip of the visitor instead of the server to nginx
i already have this in my nginx config
proxy_set_header X-Real-IP $remote_addr;
thank you

Server A: add X-Real-IP header with client's IP to outgoing request. This part depends on you code. For example, if CURL, you need to add curl_setopt($ch, CURLOPT_HTTPHEADER, [ 'X-Real-IP: '.$_SERVER['REMOTE_ADDR'] ]).
Server B: you need to configure nginx. Add to nginx's server config block:
set_real_ip_from SERVER_A_IP;
real_ip_header X-Real-IP; (not required because default value)

You would need to add it to your request headers.
$opts['http']['header'] = 'X-Real-IP: ' . $_SERVER['REMOTE_ADDR'] . "\r\n";
You would also need to configure Nginx to accept this, with set_real_ip_from config directives.
A better option would be to use cURL (see #Terra's answer), which gives you a bit more flexibility than the fopen wrappers.
The best option however is just to let Nginx do this. It's far more efficient than piping all this data through PHP. Use proxy_pass.

Related

Nginx it is possible to add ssl_client_verify to request header without proxy to let Symfony know a client ssl certificate is being used?

For a website I'm using a nginx configuration that requires a client ssl certificate. I want my Symfony/php project to be able to verify that a client ssl certificate is being used (and provide some extra information from the certificate as well). So I was thinking of doing this by adding it to the request http header.
In my nginx site configuration I have set:
ssl_client_certificate /home/user/ssl/ca.crt;
ssl_verify_client on;
This works, the client certificate is obligatory.
But I want my underlaying Symfony/php project to be able to verify that a client certificate is being used. I was thinking of adding it to the http request header, but I seem only to be able to add it to the http response header (back to the browser) like this (in the same nginx site config):
location / {
try_files $uri /app.php$is_args$args;
add_header X-ssl-client-verify $ssl_client_verify;
}
In firefox I can see this response header indeed, but that is not what I want (and can be a security hazzard). I've also looked into this:
proxy_set_header X-ssl-client-verify $ssl_client_verify;
But this does not work because I'm not using a proxy.
Is there some other way to add an element to the request header? Or is there an alternative way to get client ssl certificate information into my Symfony / php project?

Communication between two servers for a ddos challenge page?

I need to develop a challenge page much similar to the Cloudflare firewall challenge.
I know how to make the front end and the back end of the challenge app and I know how to set it up on the server.
The problem comes on how to implement it to the website which is on one server, while the challenge app is on another server. How should I make the communication between the servers? Both servers will be Ubuntu 16.4. Thank you all in advance.
I think it's better to solve this issue like cloudflare using nginx server.
Make an nginx reverse proxy with rate limit
if limit hit user will be redirected to error page
integrate your challenge app with the error page.
more about this configuration is here:
https://serverfault.com/questions/645154/how-to-redirect-to-an-other-link-when-excess-request-limit-req-with-nginx
And How to use PHP in error pages is here:
Nginx, PHP + FPM Custom Error Pages
you can run this reverse proxy on third server or you can run it on the challenge app server.
make your domain points to the reverse proxy
then
make nginx config
server {
listen 80 default_server;
server_name *.*;
client_body_timeout 5s;
client_header_timeout 5s;
location / {
proxy_set_header Host $host;
proxy_pass http://00.00.00.00/; #replace with upstream ip
}
}
you have to combine this with custom php error page

There is mismatch between HTTPS indicated on the server and client

This is the warning when I open my phpMyAdmin's login (index) page:
There is mismatch between HTTPS indicated on the server and client.
This can lead to non working phpMyAdmin or a security risk.
Please fix your server configuration to indicate HTTPS properly.
The error should be caused by a loadbalancer in between my client and phpmyadmin itself. SSL terminates on the loadbalancer so the URL being used (that phpmyadmin receives in request headers, I assume) is https://mydomain/phpmyadmin.
The loadbalancer communicates with phpmyadmin via http, so the URL being used between lb and pma is http://mydomain/phpmyadmin (not https).
I found this very fitting article on github: Possibility to deactivate SSL connection #170 which is for Docker containers and describes an env var to be passed to the container called "PMA_ABSOLUTE_URI" to fix the problem.
Which setting would this be in phpmyadmin NON Docker?
Any other solution to my problem is also highly appreciated.
Sidenote: Phpmyadmin works fine after the login. You can log in, there are no warnings after the log in and you can perform all interactions without problems. I am just worried about the warning.
I have exactly the same setup as you are describing. A front load balancer acts as reverse proxy and also as SSL/TLS terminator. The LB talks in plain http with the backend server where phpMyAdmin is running.
When I upgraded from 4.0.4.1 to 4.9.0.1 I got the same warning appearing at the phpMyAdmin login screen as you. I was able to solve this on the reverse proxy by "faking" the protocol from http to https. In my case my reverse proxy is a Nginx web server and just before I'd pass to the backend server, I added X-Forwarded-Proto:
server {
listen 443;
server_name my.phpmyadmin.example.com;
[... log and ssl settings ...]
location / {
include /etc/nginx/proxy.conf;
proxy_set_header X-Forwarded-Proto https;
proxy_pass http://backendserver;
}
}
By adding proxy_set_header X-Forwarded-Proto https; this tells the backend server that the client to proxy communication happens over https. Without setting this header, phpMyAdmin probably identifies (not sure, just a guess) that it was loaded on a https:// URL yet the communication (between reverse proxy and phpMyAdmin server) happened over http. Therefore it's a correct warning to be shown.
As soon as Nginx was reloaded, the warning disappared from the phpMyAdmin login screen.

Yii2: Add local nginx proxy to request trusted hosts

I have nginx and php7.2-fpm installed on single Debian machine. nginx forwards requests to php through linux socks. I use yii2 which has request->trustedHosts setting. I need to add my local nginx there to be able to get x-forward-proto and other secure headers set.
Which address do I need to set up in trustedHosts? Ipv4 and Ipv6 cidr loopbacks do not help.
I just don't understand what network interface nginx uses to proxy request - loopback (127.0.0.1), some internal ip, external (internet) ip? So what is the default outbound ip nginx select to bind (should I use proxy_bind to change it to 127.0.0.1)?
Thanks in advance.
request->trustedHosts setting is checked against ip address provided by $_SERVER["REMOTE_ADDR"]. So check the contents of this variable.
If your machine is behind a proxy, $_SERVER["REMOTE_ADDR"] should contain proxy's ip. Try adding that ip into trustedHosts array.
(if there is no proxy and your machine is directly connected to the internet, it seems that it makes no sense to use trustedHosts. In that case $_SERVER["REMOTE_ADDR"] would contain client's ip, but you should NOT trust any client by adding "0.0.0.0/0" into trustedHosts)

Load balancing dynamic php site using nginx

I have three servers. One for load balancing other two for serving web application. My load balancing works fine if i use my web page as static site. but when i log into my web page it does not respond correctly because every time it changes its server when the page loads. how can i do it without changing current server before log-out. My load balancing server configuration is
upstream web_backend{
server 192.168.33.2;
server 192.168.33.3;
}
server{
listen 80;
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://web_backend;
}
}
You can use Session persistence feature of nginx:
If there is the need to tie a client to a particular application
server — in other words, make the client’s session “sticky” or
“persistent” in terms of always trying to select a particular server —
the ip-hash load balancing mechanism can be used.
With ip-hash, the client’s IP address is used as a hashing key to
determine what server in a server group should be selected for the
client’s requests. This method ensures that the requests from the same
client will always be directed to the same server except when this
server is unavailable.
To configure ip-hash load balancing, just add the ip_hash directive to
the server (upstream) group configuration:
In your case just add ip_hash into your upstream definition
upstream web_backend{
ip_hash;
server 192.168.33.2;
server 192.168.33.3;
}

Categories