Skip to content

Commit cbf7633

Browse files
author
Ric Harvey
committed
Adds support for Real_ip in logs closes #106
1 parent 0b30a3a commit cbf7633

File tree

4 files changed

+38
-2
lines changed

4 files changed

+38
-2
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,17 @@ sudo docker run -d -e 'GIT_NAME=full_name' -e 'GIT_USERNAME=git_username' -e 'GI
106106
### Custom Nginx Config files
107107
Sometimes you need a custom config file for nginx to achieve this read the [Nginx config guide](https://github.com/ngineered/nginx-php-fpm/blob/master/docs/nginx_configs.md)
108108

109+
## REAL IP / X-Forwarded-For Headers
110+
If you operate your container behind a load balancer, an ELB on AWS for example, you need to configure nginx to get the real IP and not the load balancer IP in the logs by using the X-Forwarded-For. We've provided some handy flags to let you do this. You need to set both of these to get this to work:
111+
```
112+
-e "REAL_IP_HEADER=1"
113+
-e "REAL_IP_FROM=Your_CIDR"
114+
```
115+
For example:
116+
```
117+
docker run -d -e "REAL_IP_HEADER=1" -e "REAL_IP_FROM=10.1.0.0/16" richarvey/nginx-php-fpm:latest
118+
```
119+
109120
### Scripting and Templating
110121
Please see the [Scripting and templating guide](https://github.com/ngineered/nginx-php-fpm/blob/master/docs/scripting_templating.md) for more details.
111122

conf/nginx-site-ssl.conf

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,13 @@ server {
1919
sendfile off;
2020

2121
# Add stdout logging
22-
2322
error_log /dev/stdout info;
2423
access_log /dev/stdout;
2524

25+
# Add option for x-forward-for (real ip when behind elb)
26+
#real_ip_header X-Forwarded-For;
27+
#set_real_ip_from 172.16.0.0/12;
28+
2629
location / {
2730
# First attempt to serve request as file, then
2831
# as directory, then fall back to index.html

conf/nginx-site.conf

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,13 @@ server {
1212
sendfile off;
1313

1414
# Add stdout logging
15-
1615
error_log /dev/stdout info;
1716
access_log /dev/stdout;
1817

18+
# Add option for x-forward-for (real ip when behind elb)
19+
#real_ip_header X-Forwarded-For;
20+
#set_real_ip_from 172.16.0.0/12;
21+
1922
location / {
2023
# First attempt to serve request as file, then
2124
# as directory, then fall back to index.html

scripts/start.sh

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,25 @@ else
8585
sed -i "s/expose_php = On/expose_php = Off/g" /usr/local/etc/php-fpm.conf
8686
fi
8787

88+
# Pass real-ip to logs when behind ELB, etc
89+
if [[ "$REAL_IP_HEADER" == "1" ]] ; then
90+
sed -i "s/#real_ip_header X-Forwarded-For;/real_ip_header X-Forwarded-For;/" /etc/nginx/sites-available/default.conf
91+
sed -i "s/#set_real_ip_from/set_real_ip_from/" /etc/nginx/sites-available/default.conf
92+
if [ ! -z "$REAL_IP_FROM" ]; then
93+
sed -i "s#172.16.0.0/12#$REAL_IP_FROM#" /etc/nginx/sites-available/default.conf
94+
fi
95+
fi
96+
# Do the same for SSL sites
97+
if [ -f /etc/nginx/sites-available/default-ssl.conf ]; then
98+
if [[ "$REAL_IP_HEADER" == "1" ]] ; then
99+
sed -i "s/#real_ip_header X-Forwarded-For;/real_ip_header X-Forwarded-For;/" /etc/nginx/sites-available/default-ssl.conf
100+
sed -i "s/#set_real_ip_from/set_real_ip_from/" /etc/nginx/sites-available/default-ssl.conf
101+
if [ ! -z "$REAL_IP_FROM" ]; then
102+
sed -i "s#172.16.0.0/12#$REAL_IP_FROM#" /etc/nginx/sites-available/default-ssl.conf
103+
fi
104+
fi
105+
fi
106+
88107
# Increase the memory_limit
89108
if [ ! -z "$PHP_MEM_LIMIT" ]; then
90109
sed -i "s/memory_limit = 128M/memory_limit = ${PHP_MEM_LIMIT}M/g" /usr/local/etc/php/conf.d/docker-vars.ini

0 commit comments

Comments
 (0)