技术改变生活

博客园 首页 新随笔 联系 订阅 管理

一、nginx安装

 

下载nginx软件包
[root@x112 ~]# wget -c http://nginx.org/download/nginx-1.12.2.tar.gz
[root@x112 ~]#tar -xf nginx-1.12.2.tar.gz
[root@x112 ~]#cd nginx-1.12.2


安装nginx所需依赖包
[root@x112 nginx-1.12.2]#yum -y install pcre zlib-devel  openssl gcc 

预编译、编译、安装
[root@x112 nginx-1.12.2]#./configure --user=nginx --group=nginx
[root@x112 nginx-1.12.2]# make && make install
[root@x112 nginx-1.12.2]#cd /usr/local/nginx/sbin
[root@x112 nginx-1.12.2]#ln -s /usr/local/nginx/sbin/nginx /usr/bin/
[root@x112 nginx-1.12.2]#nginx 

查看nginx版本信息
[root@x112 nginx-1.12.2]#nginx -V
nginx version: nginx/1.12.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) 
configure arguments: --user=nginx --group=nginx

  

 

二、nginx反向代理

[root@x112 ~]# cd /usr/local/nginx/conf/

[root@x112 conf]# ls
fastcgi.conf          fastcgi_params.default  mime.types          nginx.conf.default   uwsgi_params
fastcgi.conf.default  koi-utf                 mime.types.default  scgi_params          uwsgi_params.default
fastcgi_params        koi-win                 nginx.conf          scgi_params.default  win-utf


反向代理,添加upstream集群模块,server虚拟主机中添加关键字proxy_pass指向后端集群模块名称
[root@x112 conf]# vim nginx.conf

..............

http {
    include       mime.types;
    default_type  application/octet-stream;

   
    sendfile        on;

    keepalive_timeout  65;


   upstream webserver {
        server  192.168.2.13:80;
  }
    server {
        listen       81;
        server_name  localhost;
       charset utf-8;


        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
                proxy_pass http://webserver;
        }

  

  

 

三、nginx版本升级

 

[root@x112 conf]# nginx -V
nginx version: nginx/1.12.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) 
configure arguments: --user=nginx --group=nginx
[root@x112 conf]# cd ..
[root@x112 software]# ls
nginx-1.12.2  nginx-1.12.2.tar.gz  nginx-1.14.2  nginx-1.14.2.tar.gz
[root@x112 software]# cd nginx-1.14.2/
[root@x112 nginx-1.14.2]# ls
auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  man  README  src


开始编译高版本的nginx,注意编译参数与旧版本的保持一致
[root@x112 nginx-1.14.2]# ./configure --user=nginx --group=nginx
checking for OS
 + Linux 3.10.0-693.el7.x86_64 x86_64
checking for C compiler ... found
 + using GNU C compiler
 + gcc version: 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) 
checking for gcc -pipe switch ... found
checking for -Wl,-E switch ... found
checking for gcc builtin atomic operations ... found
checking for C99 variadic macros ... found
checking for gcc variadic macros ... found
.........................

[root@x112 nginx-1.14.2]# make 
make -f objs/Makefile
make[1]: Entering directory `/root/linshi/software/nginx-1.14.2'
cc -c -pipe  -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g  -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \
	-o objs/src/core/nginx.o \
	src/core/nginx.c
cc -c -pipe  -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g  -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \
	-o objs/src/core/ngx_log.o \

[root@x112 nginx-1.14.2]# ls 
auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  Makefile  man  objs  README  src
[root@x112 nginx-1.14.2]# ls objs/
autoconf.err  nginx    ngx_auto_config.h   ngx_modules.c  src
Makefile      nginx.8  ngx_auto_headers.h  ngx_modules.o


备份现有版本的nginx
[root@x112 nginx-1.14.2]# mv /usr/local/nginx/sbin/{nginx,_bak}
[root@x112 nginx-1.14.2]# ls /usr/local/nginx/sbin/
 nginx_bak


拷贝编译后的高版本的nginx,替换原有的旧版本nginx
[root@x112 nginx-1.14.2]# cp objs/nginx /usr/local/nginx/sbin/
[root@x112 objs]# nginx 
[root@x112 objs]# 
[root@x112 objs]# ps -ef | grep nginx
root       9164      1  0 10:47 ?        00:00:00 nginx: master process nginx
nginx      9165   9164  0 10:47 ?        00:00:00 nginx: worker process
root       9171   1248  0 10:47 pts/1    00:00:00 grep --color=auto nginx
[root@x112 objs]# nginx -V
nginx version: nginx/1.14.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) 
configure arguments: --user=nginx --group=nginx
[root@x112 objs]# 

  

四、nginx高可用,keepalive配置

[root@x112 linshi]# vim /etc/keepalived/keepalived.conf 
! Configuration File for keepalived

global_defs {
   notification_email {
     acassen@firewall.loc
     failover@firewall.loc
     sysadmin@firewall.loc
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
   vrrp_skip_check_adv_addr
#   vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}
vrrp_script check_nginx{
	script "/etc/keepalived/chk_nginx.sh"
	interval 2
}

vrrp_instance VI_1 {
    state MASTER
    interface ens33
    virtual_router_id 51
    priority 150
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
	track_script {
		check_nginx	
	}
    virtual_ipaddress {
        192.168.0.66
    }
}

我们可以通过检测80端口是否开启来判定Nginx的运行情况,2秒钟检测一次,脚本如下
[root@x112 linshi]#vim /etc/keepalived/chk_nginx.sh
#!/bin/bash
while true
do
if [ $(netstat -tlnp|grep nginx|wc -l) -ne 1 ]
then
    /etc/init.d/keepalived stop
fi
sleep 2
done

  

  

  

posted on 2020-08-13 11:17  小阿峰  阅读(153)  评论(0编辑  收藏  举报