前言: 為了防止自己手殘點了不該點的網站, 導致惡意網站獲取我的個人信息, 或者網站在暗處偷偷獲取我的個人數據, 我需要去關注這些網站同時拉黑這些網站
目標: 將瀏覽器發起的所有請求都經過 nginx 服務器進行轉發, 然后 nginx 需要記錄這些網址以及請求所攜帶的參數信息
成果: 就在昨天, 我成功的在 linux 中部署了 nginx 正向代理服務器
搭建過程中可能遇到的問題: 1.nginx 編譯安裝失敗: 是因為沒有安裝足夠的依賴, 成功安裝后是有啟動腳本的, 在 sbin 文件夾中
存在的問題:1.日志記錄并不完整, url 不全, 沒有參數信息2.最開始只會轉發非本地請求記錄, 后來卻只轉發本地請求并記錄, 暫時未找到原因
環境: 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.安裝依賴及組件: 安裝 ifconfig: yum -y install net-tools安裝 git: yum -y install git安裝依賴: yum -y install gcc openssl openssl-devel pcre-devel zlib zlib-devel安裝 patch:yum -y install patch安裝 wget: yum -y install wget
3.下載資源:cd ~ nginx: wget http://nginx.org/download/nginx-1.20.2.tar.gzproxy_connect_rewrite_1018.patch: git clone https://gitee.com/web_design_of_web_frontend/ngx_http_proxy_connect_module.git
4.執行以下命令1>創建安裝目錄: mkdir /data2>移動:mv /root/nginx-1.20.2.tar.gz /data/nginx-1.20.2.tar.gzmv /root/ngx_http_proxy_connect_module /data/ngx_http_proxy_connect_module3>進入 data 并解壓 nginx:cd /datatar -zxvf nginx-1.20.2.tar.gz4>進入 nginx 目錄: cd nginx-1.20.25>安裝補丁: patch -p1 < /data/ngx_http_proxy_connect_module/patch/proxy_connect_rewrite_1018.patch6>nginx 編譯安裝前的配置: ./configure --add-module=/data/ngx_http_proxy_connect_module --prefix=/data/nginx-1.20.2 --with-http_ssl_module --with-http_stub_status_module7>開始編譯和安裝: make && make install8>一些配置:mkdir /data/nginx-1.20.2/logstouch /data/nginx-1.20.2/logs/access.logtouch /data/nginx-1.20.2/logs/error.logcp /data/nginx-1.20.2/conf/nginx.conf /data/nginx-1.20.2/conf/nginx.conf.bakecho "export NGINX_HOME=/data/nginx-1.20.2/sbin" >> ~/.bashrcecho "export PATH=\$PATH:\$NGINX_HOME" >> ~/.bashrcsource /root/.bashrcecho $PATH9>驗證 nginx 是否安裝成功: whereis nginx10.配置文件: 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:80https_proxy=nginx ip:443ftp_proxy=nginx ip:443export http_proxyexport https_proxyexport ftp_proxy12>至此, 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 --permanentfirewall-cmd --zone=public --add-port=8030/tcp --permanentfirewall-cmd --reloadfirewall-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