Nginx大型集群部署步骤及调优

环境准备

nginx前端调度服务器两台:
nginx1:192.168.0.1
nginx2:192.168.0.2
nginx后端页面服务器8台:
web1:192.168.0.10
web2:192.168.0.11
web3:192.168.0.12
web4:192.168.0.13
web5:192.168.0.14
web6:192.168.0.15
web7:192.168.0.16
web8:192.168.0.17
keepaliveip一个
keepalive:192.168.0.3

ansible准备

利用ansible搭建更快速
1.首先主机安装ansible
yum -y install ansible
2.配置hosts地址
vim /etc/ansible/hosts

[nginx]
nginx[1:2]
[web]
web[1:8]
[web-php]
web[1:4]
[web-nginx]
web[5:8]

注:ssh端口全部为22,否则需要另配置端口信息
3.配置ansible机的hosts地址
vim /etc/hosts

192.168.0.1 nginx1
192.168.0.2 nginx2
192.168.0.10 web1
192.168.0.11 web2
192.168.0.12 web3
192.168.0.13 web4
192.168.0.14 web5
192.168.0.15 web6
192.168.0.16 web7
192.168.0.17 web8

4.配置公私秘钥
ssh-keygen -t rsa -b 4096
其中id_rsa是私钥
id_rsa.pub这个是公钥

将公钥复制到客户机并创建 authorized_keys 权限为600
并修改 /etc/ssh/sshd_config

RSAAuthentication yes
PubkeyAuthentication yes

PasswordAuthentication no

配置ansible首次登录不输入yes
vim ansible.cfg

host_key_checking = False  

5.测试ansible环境准备
ansible web -m ping
全部返回为绿为通,单机故障单机检查.

本机配置yum仓库,并制作nginx的rpm包(基本操作,这里不写了)

将配置好的repo文件传给所有主机并安装nginx

ansible all -m copy -a 'src=**.repo dest=/etc/yum.repo.d/'

6.将编辑好的nginx.conf传送到代理服务器上

ansible nginx -m copy -a '/usr/local/nginx/conf/'

配置如下

http {
.. ..
#使用upstream定义后端服务器集群,集群名称任意(如webserver)
#使用server定义集群中的具体服务器和端口
upstream webserver {
#通过ip_hash设置调度规则为:相同客户端访问相同服务器
#ip_hash;
#server 192.168.0.10:weight=1 max_fails=1 fail_timeout=30;
                server 192.168.0.10:80;
                server 192.168.0.11:80;
                server 192.168.0.12:80;
                server 192.168.0.13:80;
                server 192.168.0.14:80;
                server 192.168.0.15:80;
                server 192.168.0.16:80;
                server 192.168.0.17:80;
                
#weight设置服务器权重值,默认值为1
#max_fails设置最大失败次数
#fail_timeout设置失败超时时间,单位为秒
#down标记服务器已关机,不参与集群调度
        }
.. ..
server {
        listen        80;
        server_name  localhost;
            location / {
#通过proxy_pass将用户的请求转发给webserver集群
            proxy_pass http://webserver;
        }
}

6.重启nginx

/usr/local/nginx/sbin/nginx -s reload

7.利用curl进行访问测试

8.后端web服务器配置
后端服务器都需要安装nginx,前四台安装php
前4台需要安装的软件包:
nginx,php,php-fpm,php-mysql
后四台需要安装nginx

前4台服务器需要配置的文件nginx.conf

location / {
            root   html;
            index  index.php  index.html   index.htm;
#设置默认首页为index.php,当用户在浏览器地址栏中只写域名或IP,不说访问什么页面时,服务器会把默认首页index.php返回给用户
        }
 location  ~  \.php$  {
            root           html;
            fastcgi_pass   127.0.0.1:9000;    #将请求转发给本机9000端口,PHP解释器
            fastcgi_index  index.php;
            #fastcgi_param   SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi.conf;
        }

创建php测试页面:
vim /usr/local/nginx/html/test1.php

<?php
$i="This is a test Page";
echo $i;
?>

调优配置:

nginx.conf文件


http {
client_header_buffer_size    1k;        //默认请求包头信息的缓存    
large_client_header_buffers  4 4k;        //大请求包头部信息的缓存个数与容量
gzip on;                            //开启压缩
gzip_min_length 1000;                //小文件不压缩
gzip_comp_level 4;                //压缩比率
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
                                    //对特定文件压缩,类型参考mime.types
 open_file_cache          max=2000  inactive=20s;
 open_file_cache_valid    60s;
 open_file_cache_min_uses 5;
 open_file_cache_errors   off;
//设置服务器最大缓存2000个文件句柄,关闭20秒内无请求的文件句柄
//文件句柄的有效时间是60秒,60秒后过期
//只有访问次数超过5次会被缓存
.. ..
}

worker_processes  2;                  ##与CPU核心数量一致
events {
worker_connections 65535;        ##每个worker最大并发连接数
use epoll;
}

server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }
        
