nginx+squid多实例负载平衡(SMP)

系统构成:

Centos8 由于选择yum安装,所以尽量将空间分配给根目录,home保留一部分存放软件包的空间即可
nginx 1.14.1 (必须安装stream_module)
squid 4.4
关闭了firewalld
关闭了selinux

一,Nginx
1.1安装

centos8里面的版本不是特别旧,就直接使用yum安装了
yum install -y nginx
yum安装的好处是,自动安装关联软件,自动进行一些基础配置
log,config文件都在系统默认的位置
######1.2配置
开头不用修改

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

接下来添加steam模块,负责tcp转发功能,进而实现负载平衡
关于log的模板,可以选择记入log的变量很有限,参考以下资料
Module ngx_stream_core_module:Embedded Variables
http://nginx.org/en/docs/stream/ngx_stream_core_module.html
在这里没有找到代表访问URL的log参数,所以,如果需要访问记录的话,只能在squid的access.log中查看

stream {
    ## TCP 代理日志格式定义
    log_format tcp_proxy '$remote_addr [$time_local] '
                         '$protocol $status $bytes_sent $bytes_received '
                         '$session_time "$upstream_addr" '
                         '"$upstream_bytes_sent" "$upstream_bytes_received" "$upstream_connect_time"';


                         ## TCP 代理日志配置
    access_log /var/log/nginx/tcp-access.log tcp_proxy;
    open_log_file_cache off;

    #定义backend服务器集,名字为stream_backend
    #backend.squid.host:port是squid服务器的ip和端口
    #如果使用127.0.0.1的话,并且squid做了acl访问控制,需要允许127.0.0.1的访问
    #按照squid的文档说明,localhost已经预置到软件中,但是及时allow了localhost,不知道为什么没有生效,就没有继续研究
    upstream stream_backend {
        server backend1.squid.host:port1;
        server backend2.squid.host:port2;
        server backend3.squid.host:port3;
        server backend4.squid.host:port4;
        # ...
    }



    ## TCP 代理配置
    server {
       listen 80;#监听本机地址和端口,当使用keeplived的情况下使用keeplived VIP
       proxy_connect_timeout 1s;
       proxy_timeout 3s;
       proxy_protocol on;#开启了proxy_protocol,如果不开启后端squid接收的访问记录都是来自于nginx的ip地址,无法进行访问跟踪
       proxy_pass stream_backend; #这里填写对端的地址,负载平衡的话就填写upstream的名字
    }
}

剩下的http server部分,应该可以删除
但是为了测试nginx是否启动,选择了保留

upstream部分:
选择使用默认的轮询方式进行负载均衡。除了这个还有以下集中方式。
1.自定义权重的轮询模式
server backend1.example.com:12345 weight=5;
2.哈希计算模式
指定一个key来进行哈斯计算,计算的结果用来选择backend服务器
hash $remote_addr;

还有很多,详细参考官网文档
http://nginx.org/en/docs/stream/ngx_stream_upstream_module.html

1.3相关命令

systemctl enable nginx.service 加入开机启动
nginx 启动
nginx -s stop — fast shutdown
nginx -s quit — graceful shutdown
nginx -s reload — reloading the configuration file
nginx -s reopen — reopening the log files

1.4访问控制

nginx也可以进行访问控制,但是比较麻烦,只能一个一个添加到conf文件中

server {
    ...
    deny  192.168.1.1;
    allow 192.168.1.0/24;
    allow 10.1.1.0/16;
    allow 2001:0db8::/32;
    deny  all;
}

过于频繁的修改conf文件,很容易出现误操作,修改了别的参数,进而造成nginx故障,所以决定在squid中进行访问控制
这就需要nginx可以向squid提供客户端真实的ip地址
所以开始proxy_protocol
同时也需要squid开启接收proxy_protocol的选项

二,Squid
2.1安装

yum的squid版本是4.4,为了方便就使用yum安装了
yum install -y squid

2.2配置

配置的比较乱,很多配置在4.0以上版本的squid中已经是默认选项了,因为叫不准,就又写出来了。。。。
加大了cache memory

cache_mem 4096 MB
reload_into_ims on
cache_swap_low 90
cache_swap_high 95

定义允许访问squid的IP的对象

acl localnet src "/etc/squid/permitip.ws"

定义一些需要过滤的东西的对象

acl banned_s0 dstdomain "/etc/squid/banned.ws"
acl banned_files urlpath_regex -i /sex
acl permitip src "/etc/squid/permitip.ws" 
acl Banned_UserAgent browser "/etc/squid/banned_ug.ws"#针对userAgent的对象

允许localnet通过proxy_protocol访问squid

