浏览器端访问,或通过curl对应地址即可返回本地公网IP,返回格式如下:
curl https://www.getip.com/ip
{
"ip": "对应公网IP"
}
server {
listen 80;
server_name www.getip.com getip.com;
rewrite ^(.*)$ https://getip.com/$1 permanent;
}
server{
listen 443 ssl http2;
server_name www.getip.com getip.com;
#ssl
ssl_certificate cert/getip.com.crt;
ssl_certificate_key cert/getip.com.key;
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_session_cache shared:SSL:10m;
ssl_prefer_server_ciphers on;
location /ip {
add_header Content-Type application/json;
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Methods "GET, OPTIONS";
add_header Access-Control-Allow-Headers "Origin, X-Requested-With, Content-Type, Accept";
add_header Access-Control-Allow-Credentials "true";
if ($request_method = 'OPTIONS') {
add_header Content-Type 'text/plain';
add_header Content-Length 0;
return 204;
}
add_header X-Client-IP $remote_addr;
return 200 '{"ip": "$remote_addr"}';
}
#log
access_log /var/log/nginx/getip.com_access.log;
error_log /var/log/nginx/getip.com_error.log;
}