location ~* \.(jpg|jpeg|gif|png|css|js|ico|xml)$ {
expires        30d;            //定义客户端缓存时间为30天
}
error_page   404  /40x.html;    //自定义错误页面

优化内核:
ulimit -a //查看所有属性值
ulimit -Hn 100000 //设置硬限制(临时规则)
ulimit -Sn 100000 //设置软限制(临时规则)
vim /etc/security/limits.conf
… …

  •           soft    nofile            100000
    
  •           hard    nofile            100000
    

调优配置必须重启nginx

日志切割

1.手动执行
mv access.log access2.log
kill -USR1 $(cat /usr/local/nginx/logs/nginx.pid)
备注:/usr/local/nginx/logs/nginx.pid文件中存放的是nginx的进程PID号。

自动完成
vim /usr/local/nginx/logbak.sh

#!/bin/bash
date=`date +%Y%m%d`
logpath=/usr/local/nginx/logs
mv $logpath/access.log $logpath/access-$date.log
mv $logpath/error.log $logpath/error-$date.log
kill -USR1 $(cat $logpath/nginx.pid)

crontab -e
03 03 * * 5 /usr/local/nginx/logbak.sh

附加

查看服务器状态:
编译安装时需要添加模块–with-http_stub_status_module
修改配置文件定义状态页面
vim /usr/local/nginx/conf/nginx.conf

… …
location /status {
                stub_status on;
                 #allow IP地址;
                 #deny IP地址;
        }
… …

重启nginx
curl http://ip/status 可以看见访问信息
具体内容如下:
Active connections:当前活动的连接数量。
Accepts:已经接受客户端的连接总数量。
Handled:已经处理客户端的连接总数量。
(一般与accepts一致,除非服务器限制了连接数量)。
Requests:客户端发送的请求数量。
Reading:当前服务器正在读取客户端请求头的数量。
Writing:当前服务器正在写响应信息的数量。
Waiting:当前多少客户端在等待服务器的响应。

地址重写

vim /usr/local/nginx/conf/nginx.conf


server {
        listen       80;
        server_name  localhost;
#rewrite /a.html  /b.html;            
#访问a.html重定向到b.html
或
#rewrite /a.html  /b.html  redirect;            
#访问a.html重定向到b.html(跳转地址栏)
或者
#访问192.168.0.5的请求重定向至www.ccc.cn
#rewrite ^/ http://www.ccc.cn/;
或
#rewrite ^/(.*)$ http://www.dd.cn/$1;
#修改配置文件(访问192.168.0.5/下面子页面,重定向至www.dd.cn/下相同的页面)
location / {
    root   html;
index  index.html index.htm;
}
#这里,~符号代表正则匹配,*符号代表不区分大小写
if ($http_user_agent ~* firefox) {            //识别客户端firefox浏览器
rewrite ^(.*)$ /firefox/$1;
#实现curl和火狐访问相同链接返回的页面不同
}

地址重写格式【总结】
rewrite 旧地址 新地址 [选项];
last 不再读其他rewrite
break 不再读其他语句,结束请求
redirect 临时重定向
permament 永久重定向

猜你喜欢

转载自blog.csdn.net/m0_38139137/article/details/90511071
今日推荐