前言: 为了防止自己手残点了不该点的网站, 导致恶意网站获取我的个人信息, 或者网站在暗处偷偷获取我的个人数据, 我需要去关注这些网站同时拉黑这些网站
目标: 将浏览器发起的所有请求都经过 nginx 服务器进行转发, 然后 nginx 需要记录这些网址以及请求所携带的参数信息
成果: 就在昨天, 我成功的在 linux 中部署了 nginx 正向代理服务器
搭建过程中可能遇到的问题:
1.nginx 编译安装失败: 是因为没有安装足够的依赖, 成功安装后是有启动脚本的, 在 sbin 文件夹中
存在的问题:
1.日志记录并不完整, url 不全, 没有参数信息
遇到的问题:
1.远端的请求没有代理
可能是因为修改了 nginx 配置之后, window 的代理设置需要重新刷新, 也就是关闭开启
2.nginx 运行中, 但是出现 502 Bad Gateway
因为目前正在运行代理服务器, 当请求后会先经过代理服务器, 然后 error 中会打印信息, 关掉正向代理即可
疑惑:
1.在 profile 中设置的 443 端口是怎么回事
环境: window10、centos7、nginx-1.20.2、proxy_connect_rewrite_1018.patch
欢迎大家查漏补缺
1.centos7 最小安装完成后是无法联网的
修改 /etc/sysconfig/network-scripts/ifcfg-ens33 配置: ONBOOT=yes
重启网络: systemctl restart network
2.安装依赖及组件:
yum -y install net-tools
yum -y install git
yum -y install gcc openssl openssl-devel pcre-devel zlib zlib-devel
yum -y install patch
yum -y install wget
3.下载资源:
cd ~
wget http://nginx.org/download/nginx-1.20.2.tar.gz
git clone https://gitee.com/web_design_of_web_frontend/ngx_http_proxy_connect_module.git
4.执行以下命令
1>创建安装目录: mkdir /data
2>移动:
mv /root/nginx-1.20.2.tar.gz /data/nginx-1.20.2.tar.gz
mv /root/ngx_http_proxy_connect_module /data/ngx_http_proxy_connect_module
3>进入 data 并解压 nginx:
cd /data
tar -zxvf nginx-1.20.2.tar.gz
4>进入 nginx 目录: cd nginx-1.20.2
5>安装补丁: patch -p1 < /data/ngx_http_proxy_connect_module/patch/proxy_connect_rewrite_1018.patch
6>nginx 编译安装前的配置:
./configure --add-module=/data/ngx_http_proxy_connect_module --prefix=/data/nginx-1.20.2 --with-http_ssl_module --with-http_stub_status_module
7>开始编译和安装: make && make install
8>一些配置:
mkdir /data/nginx-1.20.2/logs
touch /data/nginx-1.20.2/logs/access.log
touch /data/nginx-1.20.2/logs/error.log
cp /data/nginx-1.20.2/conf/nginx.conf /data/nginx-1.20.2/conf/nginx.conf.bak
echo "export NGINX_HOME=/data/nginx-1.20.2/sbin" >> ~/.bashrc
echo "export PATH=\$PATH:\$NGINX_HOME" >> ~/.bashrc
source /root/.bashrc
echo $PATH
9>验证 nginx 是否安装成功: whereis nginx
10.配置文件: vi /data/nginx-1.20.2/conf/nginx.conf
# main 与 log_format 有关, 下面讲
server {
listen 8030;
server_name localhost;
resolver 114.114.114.114 ipv6=off;
proxy_connect;
proxy_connect_allow all;
proxy_connect_connect_timeout 10s;
proxy_connect_read_timeout 10s;
proxy_connect_send_timeout 10s;
access_log /data/nginx-1.20.2/logs/server.log main;
location / {
proxy_pass https://$host$request_uri;
proxy_set_header HOST $host;
proxy_http_version 1.1;
proxy_ssl_server_name on;
access_log /data/nginx-1.20.2/logs/location.log main;
}
}
11>配置 /etc/profile(在文件最下面添加即可), 添加完成后执行命令 source /etc/profile:
# 这里的地址要写代理的服务器地址
http_proxy=nginx ip:80
https_proxy=nginx ip:443
ftp_proxy=nginx ip:443
export http_proxy
export https_proxy
export ftp_proxy
12>至此, nginx 正向代理的相关配置完成
5.一些命令:
启动:
通过配置启动: nginx -c ./conf/nginx.conf
直接启动: nginx
停止: nginx -s stop
重启:
配置重启: nginx -c ./conf/nginx.conf -s reload
直接重启: nginx -s reload
6.windows 设置代理
windows 按键 -> 设置 -> 网络和 internet -> 代理 -> 使用代理服务器
输入代理服务器的 ip 及代理服务器监听的端口, 其实就是 nginx 所在的 linux 的 ip 以及 nginx 配置文件中设置的端口 8030
7.防火墙端口
firewall-cmd --zone=public --add-port=80/tcp --permanent
firewall-cmd --zone=public --add-port=8030/tcp --permanent
firewall-cmd --reload
firewall-cmd --zone=public --list-ports
8.然后开始访问浏览器, 所有的请求会经过代理服务器
log_format 是 nginx 的日志记录格式. 书写方式为: log_format key value, 下面提供两种
1.第一种是 nginx 默认的日志格式:
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
2.第二种是 json 格式, 更全一点:
log_format main '{"time": "$time_iso8601", '
'"remote_addr": "$remote_addr", '
'"remote_user": "$remote_user", '
'"request": "$request", '
'"status": $status, '
'"body_bytes_sent": $body_bytes_sent, '
'"referer": "$http_referer", '
'"user_agent": "$http_user_agent", '
'"request_method": "$request_method", '
'"scheme": "$scheme", '
'"server_name": "$server_name", '
'"request_uri": "$request_uri", '
'"uri": "$uri", '
'"query_string": "$query_string", '
'"server_protocol": "$server_protocol", '
'"request_length": $request_length, '
'"request_time": $request_time, '
'"upstream_addr": "$upstream_addr", '
'"upstream_response_time": "$upstream_response_time", '
'"upstream_status": "$upstream_status", '
'"ssl_protocol": "$ssl_protocol", '
'"ssl_cipher": "$ssl_cipher"}';
参考链接:
https://blog.csdn.net/chen_CJH/article/details/131827744?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522171569291716800186531378%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=171569291716800186531378&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~top_positive~default-1-131827744-null-null.142^v100^pc_search_result_base7&utm_term=nginx%E6%AD%A3%E5%90%91%E4%BB%A3%E7%90%86%E8%AE%BF%E9%97%AE%E5%A4%96%E7%BD%91&spm=1018.2226.3001.4187