# ip2region **Repository Path**: jet-long/ip2region ## Basic Information - **Project Name**: ip2region - **Description**: 根据ip地址获得所在地区名称,如:中国|0|北京|北京市|联通。本地化部署,本地数据库,无外网请求 - **Primary Language**: PHP - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 0 - **Created**: 2025-01-03 - **Last Updated**: 2025-01-03 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # ip2region #### Introduction Obtain the location name based on the IP address, such as: 中国|0|北京|北京市|联通. Local deployment, local database, no external network requests. #### Software Architecture 1. Development Framework: webman, PHP version 8.0 or above 2. Source of Extension: https://gitee.com/lionsoul/ip2region #### Usage Instructions 1. You can pull the code and run it yourself ```html git clone https://gitee.com/jet-long/ip2region.git cd ip2region composer install # Start webman service on Windows php .\windows.php # Start webman service on Linux (add -d to run in the background) php webman start -d # Pack into a binary file (the packed file will be in the build directory) php -d phar.readonly=0 ./webman build:bin # Specify the PHP version for packaging php -d phar.readonly=0 ./webman build:bin 8.1 ``` 2. 【Recommended】Download the compressed package to run the binary file (this binary file is packed with webman features, and the packed file currently only supports running on x86_64 architecture Linux systems, and does not support Windows and Mac systems) ```html 1. Download the latest release version 2. Unzip the package 3. Grant executable permission to the program files 4. Start the program: ./ip2region start -d ``` 3. This program exposes an API address that, when a specific IP address is passed in, returns the corresponding JSON format data ```javascript // Request address http://localhost:9501/ip2region?ip=111.203.145.123 // Example of returned data: { "code": 1, "data": { "ip": "111.203.145.123", "region": "China|0|Beijing|Beijing|Unicom", "gap": 0.023193359375 }, "msg": "success", "time": 1735889117 } ``` 4. Add site configuration domain name in nginx and forward to webman service ```nginx server { listen 80; server_name www.test.com; index index.php index.html index.htm; root /path/to/ip2region/public; # Forward requests to webman location ^~ / { proxy_set_header Host $http_host; proxy_set_header X-Forwarded-For $remote_addr; proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header X-Real-IP $remote_addr; proxy_http_version 1.1; proxy_set_header Connection ""; if (!-f $request_filename){ proxy_pass http://127.0.0.1:8703; } } # Deny access to all files ending with .php location ~ \.php$ { return 404; } # Allow access to the .well-known directory location ~ ^/\.well-known/ { allow all; } # Deny access to all files or directories starting with . location ~ /\. { return 404; } } ```