Coder Social home page Coder Social logo

ar-ngxrealip's Introduction

Get Real Visitor IP Address (Restoring Visitor IPs) with Nginx and ArvanCloud

This project aims to modify your nginx configuration to let you get the real ip address of your visitors for your web applications that behind of ArvanCloud's reverse proxy network. Bash script can be scheduled to create an automated up-to-date ArvanCloud ip list file.

To provide the client (visitor) IP address for every request to the origin, ArvanCloud adds the "ar-real-ip" header. We will catch the header and get the real ip address of the visitor.

Nginx Configuration

With a small configuration modification we can integrate replacing the real ip address of the visitor instead of getting ArvanCloud's load balancers' ip addresses.

Open "/etc/nginx/nginx.conf" file with your favorite text editor and just add the following lines to your nginx.conf inside http{....} block.

include /etc/nginx/arvancloud;

The bash script may run manually or can be scheduled to refresh the ip list of ArvanCloud automatically.

#!/bin/bash

ARVANCLOUD_FILE_PATH=/etc/nginx/arvancloud

echo "#ArvanCloud" > $ARVANCLOUD_FILE_PATH;
echo "" >> $ARVANCLOUD_FILE_PATH;

echo "# - IPv4" >> $ARVANCLOUD_FILE_PATH;
for i in `curl https://www.arvancloud.com/fa/ips.txt`; do
    echo "set_real_ip_from $i;" >> $ARVANCLOUD_FILE_PATH;
done

echo "" >> $ARVANCLOUD_FILE_PATH;
echo "real_ip_header ar-real-ip;" >> $ARVANCLOUD_FILE_PATH;

#test configuration and reload nginx
nginx -t && systemctl reload nginx

Output

Your "/etc/nginx/arvancloud" file may look like as below;

#ArvanCloud ip addresses

# - IPv4
set_real_ip_from 92.114.16.80/28;
set_real_ip_from 185.112.35.144/28;
set_real_ip_from 185.49.87.120/29;
set_real_ip_from 185.20.160.248/29;
set_real_ip_from 5.160.139.200/29;
set_real_ip_from 46.224.2.32/29;
set_real_ip_from 2.146.0.0/28;
set_real_ip_from 79.175.138.128/29;
set_real_ip_from 185.143.232.0/22;
set_real_ip_from 89.187.169.88/29;
set_real_ip_from 89.187.178.96/29;
set_real_ip_from 185.152.67.56/29;
set_real_ip_from 185.246.211.64/27;
set_real_ip_from 89.187.175.136/29;
set_real_ip_from 195.181.173.128/29;
set_real_ip_from 83.123.255.56/31;
set_real_ip_from 83.121.255.40/31;
set_real_ip_from 164.138.128.28/31;
set_real_ip_from 188.229.116.16/29;
set_real_ip_from 130.185.120.0/23;
set_real_ip_from 45.76.176.64/28;
set_real_ip_from 198.13.38.112/28;
set_real_ip_from 149.28.160.0/28;
set_real_ip_from 94.182.182.28/30;
set_real_ip_from 185.17.115.176/30;
set_real_ip_from 139.180.154.16/29;
set_real_ip_from 45.77.129.64/29;
set_real_ip_from 178.22.121.224/29;
set_real_ip_from 178.216.248.224/29;
set_real_ip_from 195.181.174.96/29;
set_real_ip_from 103.205.143.112/29;
set_real_ip_from 103.250.185.144/29;
set_real_ip_from 89.187.163.144/28;
set_real_ip_from 217.138.193.128/29;
set_real_ip_from 91.193.5.216/29;
set_real_ip_from 188.241.176.160/29;
set_real_ip_from 193.239.86.24/29;
set_real_ip_from 176.125.231.56/29;
set_real_ip_from 84.252.94.0/29;
set_real_ip_from 91.219.214.48/29;
set_real_ip_from 193.29.105.128/29;
set_real_ip_from 103.26.207.48/29;
set_real_ip_from 89.45.48.8/29;
set_real_ip_from 185.105.101.200/29;

real_ip_header ar-real-ip;

Crontab

Change the location of "/opt/scripts/arvancloud-ip-whitelist-sync.sh" anywhere you want. ArvanCloud ip addresses are automatically refreshed every day, and nginx will be realoded when synchronization is completed.

# Auto sync ip addresses of ArvanCloud and reload nginx
30 2 * * * /opt/scripts/arvancloud-ip-whitelist-sync.sh >/dev/null 2>&1

ar-ngxrealip's People

Contributors

3aaber avatar arastu avatar timcheh avatar yzdann avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.