Browse Source

refactor: nginx config file

master
dnomd343 3 years ago
parent
commit
40933bb310
  1. 3
      backend/getInfo.php
  2. 15
      backend/queryInfo.php
  3. 59
      conf/nginx/docker.conf
  4. 55
      conf/nginx/ip.conf

3
backend/getInfo.php

@ -100,7 +100,8 @@ function getIPInfo($ip) {
} }
} }
if ($_GET['cli'] == "true") { // 使用命令行模式 global $request;
if ($request['cli']) { // 使用命令行模式
$cli = "IP: ".$info['ip'] . PHP_EOL; $cli = "IP: ".$info['ip'] . PHP_EOL;
$cli .= "AS: ".$info['as'] . PHP_EOL; $cli .= "AS: ".$info['as'] . PHP_EOL;
$cli .= "City: ".$info['city'] . PHP_EOL; $cli .= "City: ".$info['city'] . PHP_EOL;

15
backend/queryInfo.php

@ -22,7 +22,7 @@ function preRount() { // 解析请求路径
} else if ($requestUri == '/version') { // URI -> /version } else if ($requestUri == '/version') { // URI -> /version
$request['version'] = true; $request['version'] = true;
return; return;
} else if ($requestUri == '/info') { // URI -> /info } else if ($requestUri == '/info' || $requestUri == '/info/') { // URI -> /info or /info/
$request['ip'] = getClientIP(); $request['ip'] = getClientIP();
return; return;
} else if ($requestUri == '/query') { // URI -> /query?xxx=xxx } else if ($requestUri == '/query') { // URI -> /query?xxx=xxx
@ -34,7 +34,11 @@ function preRount() { // 解析请求路径
} }
preg_match('#^/([^/]+?)$#', $requestUri, $match); // URI -> /{ip} preg_match('#^/([^/]+?)$#', $requestUri, $match); // URI -> /{ip}
if (count($match) > 0) { if (count($match) > 0) {
$request['ip'] = $match[1]; if ($request['cli']) { // 命令行模式
$request['ip'] = $match[1];
} else {
$request['error'] = true;
}
return; return;
} }
preg_match('#^/info/([^/]+?)$#', $requestUri, $match); // URI -> /info/{ip} preg_match('#^/info/([^/]+?)$#', $requestUri, $match); // URI -> /info/{ip}
@ -91,9 +95,10 @@ function routeParam() {
if ($request['cli']) { // 命令行模式 if ($request['cli']) { // 命令行模式
echo "Illegal Request" . PHP_EOL; echo "Illegal Request" . PHP_EOL;
} else { } else {
$reply = array(); $reply = array(
$reply['status'] = 'F'; 'status' => 'F',
$reply['message'] = 'Illegal Request'; 'message' => 'Illegal Request'
);
header('Content-Type: application/json; charset=utf-8'); header('Content-Type: application/json; charset=utf-8');
echo json_encode($reply); echo json_encode($reply);
} }

59
conf/nginx/docker.conf

@ -1,12 +1,20 @@
server { server {
listen 1601; listen 127.0.0.1:1601;
set $this 127.0.0.1:1601; set $my_host 127.0.0.1:1601;
set_real_ip_from 0.0.0.0/0; set_real_ip_from 0.0.0.0/0;
real_ip_header X-Real-IP; real_ip_header X-Real-IP;
root /var/www/echoIP; root /var/www/echoIP;
error_page 403 404 = /error.html; error_page 403 404 = /error.html;
location ^~ /assets {}
location = /index.html {}
location = /error.html {}
location = /error {
index error.html;
}
location = /ua { location = /ua {
if ($http_user_agent ~* (curl|wget)) { if ($http_user_agent ~* (curl|wget)) {
return 200 $http_user_agent\n; return 200 $http_user_agent\n;
@ -18,56 +26,19 @@ server {
location = / { location = / {
set $query_param ?justip=true&cli=true; set $query_param ?justip=true&cli=true;
if ($http_user_agent ~* (curl|wget)) { if ($http_user_agent ~* (curl|wget)) {
proxy_pass http://$this/query$query_param; proxy_pass http://$my_host/query$query_param;
} }
index index.html; index index.html;
} }
location = /ip { location / {
set $query_param ?justip=true; set $query_param $query_string;
if ($http_user_agent ~* (curl|wget)) { if ($http_user_agent ~* (curl|wget)) {
set $query_param $query_param&cli=true; set $query_param $query_param&cli=true;
} }
proxy_pass http://$this/query$query_param;
}
location = /version {
set $query_param ?version=true;
if ($http_user_agent ~* (curl|wget)) {
set $query_param $query_param&cli=true;
}
proxy_pass http://$this/query$query_param;
}
location ~* ^/([^/]+?)$ {
set $request_ip $1;
if ($http_user_agent ~* (curl|wget)) {
proxy_pass http://$this/info/$request_ip;
}
}
location ^~ /info {
set $is_legal 0;
set $query_param ?cli=false;
if ($http_user_agent ~* (curl|wget)) {
set $query_param ?cli=true;
}
if ($uri ~* ^/info/?$) {
set $is_legal 1;
}
if ($uri ~* ^/info/([^/]+?)$) {
set $is_legal 1;
set $query_param $query_param&ip=$1;
}
if ($is_legal = 0) {
set $query_param $query_param&error=true;
}
proxy_pass http://$this/query$query_param;
}
location = /query {
include fastcgi_params; include fastcgi_params;
fastcgi_pass 127.0.0.1:9000; fastcgi_pass 127.0.0.1:9000;
fastcgi_param QUERY_STRING $query_param;
fastcgi_param SCRIPT_FILENAME /var/www/echoIP/backend/queryInfo.php; fastcgi_param SCRIPT_FILENAME /var/www/echoIP/backend/queryInfo.php;
} }
} }

55
conf/nginx/ip.conf

@ -34,13 +34,21 @@ server {
server { server {
listen 127.0.0.1:1601; listen 127.0.0.1:1601;
set $this 127.0.0.1:1601; set $my_host 127.0.0.1:1601;
set_real_ip_from 0.0.0.0/0; set_real_ip_from 0.0.0.0/0;
real_ip_header X-Real-IP; real_ip_header X-Real-IP;
root /var/www/echoIP; root /var/www/echoIP;
error_page 403 404 = /error.html; error_page 403 404 = /error.html;
location ^~ /assets {}
location = /index.html {}
location = /error.html {}
location = /error {
index error.html;
}
location = /ua { location = /ua {
if ($http_user_agent ~* (curl|wget)) { if ($http_user_agent ~* (curl|wget)) {
return 200 $http_user_agent\n; return 200 $http_user_agent\n;
@ -52,56 +60,19 @@ server {
location = / { location = / {
set $query_param ?justip=true&cli=true; set $query_param ?justip=true&cli=true;
if ($http_user_agent ~* (curl|wget)) { if ($http_user_agent ~* (curl|wget)) {
proxy_pass http://$this/query$query_param; proxy_pass http://$my_host/query$query_param;
} }
index index.html; index index.html;
} }
location = /ip { location / {
set $query_param ?justip=true; set $query_param $query_string;
if ($http_user_agent ~* (curl|wget)) {
set $query_param $query_param&cli=true;
}
proxy_pass http://$this/query$query_param;
}
location = /version {
set $query_param ?version=true;
if ($http_user_agent ~* (curl|wget)) { if ($http_user_agent ~* (curl|wget)) {
set $query_param $query_param&cli=true; set $query_param $query_param&cli=true;
} }
proxy_pass http://$this/query$query_param;
}
location ~* ^/([^/]+?)$ {
set $request_ip $1;
if ($http_user_agent ~* (curl|wget)) {
proxy_pass http://$this/info/$request_ip;
}
}
location ^~ /info {
set $is_legal 0;
set $query_param ?cli=false;
if ($http_user_agent ~* (curl|wget)) {
set $query_param ?cli=true;
}
if ($uri ~* ^/info/?$) {
set $is_legal 1;
}
if ($uri ~* ^/info/([^/]+?)$) {
set $is_legal 1;
set $query_param $query_param&ip=$1;
}
if ($is_legal = 0) {
set $query_param $query_param&error=true;
}
proxy_pass http://$this/query$query_param;
}
location = /query {
include fastcgi_params; include fastcgi_params;
fastcgi_pass 127.0.0.1:9000; fastcgi_pass 127.0.0.1:9000;
fastcgi_param QUERY_STRING $query_param;
fastcgi_param SCRIPT_FILENAME /var/www/echoIP/backend/queryInfo.php; fastcgi_param SCRIPT_FILENAME /var/www/echoIP/backend/queryInfo.php;
} }
} }
Loading…
Cancel
Save