2019-07-25 代理IP匿名性检测(docker)
@Date : 2019-07-25, 13:08
@Author : lmingzhi (lmingzhi@gmail.com)
@Version : 1.0
[TOC]
1.操作步骤
# 建立设置文件存放目录
mkdir -p ~/nginx
# 预启动 echo header docker 容器,本地端口号 80
docker run -d --rm --name nginx_echo -p 80:8080 brndnmtthws/nginx-echo-headers
# 拷贝容器内 nginx 设置文件到本地
docker cp nginx_echo:/usr/local/openresty/nginx/conf ~/nginx
# 修改配置文件 nginx.conf
# 注意'$'需要添加转义符
cat > ~/nginx/conf/nginx.conf << END
error_log stderr debug;
events {
worker_connections 1024;
}
http {
access_log off;
include mime.types;
server {
listen 8080;
location / {
echo "
{
\"ua\": \"\$http_user_agent\",
\"X-Real-IP\": \"\$remote_addr\",
\"X-Forwarded-For\": \"\$proxy_add_x_forwarded_for\"
}";
}
}
}
END
# 目录结构
➜ tree ~/nginx
/Users/$(whoami)/nginx
└── conf
├── fastcgi.conf
├── fastcgi.conf.default
├── fastcgi_params
├── fastcgi_params.default
├── koi-utf
├── koi-win
├── mime.types
├── mime.types.default
├── nginx.conf
├── nginx.conf.default
├── scgi_params
├── scgi_params.default
├── uwsgi_params
├── uwsgi_params.default
└── win-utf
1 directory, 15 files
# 关闭容器
docker stop nginx_echo
# 重启容器,挂载配置文件
docker run -d --rm \
--name nginx_echo \
-v ~/nginx/conf:/usr/local/openresty/nginx/conf \
-p 80:8080 \
brndnmtthws/nginx-echo-headers
# 本地浏览器访问
➜ curl http://127.0.0.1
{
"ua": "curl/7.54.0",
"X-Real-IP": "172.17.0.1",
"X-Forwarded-For": "172.17.0.1"
}
备注:
检查X-Forwarded-For
是否会带有真实请求IP。
2.配置文件示例
修改镜像里的配置文件 /usr/local/openresty/nginx/conf/nginx.conf
error_log stderr debug;
events {
worker_connections 1024;
}
http {
access_log off;
include mime.types;
server {
listen 8080;
location / {
echo "
{
\"ua\": \"$http_user_agent\",
\"X-Real-IP\": \"$remote_addr\",
\"X-Forwarded-For\": \"$proxy_add_x_forwarded_for\"
}";
}
}
}
3.附录 nginx-echo-headers
Docker 镜像
3.1 nginx-echo-headers/Dockerfile
FROM openresty/openresty:1.11.2.5-alpine
EXPOSE 8080
ADD nginx.conf /usr/local/openresty/nginx/conf/nginx.conf
RUN chgrp -R 0 /usr/local/openresty/nginx/ && \
chmod -R g=u /usr/local/openresty/nginx/
3.2 nginx-echo-headers/nginx.conf
error_log stderr debug;
events {
worker_connections 1024;
}
http {
access_log off;
include mime.types;
server {
listen 8080;
location / {
echo_duplicate 1 $echo_client_request_headers;
echo "\r";
echo_read_request_body;
echo $request_body;
}
}
}