Skip to content

Commit

Permalink
Get real ip using nginx
Browse files Browse the repository at this point in the history
  • Loading branch information
galeaspablo committed Sep 26, 2024
1 parent f792b77 commit a68fb40
Showing 1 changed file with 15 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,19 @@ http {
keepalive_timeout 65;

# Access and error logs are sent to stdout and stderr
access_log /dev/stdout;
access_log /dev/stdout main;
error_log /dev/stderr warn;

# Custom log format to log both REMOTE_ADDR and X-Forwarded-For
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';

# Set trusted Cloud Run proxy IP ranges
set_real_ip_from 0.0.0.0/0; # All IP ranges, or specific trusted ranges can be used
real_ip_header X-Forwarded-For;
real_ip_recursive on;

server {
listen 8080;
server_name localhost;
Expand All @@ -34,6 +44,10 @@ http {
fastcgi_pass unix:/var/run/php/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

# Pass the corrected client IP to PHP
fastcgi_param REMOTE_ADDR $realip_remote_addr;

include fastcgi_params;
}
}
Expand Down

0 comments on commit a68fb40

Please sign in to comment.