Apache2 and Nginx Server Address in Header
Recently I get a request which needs to send the server IP address(not client IP address) from Apache2 to Nginx, finally to our lambda Function.
It is not that easy to do this, first of all, that machine has about 4 IP, I have them all listed in the /etc/hosts, I do not know how all these are set, but I have all the IP in /etc/hosts.
So first step, apache2 need to have it. Pass that to nginx, nginx need to forward that to lambda.
A website which can help us to check the request header
http://requestbin.fullcontact.com
Nginx Setting
Easy thing first, How nginx forward the header from apache2 to lambda is easy, this configuration will do the work.
# redirect traffic to aws lambda
resolver 8.8.8.8;
set $ocpServer 'https://proxy.sillycat.com';
location / {
if ( $http_user_agent = 'oldclients' ) {
proxy_pass http://localhost:5080;
}
if ( $http_user_agent != 'oldclients' ) {
proxy_pass $ocpServer;
}
#for testing the header, comments out
#proxy_pass http://requestbin.fullcontact.com/195blxxxx;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass_request_headers on;
proxy_ssl_server_name on;
}
proxy_pass_request_headers on; will do the work.
How Apache2 Get the IP
Here is a detail situation in my case. The Apache2 Configuration is not hard code in my case, it is generate by Python Script. So my idea is Python Script can find the IP first, then write that IP in the configuration file there. So it will be simple configuration in Apache2
RequestHeader set X-Sillycat-Serveraddr ‘198.168.1.159'
How Python get the IP
import socket
try:
SILLYCAT_EXTERNAL_IP = socket.gethostbyname(“sillycat-external-ip")
except:
SILLYCAT_EXTERNAL_IP = ""
pass
"""
RequestHeader set X-Sillycat-Serveraddr {sillycat_external_ip}
""".format(sillycat_external_ip=SILLYCAT_EXTERNAL_IP)
That is it, it will work.
References:
http://requestbin.fullcontact.com
https://stackoverflow.com/questions/154441/set-up-an-http-proxy-to-insert-a-header
https://serverfault.com/questions/391554/forward-custom-header-from-nginx-reverse-proxy
https://stackoverflow.com/questions/19751313/forward-request-headers-from-nginx-proxy-server
Recently I get a request which needs to send the server IP address(not client IP address) from Apache2 to Nginx, finally to our lambda Function.
It is not that easy to do this, first of all, that machine has about 4 IP, I have them all listed in the /etc/hosts, I do not know how all these are set, but I have all the IP in /etc/hosts.
So first step, apache2 need to have it. Pass that to nginx, nginx need to forward that to lambda.
A website which can help us to check the request header
http://requestbin.fullcontact.com
Nginx Setting
Easy thing first, How nginx forward the header from apache2 to lambda is easy, this configuration will do the work.
# redirect traffic to aws lambda
resolver 8.8.8.8;
set $ocpServer 'https://proxy.sillycat.com';
location / {
if ( $http_user_agent = 'oldclients' ) {
proxy_pass http://localhost:5080;
}
if ( $http_user_agent != 'oldclients' ) {
proxy_pass $ocpServer;
}
#for testing the header, comments out
#proxy_pass http://requestbin.fullcontact.com/195blxxxx;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass_request_headers on;
proxy_ssl_server_name on;
}
proxy_pass_request_headers on; will do the work.
How Apache2 Get the IP
Here is a detail situation in my case. The Apache2 Configuration is not hard code in my case, it is generate by Python Script. So my idea is Python Script can find the IP first, then write that IP in the configuration file there. So it will be simple configuration in Apache2
RequestHeader set X-Sillycat-Serveraddr ‘198.168.1.159'
How Python get the IP
import socket
try:
SILLYCAT_EXTERNAL_IP = socket.gethostbyname(“sillycat-external-ip")
except:
SILLYCAT_EXTERNAL_IP = ""
pass
"""
RequestHeader set X-Sillycat-Serveraddr {sillycat_external_ip}
""".format(sillycat_external_ip=SILLYCAT_EXTERNAL_IP)
That is it, it will work.
References:
http://requestbin.fullcontact.com
https://stackoverflow.com/questions/154441/set-up-an-http-proxy-to-insert-a-header
https://serverfault.com/questions/391554/forward-custom-header-from-nginx-reverse-proxy
https://stackoverflow.com/questions/19751313/forward-request-headers-from-nginx-proxy-server