$npx -y skills add chaterm/terminal-skills --skill proxy代理服务器配置
| 1 | # 代理服务器配置 |
| 2 | |
| 3 | ## 概述 |
| 4 | Squid、Nginx 代理、正向/反向代理配置技能。 |
| 5 | |
| 6 | ## Squid 正向代理 |
| 7 | |
| 8 | ### 安装与管理 |
| 9 | ```bash |
| 10 | # 安装 |
| 11 | apt install squid # Debian/Ubuntu |
| 12 | yum install squid # CentOS/RHEL |
| 13 | |
| 14 | # 服务管理 |
| 15 | systemctl start squid |
| 16 | systemctl enable squid |
| 17 | systemctl reload squid |
| 18 | |
| 19 | # 检查配置 |
| 20 | squid -k parse |
| 21 | squid -k check |
| 22 | ``` |
| 23 | |
| 24 | ### 基础配置 |
| 25 | ```bash |
| 26 | # /etc/squid/squid.conf |
| 27 | # 端口配置 |
| 28 | http_port 3128 |
| 29 | |
| 30 | # ACL 定义 |
| 31 | acl localnet src 10.0.0.0/8 |
| 32 | acl localnet src 172.16.0.0/12 |
| 33 | acl localnet src 192.168.0.0/16 |
| 34 | |
| 35 | acl SSL_ports port 443 |
| 36 | acl Safe_ports port 80 21 443 70 210 280 488 591 777 1025-65535 |
| 37 | |
| 38 | # 访问控制 |
| 39 | http_access deny !Safe_ports |
| 40 | http_access deny CONNECT !SSL_ports |
| 41 | http_access allow localnet |
| 42 | http_access deny all |
| 43 | |
| 44 | # 缓存配置 |
| 45 | cache_dir ufs /var/spool/squid 100 16 256 |
| 46 | maximum_object_size 100 MB |
| 47 | cache_mem 256 MB |
| 48 | |
| 49 | # 日志 |
| 50 | access_log /var/log/squid/access.log squid |
| 51 | cache_log /var/log/squid/cache.log |
| 52 | ``` |
| 53 | |
| 54 | ### 认证配置 |
| 55 | ```bash |
| 56 | # 基础认证 |
| 57 | auth_param basic program /usr/lib/squid/basic_ncsa_auth /etc/squid/passwd |
| 58 | auth_param basic children 5 |
| 59 | auth_param basic realm Squid Proxy |
| 60 | auth_param basic credentialsttl 2 hours |
| 61 | |
| 62 | acl authenticated proxy_auth REQUIRED |
| 63 | http_access allow authenticated |
| 64 | |
| 65 | # 创建用户 |
| 66 | htpasswd -c /etc/squid/passwd user1 |
| 67 | htpasswd /etc/squid/passwd user2 |
| 68 | ``` |
| 69 | |
| 70 | ### 透明代理 |
| 71 | ```bash |
| 72 | # Squid 配置 |
| 73 | http_port 3128 transparent |
| 74 | |
| 75 | # iptables 重定向 |
| 76 | iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 3128 |
| 77 | iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 443 -j REDIRECT --to-port 3128 |
| 78 | ``` |
| 79 | |
| 80 | ### 访问控制 |
| 81 | ```bash |
| 82 | # 时间控制 |
| 83 | acl work_hours time MTWHF 09:00-18:00 |
| 84 | http_access allow localnet work_hours |
| 85 | |
| 86 | # 域名黑名单 |
| 87 | acl blocked_sites dstdomain .facebook.com .youtube.com |
| 88 | http_access deny blocked_sites |
| 89 | |
| 90 | # URL 正则 |
| 91 | acl blocked_urls url_regex -i porn adult gambling |
| 92 | http_access deny blocked_urls |
| 93 | |
| 94 | # 带宽限制 |
| 95 | delay_pools 1 |
| 96 | delay_class 1 2 |
| 97 | delay_parameters 1 1000000/1000000 100000/100000 |
| 98 | delay_access 1 allow localnet |
| 99 | ``` |
| 100 | |
| 101 | ## Nginx 反向代理 |
| 102 | |
| 103 | ### 基础反向代理 |
| 104 | ```nginx |
| 105 | server { |
| 106 | listen 80; |
| 107 | server_name example.com; |
| 108 | |
| 109 | location / { |
| 110 | proxy_pass http://backend:8080; |
| 111 | proxy_set_header Host $host; |
| 112 | proxy_set_header X-Real-IP $remote_addr; |
| 113 | proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; |
| 114 | proxy_set_header X-Forwarded-Proto $scheme; |
| 115 | } |
| 116 | } |
| 117 | ``` |
| 118 | |
| 119 | ### HTTPS 反向代理 |
| 120 | ```nginx |
| 121 | server { |
| 122 | listen 443 ssl http2; |
| 123 | server_name example.com; |
| 124 | |
| 125 | ssl_certificate /etc/nginx/ssl/cert.pem; |
| 126 | ssl_certificate_key /etc/nginx/ssl/key.pem; |
| 127 | ssl_protocols TLSv1.2 TLSv1.3; |
| 128 | ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256; |
| 129 | |
| 130 | location / { |
| 131 | proxy_pass http://backend:8080; |
| 132 | proxy_http_version 1.1; |
| 133 | proxy_set_header Upgrade $http_upgrade; |
| 134 | proxy_set_header Connection "upgrade"; |
| 135 | proxy_set_header Host $host; |
| 136 | proxy_set_header X-Real-IP $remote_addr; |
| 137 | } |
| 138 | } |
| 139 | ``` |
| 140 | |
| 141 | ### WebSocket 代理 |
| 142 | ```nginx |
| 143 | location /ws { |
| 144 | proxy_pass http://websocket_backend; |
| 145 | proxy_http_version 1.1; |
| 146 | proxy_set_header Upgrade $http_upgrade; |
| 147 | proxy_set_header Connection "upgrade"; |
| 148 | proxy_set_header Host $host; |
| 149 | proxy_read_timeout 86400; |
| 150 | } |
| 151 | ``` |
| 152 | |
| 153 | ### 缓存配置 |
| 154 | ```nginx |
| 155 | proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m max_size=1g inactive=60m; |
| 156 | |
| 157 | server { |
| 158 | location / { |
| 159 | proxy_pass http://backend; |
| 160 | proxy_cache my_cache; |
| 161 | proxy_cache_valid 200 302 10m; |
| 162 | proxy_cache_valid 404 1m; |
| 163 | proxy_cache_use_stale error timeout updating; |
| 164 | add_header X-Cache-Status $upstream_cache_status; |
| 165 | } |
| 166 | } |
| 167 | ``` |
| 168 | |
| 169 | ## Nginx 正向代理 |
| 170 | |
| 171 | ### HTTP 正向代理 |
| 172 | ```nginx |
| 173 | server { |
| 174 | listen 8080; |
| 175 | resolver 8.8.8.8; |
| 176 | |
| 177 | location / { |
| 178 | proxy_pass http://$http_host$request_uri; |
| 179 | proxy_set_header Host $http_host; |
| 180 | proxy_buffers 256 4k; |
| 181 | proxy_max_temp_file_size 0; |
| 182 | proxy_connect_timeout 30; |
| 183 | } |
| 184 | } |
| 185 | ``` |
| 186 | |
| 187 | ### HTTPS 正向代理(ngx_http_proxy_connect_module) |
| 188 | ```nginx |
| 189 | server { |
| 190 | listen 8080; |
| 191 | resolver 8.8.8.8; |
| 192 | |
| 193 | proxy_connect; |
| 194 | proxy_connect_allow 443 563; |
| 195 | proxy_connect_connect_timeout 10s; |
| 196 | proxy_connect_read_timeout 10s; |
| 197 | proxy_connect_send_timeout 10s; |
| 198 | |
| 199 | location / { |
| 200 | proxy_pass http://$host; |
| 201 | proxy_set_header Host $host; |
| 202 | } |
| 203 | } |
| 204 | ``` |
| 205 | |
| 206 | ## HAProxy 代理 |
| 207 | |
| 208 | ### TCP 代理 |
| 209 | ```bash |
| 210 | frontend tcp_front |
| 211 | bind *:3306 |
| 212 | mode tcp |
| 213 | default_backend mysql_back |
| 214 | |
| 215 | backend mysql_back |
| 216 | mode tcp |
| 217 | balance roundrobin |
| 218 | server mysql1 192.168.1.10:3306 check |
| 219 | server mysql2 192.168.1.11:3306 check |
| 220 | ``` |
| 221 | |
| 222 | ### HTTP 代理 |
| 223 | ```bash |
| 224 | frontend http_front |
| 225 | bind *:80 |
| 226 | mode http |
| 227 | default_backend web_back |
| 228 | |
| 229 | backend web_back |
| 230 | mode http |
| 231 | balance roundrobin |
| 232 | option httpchk GET /health |
| 233 | server web1 192.168.1.10:8080 check |
| 234 | server web2 192.168.1.11:8080 check |
| 235 | ``` |
| 236 | |
| 237 | ## SOCKS 代理 |
| 238 | |
| 239 | ### SSH SOCKS 代理 |
| 240 | ```bash |
| 241 | # 创建 SOCKS5 代理 |
| 242 | ssh -D 1080 -f -C -q -N user@remote_server |
| 243 | |
| 244 | # 后台运行 |
| 245 | ssh -D 1080 -fNq user@remote_server |
| 246 | |
| 247 | # |