Apache2 and Nginx Server Address in Header

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值