proxy_protocol_access allow localnet

允许或者拒绝其他对象

http_access deny banned_files
http_access deny banned_s0
http_access deny Banned_UserAgent
# 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
http_access allow permitip

# And finally deny all other access to this proxy
http_access deny all
icp_access allow all
# Squid normally listens to port 3128
#http_port localhost:400$
icp_port 0  #把icp关了,不取cache

下面这些都没改

logfile_rotate 8
#always_direct deny all
#never_direct allow all

cache_mgr root
cache_effective_user squid
cache_effective_group squid
visible_hostname proxy
hostname_aliases proxy  

这里是关键
启动4个squid进程,然后为了修改时尽量少改动主配置文件
添加了backend.conf这个配置文件
需要include到主config文件中

workers 4

include /etc/squid/backend.conf

以下是backend.conf的内容

#给squid加了一个上级代理
cache_peer 上层代理的IP parent 代理端口 ICP端口 login=用户名:密码 no-query default


# each backend must listen on a unique port
# without this the CARP algorithm would be useless
# 重点来了,为了显示客户端真实的ip,nginx和squid启用了proxy_protocol进行数据传递,这里就必须要加上require-proxy-header
# ${process_number}是将使用的处理器编号作为端口的一部分,进而来区分不同的squid实例,之前的主文件中定义了 [workers 4],所以会出现四个squid进程
http_port 0.0.0.0:400${process_number} require-proxy-header

# a 10 GB cache of small (up to 32KB) objects accessible by any backend worker
cache_dir rock /var/squid/cache/cacheRock 10240 max-size=32768

# NP: for now AUFS does not support SMP but the CARP algorithm helps reduce object duplications
# a 10 GB cache of large (over 32KB) objects per-worker
cache_dir aufs /var/squid/cache/cache${process_number} 10240 128 128 min-size=32769

# the default maximum cached object size is a bit small
# you want the backend to be able to cache some fairly large objects
maximum_object_size 512 MB

# you want the backend to have a small cache_mem
cache_mem 4 MB

# the backends require a different name to frontends, but can share one
# this prevents forwarding loops between backends while allowing
# frontend to forward via the backend
visible_hostname backend${process_number}.example.com

# change /var/log/squid to your own log directory
# 这里顶一个log的格式,名字叫做combined,默认的logformat显示的时间太恶心了,忘了是什么时间了,反正不是人看的时间
logformat combined %>a %ui %un [%tl] "%rm %ru HTTP/%rv" %Hs %<st "%{Referer}>h" "%{User-Agent}>h" %Ss:%
Sh %{host}>h


# 注释掉的部分,是原来的log,根据process的不同,分开记录的
# 在调试的时候可以清除的看到,是不是访问被分配给每一个process上的squid了,调试完成后,就可以将所有log合并显示了
#access_log /var/log/squid/backend${process_number}.access.log combined
#cache_log /var/log/squid/backend${process_number}.cache.log

access_log /var/log/squid/1.access.log combined
cache_log /var/log/squid/2.cache.log  combined

# add just enough access permissions to allow the frontend
http_access allow localhost


#always_direct deny all  这句加上就会有问题,等有时间再看看为什么
# 下面这句是将所有request都送到父代理中,不允许自己转发,当父代理失效的时候,这个子代理也无法正常访问网页
never_direct allow all

三,后记
Squid的问题

1,初始化你在 squid.conf 里配置的 cache 目录
#/usr/local/squid/sbin/squid -z //初始化缓存空间
如果有错误提示,请检查你的 cache目录的权限。
2,对你的squid.conf 排错,即验证 squid.conf 的 语法和配置。
#/usr/local/squid/sbin/squid -k parse
如果squid.conf 有语法或配置错误,这里会返回提示你,如果没有返回,恭喜,可以尝试启动squid。
3,在前台启动squid,并输出启动过程。
#/usr/local/squid/sbin/squid -N -d1
如果有到 ready to server reques,恭喜,启动成功。
然后 ctrl + c,停止squid,并以后台运行的方式启动它。
4,启动squid在后台运行。
#/usr/local/squid/sbin/squid -s
这时候可以 ps -A 来查看系统进程,可以看到俩个 squid 进程。
5,停止 squid
#/usr/local/squid/sbin/squid -k shutdown
————————————————
版权声明:本文为CSDN博主「乌托帮主」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/zhaozhanyong/article/details/6337928

squid在shutdown之后,可能需要一些时间才能完全停止,注意查看ps -e

Nginx

stream模块的话没有办法使用proxy_set_header和realip这些东西,不知道为什么官方文档在解释realip使用时,要把stream和http两个模块放在一起讲,以后再研究吧

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值