Nginx配置负载均衡

1、安装

# 运行 yum,安装相关依赖包
[root@single ~]# sudo yum -y install openssl openssl-devel pcre pcre-devel zlib zlib-devel gcc gcc-c++
# 将nginx-1.12.2.tar.gz 上传到/opt/download 下
[root@single download]# tar -zxf nginx-1.12.2.tar.gz -C /opt/software
[root@single software]# cd nginx-1.12.2/
# 进入解压缩目录,执行
[root@single nginx-1.12.2]# ./configure --prefix=/opt/module/nginx
[root@single nginx-1.12.2]# make && make install
[root@single nginx-1.12.2]# cd /opt/module/nginx/
# 启动 Nginx,在/opt/module/nginx/sbin 目录下执行 ./nginx
[root@single nginx]# cd sbin/
[root@single sbin]# ./nginx
# 查看启动情况
[root@single sbin]# ps -ef |grep nginx
root      26495      1  0 23:01 ?        00:00:00 nginx: master process ./nginx
nobody    26496  26495  0 23:01 ?        00:00:00 nginx: worker process
root      26498   9320  0 23:01 pts/1    00:00:00 grep --color=auto nginx

2、常用命令和问题

重启 Nginx
./nginx -s reload

关闭 Nginx
./nginx -s stop

通过配置文件启动,其中-c 是指定配置文件,而且配置文件路径必须指定绝对路径
./nginx -c /opt/module/nginx/conf/nginx.conf /opt/module/nginx/sbin/nginx -c /opt/module/nginx/conf/nginx.conf

配置检查:当修改 Nginx 配置文件后, 可以使用 Nginx 命令进行配置文件语法检查, 用于检查Nginx 配置文件是否正确 
/opt/module /nginx/sbin/nginx -c /opt/module/nginx/conf/nginx.conf –t

如果 80 端口号被占用 httpd 
sudo systemctl stop httpd
sudo systemctl disable httpd

部分机器启动时报错: 
/usr/local/nginx/sbin/nginx: error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory 
解决: ln -s/usr/local/lib/libpcre.so.1 /lib64

3、配置负载均衡

数据发给 nginx, 然后 nginx 再转发给我们的日志服务器

打开 nginx 配置文件

vi /opt/module/nginx/conf/nginx.conf 

修改如下配置

  • 增加upstream logcluster模块,其中logcluster为自定义的名字,内容为各个机器的负载权重
  • 修改location模块内容,配置:proxy_pass http://logcluster; proxy_connect_timeout 10;
http {
    upstream logcluster{
        server single:8081 weight=1;
        }
    server {
        listen       80;
        server_name  localhost;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;
        location / {
                proxy_pass http://logcluster;
                proxy_connect_timeout 10;
        }
# 其他省略
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值