在网关中使用Nginx配置HTTP透明代理案例
出于某些需求在网关级架设HTTP透明代理,劫持用户HTTP请求,转发或直接进行响应。
iptables配置
iptables用于将经过网关的TCP80端口的上行流量转发至网关上的Nginx服务。
sudoiptables-tnat-APREROUTING-ptcp-mtcp--dport80-jDNAT\ --to-destination网关IP:端口
Nginx演示配置
worker_processes 1;
events{
worker_connections 1024;
}
http{
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server{
listen 8000;
server_name localhost;
resolver8.8.8.8;
location/test{
proxy_passhttp://hev.cc/sftp/files/;
proxy_buffers2564k;
proxy_max_temp_file_size0k;
}
location/{
#包含关键词'计算机'重定向至/test
rewrite^.*计算机.*$/testlast;
#通用透明代理
proxy_pass$scheme://$host$request_uri;
proxy_set_headerHost$http_host;
proxy_buffers2564k;
proxy_max_temp_file_size0k;
}
}
#匹配*.baidu.com域名
server{
listen 8000;
server_name *.baidu.com;
location/{
root html;
index index.htmlindex.htm;
}
}
}