<article style="font-weight: 400;">
<header>
</header><!-- .entry-header -->
<div><div id="safari-reader-element-marker" style="position: relative; top: 97%;"></div>
<h1 class="clear">脑图</h1>
<h1 class="clear">Nginx</h1>
<p>nginx配置文件主要分为六个区域:</p>
<p><code>main(全局设置)</code>、<code>events(nginx工作模式)</code>、<code>http(http设置)</code>、</p>
<p><code>sever(主机设置)</code>、<code>location(URL匹配)</code>、<code>upstream(负载均衡服务器设置)</code></p>
<h2 class="clear">虚拟主机</h2>
<pre><code>
include /usr/local/nginx/conf/vhosts/*;
</code></pre>
<h2 class="clear">正向代理反向代理概念</h2>
<p>两者的区别在于代理的对象不一样:<strong>正向代理</strong>代理的对象是客户端,<strong>反向代理</strong>代理的对象是服务端</p>
<p><img decoding="async" src="https://upload-images.jianshu.io/upload_images/21597556-09e8e7970de3cc8e.png" data-src="https://chevereto.mypicbed.top:11443/images/2019/04/2019-04-21_08-24.png" alt="2019-04-21_08-24.png"></p>
<h2 class="clear">配置反向代理:</h2>
<pre><code>
server {
listen 80;
server_name nginx-01.xiaoniu.cn;
172.16.203.101/hello.html
location / {
root html;
proxy_pass http://192.168.0.21:8080;
}
}
</code></pre>
<h2 class="clear">负载均衡</h2>
<p>在http这个节下面配置一个叫upstream的,后面的名字可以随意取,但是要和location下的proxy_pass http://后的保持一致。</p>
<pre><code>
http {
upstream tomcats {
server 172.16.203.20:8080 weight=1;
server tomcat-02.xiaoniu.cn:8080 weight=1;
server tomcat-02.xiaoniu.cn:8080 weight=1;
}
location ~ .*.(jsp|do|action) {
proxy_pass http://tomcats;
}
}
</code></pre>
<h2 class="clear">配置动静分离:</h2>
<p>动态资源 index.jsp</p>
<pre><code>
location ~ .*.(jsp|do|action)$ {
proxy_pass http://tomcat-01.xiaoniu.cn:8080;
}
</code></pre>
<p>静态资源</p>
<pre><code>
location ~ .*.(html|js|css|gif|jpg|jpeg|png)$ {
expires 3d;
}
</code></pre>
<h2 class="clear">高可靠软件keepalived</h2>
<p>keepalive是一款可以实现高可靠的软件,通常部署在2台服务器上,分为一主一备。Keepalived可以对本机上的进程进行检测,一旦Master检测出某个进程出现问题,将自己切换成Backup状态,然后通知另外一个节点切换成Master状态。</p>
<p><img decoding="async" src="https://upload-images.jianshu.io/upload_images/21597556-3e88fba49effa729.png" data-src="https://chevereto.mypicbed.top:11443/images/2019/04/2019-04-21_08-39.png" alt="2019-04-21_08-39.png"> ^8q5wnv</p>
<p><img decoding="async" src="https://upload-images.jianshu.io/upload_images/21597556-7dab690140730eb4.png" data-src="https://chevereto.mypicbed.top:11443/images/2019/04/2019-04-22_00-18.png" alt="2019-04-22_00-18.png"></p>
<h3 class="clear">keepalived安装</h3>
<p>下载keepalived官网:http://keepalived.org</p>
<p>将keepalived解压到/usr/local/src目录下</p>
<pre><code>
tar -zxvf keepalived-1.3.6.tar.gz -C /usr/local/src
</code></pre>
<p>进入到/usr/local/src/keepalived-1.3.6目录</p>
<pre><code>
cd /usr/local/src/keepalived-1.3.6
</code></pre>
<p>开始configure</p>
<pre><code>
./configure
</code></pre>
<p>编译并安装</p>
<pre><code>
make && make install
</code></pre>
<h3 class="clear">将keepalived添加到系统服务中</h3>
<p>拷贝执行文件</p>
<p>将init.d文件拷贝到etc下,加入开机启动项</p>
<p>将keepalived文件拷贝到etc下</p>
<pre><code>
cp /usr/local/src/keepalived-1.3.6/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
</code></pre>
<p>创建keepalived文件夹</p>
<pre><code>
mkdir -p /etc/keepalived
</code></pre>
<p>将keepalived配置文件拷贝到etc下</p>
<pre><code>
cp /usr/local/src/keepalived-1.3.6/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf
</code></pre>
<p>添加可执行权限</p>
<pre><code>
chmod +x /etc/init.d/keepalived
</code></pre>
<p>添加keepalived到开机启动</p>
<pre><code>
chkconfig --add keepalived
chkconfig keepalived on
</code></pre>
<h3 class="clear">配置keepalived虚拟IP</h3>
<p>MASTER节点</p>
<pre><code>
global_defs {}vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 10.0.0.10/24 }}
</code></pre>
<p>BACKUP节点</p>
<pre><code>
global_defs {}vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 51 priority 99 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 10.0.0.10/24 }}
</code></pre>
<p>分别启动两台机器上的keepalived</p>
<pre><code>
service keepalived start
</code></pre>
<h3 class="clear">配置keepalived心跳检查</h3>
<p>MASTER节点</p>
<pre><code>
global_defs {} vrrp_script chk_health { script "[[ ps -ef | grep nginx | grep -v grep | wc -l
-ge 2 ]] && exit 0 || exit 1" interval 1 weight -2} vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 1 priority 100 advert_int 2 authentication { auth_type PASS auth_pass 1111 } track_script { chk_health } virtual_ipaddress { 10.0.0.10/24 } notify_master "/usr/local/keepalived/sbin/notify.sh master" notify_backup "/usr/local/keepalived/sbin/notify.sh backup" notify_fault "/usr/local/keepalived/sbin/notify.sh fault"}
</code></pre>
<p>添加切换通知脚本</p>
<pre><code>
vi /usr/local/keepalived/sbin/notify.sh
!/bin/bash case "$1" in master) /usr/local/nginx/sbin/nginx exit 0 ;;backup) /usr/local/nginx/sbin/nginx -s stop /usr/local/nginx/sbin/nginx exit 0 ;; fault) /usr/local/nginx/sbin/nginx -s stop exit 0 ;; *) echo 'Usage: notify.sh {master|backup|fault}' exit 1 ;;esac
</code></pre>
<p>添加执行权限</p>
<pre><code>
chmod +x /usr/local/keepalived/sbin/notify.sh
global_defs {} vrrp_script chk_health { script "[[ ps -ef | grep nginx | grep -v grep | wc -l
-ge 2 ]] && exit 0 || exit 1" interval 1 weight -2} vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 1 priority 99 advert_int 1 authentication { auth_type PASS auth_pass 1111 } track_script { chk_health } virtual_ipaddress { 10.0.0.10/24 } notify_master "/usr/local/keepalived/sbin/notify.sh master" notify_backup "/usr/local/keepalived/sbin/notify.sh backup" notify_fault "/usr/local/keepalived/sbin/notify.sh fault"}
</code></pre>
<p>在第二台机器上添加notify.sh脚本</p>
<p>分别在两台机器上启动keepalived</p>
<pre><code>
service keepalived start
chkconfig keepalived on
</code></pre>
<h2 class="clear">Nginx增加插件模块</h2>
<p>安装依赖:kafkaC客户端</p>
<pre><code>
git clone https://github.com/edenhill/librdkafka
cd librdkafka
./configure
make
sudo make install
</code></pre>
<p>重新编译nginx把插件编译进去:等于重装</p>
<pre><code>
git clone https://github.com/brg-liuwei/ngx_kafka_module
如果没有configure就要重新下载nginx源码包
cd /path/to/nginx
./configure --add-dynamic-module=/path/to/ngx_kafka_module
sudo make
sudo make install
或者:只make,然后把objs/nginx拷贝到nginx安装目录去.
</code></pre>
<p>然后在nginx.conf中第一行增加</p>
<pre><code>
load_module /usr/local/nginx/modules/ngx_http_kafka_module.so;
</code></pre>
<p>sudo nginx -t 如果报:</p>
<pre><code>
nginx: [emerg] dlopen() "/usr/local/nginx/modules/ngx_http_kafka_module.so" failed (librdkafka.so.1: cannot open shared object file: No such file or directory)
</code></pre>
<p>需要做动态链接库的软连:不要做到/bin/下而是/lib/下</p>
<pre><code>
sudo ln -s /usr/local/lib/librdkafka.so /lib/librdkafka.so.1
</code></pre>
<p>重新加载配置:</p>
<pre><code>
sudo nginx -s reload
</code></pre>
<h1 class="clear">引用</h1>
<h1 class="clear">社交</h1>
<p>每日都有工作和生活记录,赏一个
<img decoding="async" src="https://upload-images.jianshu.io/upload_images/21597556-f8bd3684d9a5deb0.png" data-src="https://chevereto.mypicbed.top:11443/images/2020/03/03/a73c4baf7cf83500f0e952e513e97551.png" alt="收款码" data-was-processed="true"></p>
</div>
<div>
<h2 class="clear">发布者</h2>
<div>
<h3>majia</h3>
<p>
本人计算机专业毕业10年程序员,承接电脑组装,网络维修,电商代购,网站建设,数据0-1及1-10的建设和支持,it技术咨询,家用NAS搭建,it教育培训的单子 <a href="https://majiablog.cn/author/majia/" rel="author" target="_top">
查看所有由majia发布的文章 </a>
</p><!-- .author-bio -->
</div><!-- .author-description -->
</div>
<!-- .entry-footer -->
</article><blockquote><p>本文使用 <a href="https://www.jianshu.com/p/5709df6fb58d" class="internal">文章同步助手</a> 同步</p></blockquote>