少女祈祷中...
NEKO
知识库

squid

安装

yum install -y squid httpd-tools

配置

  • /etc/squid/squid.conf

#
# Recommended minimum configuration:
#

# Example rule allowing access from your local networks.
# Adapt to list your (internal) IP networks from where browsing
# should be allowed
acl localnet src 10.0.0.0/8     # RFC1918 possible internal network
acl localnet src 172.16.0.0/12  # RFC1918 possible internal network
acl localnet src 192.168.0.0/16 # RFC1918 possible internal network
acl localnet src fc00::/7       # RFC 4193 local private network range
acl localnet src fe80::/10      # RFC 4291 link-local (directly plugged) machines

acl SSL_ports port 443
acl Safe_ports port 80          # http
acl Safe_ports port 21          # ftp
acl Safe_ports port 443         # https
acl Safe_ports port 210         # wais
acl Safe_ports port 1025-65535  # unregistered ports
acl Safe_ports port 280         # http-mgmt
acl Safe_ports port 488         # gss-http
acl Safe_ports port 591         # filemaker
acl Safe_ports port 777         # multiling http
acl CONNECT method CONNECT

#
# Recommended minimum Access Permission configuration:
#
# Deny requests to certain unsafe ports
http_access deny !Safe_ports

# Deny CONNECT to other than secure SSL ports
http_access deny CONNECT !SSL_ports

# Only allow cachemgr access from localhost
http_access allow localhost manager
http_access deny manager

# We strongly recommend the following be uncommented to protect innocent
# web applications running on the proxy server who think the only
# one who can access services on "localhost" is a local user
#http_access deny to_localhost

#
# INSERT YOUR OWN RULE(S) HERE TO ALLOW ACCESS FROM YOUR CLIENTS
#

# Example rule allowing access from your local networks.
# Adapt localnet in the ACL section to list your (internal) IP networks
# from where browsing should be allowed
http_access allow localnet
http_access allow localhost

# And finally deny all other access to this proxy
http_access deny all

# Squid normally listens to port 3128
http_port 3128

# Uncomment and adjust the following to add a disk cache directory.
#cache_dir ufs /var/spool/squid 100 16 256

# Leave coredumps in the first cache dir
coredump_dir /var/spool/squid

# ========== DNS 独立解析(避免依赖内网 DNS) ==========
dns_nameservers 114.114.114.114 223.5.5.5
connect_timeout 10 seconds        # 连接上游超时,10s 足够
forward_timeout 120 seconds       # 从 20s 提到 120s,慢 API 需要
read_timeout 60 seconds           # 读上游响应超时
request_timeout 10 minutes        # 等客户端请求体超时(POST 大 body)
persistent_request_timeout 30 seconds
client_idle_pconn_timeout 2 minutes
server_idle_pconn_timeout 2 minutes

# ========== 性能调优(内存+磁盘双重缓存) ==========
# 内存缓存:热点小文件放内存
cache_mem 0   # 视跳板机内存调整,建议物理内存的 1/8
# 内存中单个对象上限(一般 ≤ cache_mem / 10)
# maximum_object_size_in_memory 128 KB

# 磁盘缓存:大文件落盘(路径 /var/spool/squid,大小 2GB,16 个一级目录,256 个二级目录)
# cache_dir ufs /var/spool/squid/cache 2048 16 256
# 最大缓存对象 64MB
# maximum_object_size 64 MB
# 小于 1KB 的不缓存,节省 inode
# minimum_object_size 1 KB

# ========== 缓存策略(LRU 淘汰算法,开启缓存命中统计) ==========
# cache_replacement_policy lru
# memory_replacement_policy heap GDSF

# 哪些请求不缓存(动态 API 不缓存,节约磁盘)
# acl QUERY urlpath_regex -i cgi-bin \.php \.jsp \.asp
# cache deny QUERY
cache deny all

# ========== 连接优化(高并发内核参数) ==========
# 最大并发连接数(视硬件调整)
max_filedescriptors 32768
# 客户端空闲超时(保持长连接,减少握手开销)
client_lifetime 10 minutes
# 连接复用
pipeline_prefetch on
# 上游 keep-alive 复用
server_persistent_connections on
client_persistent_connections on
request_body_max_size 0

# ========== 日志格式(方便分析哪个内网 IP 在访问) ==========
logformat api_proxy %ts.%03tu %>a %ui %un "%rm %ru HTTP/%rv" %Hs %<st %tr %{Host}>h "%{User-Agent}>h" %Ss/%Sh %<A
access_log stdio:/var/log/squid/access.log api_proxy
cache_log stdio:/var/log/squid/cache.log

#
# Add any of your own refresh_pattern entries above these.
#
# refresh_pattern ^ftp:           1440    20%     10080
# refresh_pattern -i (/cgi-bin/|\?) 0     0%      0
# refresh_pattern .               0       20%     4320

初始化缓存

squid -z

验证配置

squid -k parse

启动

systemctl enable squid
systemctl start squid

配置代理

  • /etc/profile.d/proxy.sh

export http_proxy="http://proxy:3128"
export https_proxy="http://proxy:3128"
export HTTP_PROXY="http://proxy:3128"
export HTTPS_PROXY="http://proxy:3128"
export no_proxy="localhost,127.0.0.1,10.0.0.0/8,172.16.0.0/12,192.168.0.0/16,.internal"
export NO_PROXY="$no_proxy"

source /etc/profile.d/proxy.sh

  • 配置 java 代理

export JAVA_TOOL_OPTIONS="-Dhttp.proxyHost=proxy -Dhttp.proxyPort=3128 -Dhttps.proxyHost=proxy -Dhttps.proxyPort=3128 -Dhttp.nonProxyHosts=localhost|127.0.0.1|10.*|172.16.*|192.168.*"

评论