lamp架构--Nginx配置管理、负载均衡、平滑升级及限流;日志可视化&tomcat结合memcache


一、nginx实现负载均衡

1 2、3台机子安装httpd服务,并为实验方便写好易区分的测试页
yum install httpd -y
systemctl enable --now httpd
echo server2 > /var/www/html/index.html
echo server3 > /var/www/html/index.html

events {
    use epoll;
    worker_connections 65535;
}
http {
     upstream westos {
     server 172.25.56.12:80;
     server 172.25.56.13:80;
     }
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '

....

        #location / {
        #    root   html;
        #    index  index.php index.html index.htm;
        #
        #}
        location / {
             proxy_pass http://westos;
        }

在这里插入图片描述

在这里插入图片描述
nginx -t
nginx

访问测试:
在这里插入图片描述

二、Nginx的平滑升级

备份源程序:

[root@server1 nginx-1.19.1]# cd /usr/local/nginx/sbin/
[root@server1 sbin]# ls
nginx
[root@server1 sbin]# mv nginx nginx.old
[root@server1 sbin]# ls
nginx.old

拷贝新程序:

[root@server1 ~]# cd nginx-1.19.1/
[root@server1 nginx-1.19.1]# cd objs/
[root@server1 objs]# ls
autoconf.err  nginx    ngx_auto_config.h   ngx_modules.c  src
Makefile      nginx.8  ngx_auto_headers.h  ngx_modules.o
[root@server1 objs]# cp -f nginx /usr/local/nginx/sbin/
[root@server1 objs]# cd /usr/local/nginx/sbin/
[root@server1 sbin]# ls
nginx  nginx.old
[root@server1 sbin]# ./nginx -v
nginx version: nginx/1.19.1
[root@server1 sbin]# ./nginx.old -v
nginx version: nginx/1.18.0
[root@server1 sbin]# ps ax | grep nginx
 3191 ?        Ss     0:00 nginx: master process /usr/local/nginx/sbin/nginx
 3192 ?        S      0:00 nginx: worker process
 6223 pts/0    S+     0:00 grep --color=auto nginx

在这里插入图片描述

[root@server1 sbin]# ps ax | grep nginx
 3191 ?        Ss     0:00 nginx: master process /usr/local/nginx/sbin/nginx
 3192 ?        S      0:00 nginx: worker process
 6231 pts/0    S+     0:00 grep --color=auto nginx
[root@server1 sbin]# kill -USR2  3191 
[root@server1 sbin]# ps ax | grep nginx
 3191 ?        Ss     0:00 nginx: master process /usr/local/nginx/sbin/nginx
 3192 ?        S      0:00 nginx: worker process
 6232 ?        S      0:00 nginx: master process /usr/local/nginx/sbin/nginx
 6233 ?        S      0:00 nginx: worker process
 6235 pts/0    S+     0:00 grep --color=auto nginx
 关闭原woker进程但保留主进程:为了回退
[root@server1 sbin]# kill -WINCH  3191
[root@server1 sbin]# ps ax | grep nginx
 3191 ?        Ss     0:00 nginx: master process /usr/local/nginx/sbin/nginx
 6232 ?        S      0:00 nginx: master process /usr/local/nginx/sbin/nginx
 6233 ?        S      0:00 nginx: worker process
 6237 pts/0    R+     0:00 grep --color=auto nginx

在这里插入图片描述

在这里插入图片描述
版本回退:

[root@server1 ~]# cd /usr/local/nginx/sbin/
[root@server1 sbin]# ls
nginx  nginx.old
[root@server1 sbin]# mv nginx nginx.new
[root@server1 sbin]# mv nginx.old nginx
[root@server1 sbin]# ls
nginx  nginx.new
[root@server1 sbin]# ps ax | grep nginx
 3191 ?        Ss     0:00 nginx: master process /usr/local/nginx/sbin/nginx
 6232 ?        S      0:00 nginx: master process /usr/local/nginx/sbin/nginx
 6233 ?        S      0:00 nginx: worker process
 6266 pts/0    S+     0:00 grep --color=auto nginx
[root@server1 sbin]# kill -HUP  3191
[root@server1 sbin]# ps ax | grep nginx
 3191 ?        Ss     0:00 nginx: master process /usr/local/nginx/sbin/nginx
 6232 ?        S      0:00 nginx: master process /usr/local/nginx/sbin/nginx
 6233 ?        S      0:00 nginx: worker process
 6267 ?        S      0:00 nginx: worker process
 6269 pts/0    S+     0:00 grep --color=auto nginx
[root@server1 sbin]# kill -WINCH 6232
[root@server1 sbin]# ps ax | grep nginx
 3191 ?        Ss     0:00 nginx: master process /usr/local/nginx/sbin/nginx
 6232 ?        S      0:00 nginx: master process /usr/local/nginx/sbin/nginx
 6267 ?        S      0:00 nginx: worker process
 6271 pts/0    S+     0:00 grep --color=auto nginx

在这里插入图片描述
在这里插入图片描述

三、添加sticky模块

[root@server1 ~]# ls
 nginx-goodies-nginx-sticky-module-ng-08a395c66e42
goaccess-1.4                         nginx-goodies-nginx-sticky-module-ng-08a395c66e42.zip nginx-1.18.0 
[root@server1 ~]# cd nginx-1.18.0/
[root@server1 nginx-1.18.0]# ls
[root@server1 nginx-1.18.0]#  ./configure  --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --add-module=/root/nginx-goodies-nginx-sticky-module-ng-08a395c66e42
 [root@server1 nginx-1.18.0]# make
[root@server1 nginx-1.18.0]# cd objs/
[root@server1 objs]# ls
addon         Makefile  nginx.8            ngx_auto_headers.h  ngx_modules.o
autoconf.err  nginx     ngx_auto_config.h  ngx_modules.c       src
[root@server1 objs]# cp nginx /usr/local/nginx/sbin/
[root@server1 objs]# nginx -v
nginx version: nginx/1.18.0
[root@server1 conf]# vim nginx.conf
http {
     upstream westos {
     sticky;
     server 172.25.56.12:80;
     server 172.25.56.13:80;
     }
[root@server1 conf]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful


语法检测没问题就表明添加成功!

四、Nginx配置

[root@server1 conf]# cat nginx.conf
#user  nobody;
user  nginx nginx;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    use epoll;
    worker_connections 65535;
}


http {
     upstream westos {
     server 172.25.56.12:80;
     server 172.25.56.13:80;
     }
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.html;
        
        }
#        location / {
#             proxy_pass http://westos;
#        }
#
        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            #fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            include        fastcgi.conf;
        }

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    server {
        listen       443 ssl;
        server_name  localhost;

        ssl_certificate      cert.pem;
        ssl_certificate_key  cert.pem;

        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;

        ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers  on;

        location / {
            root   html;
            index  index.html index.htm;
        }
    }


server {
        listen 80;
        server_name     www.westos.org;

        location / {
        proxy_pass http://westos;
        }
}

server {
        listen 80;
        server_name     www.linux.org;

        location / {
                root  /web1;
                index   index.html;
        }
}
}


[root@server1 certs]# pwd
/etc/pki/tls/certs
[root@server1 certs]# make cert.pem
umask 77 ; \
PEM1=`/bin/mktemp /tmp/openssl.XXXXXX` ; \
PEM2=`/bin/mktemp /tmp/openssl.XXXXXX` ; \
/usr/bin/openssl req -utf8 -newkey rsa:2048 -keyout $PEM1 -nodes -x509 -days 365 -out $PEM2  ; \
cat $PEM1 >  cert.pem ; \
echo ""    >> cert.pem ; \
cat $PEM2 >> cert.pem ; \
rm -f $PEM1 $PEM2
Generating a 2048 bit RSA private key
.........................................................................................................................+++
..........................................................................................................................+++
writing new private key to '/tmp/openssl.b12t28'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:cn
State or Province Name (full name) []:shaanxi
Locality Name (eg, city) [Default City]:xi'an
Organization Name (eg, company) [Default Company Ltd]:westos
Organizational Unit Name (eg, section) []:linux
Common Name (eg, your name or your server's hostname) []:server1
Email Address []:root@westos.org
[root@server1 certs]# ls
ca-bundle.crt  ca-bundle.trust.crt  cert.pem  make-dummy-cert  Makefile  renew-dummy-cert
[root@server1 certs]# mv cert.pem /usr/local/nginx/conf/
[root@server1 conf]# mkdir /web1
[root@server1 conf]# cd /web1/
[root@server1 web1]# echo web1 > index.html
[root@server1 conf]#nginx -t
[root@server1 conf]#nginx -s reload

访问不同域名有不同测试页
访问Ip为源测试页
在这里插入图片描述
在这里插入图片描述

自动索引

在这里插入图片描述
nginx -s reload
在这里插入图片描述

在这里插入图片描述

 location ~ .*\.(gif|jpg|png)$ {
           expires 365d;
           root html;
         }

nginx -s reload
在这里插入图片描述

日志轮询

[root@server1 logs]# date +%F -d -1day
2021-04-02
[root@server1 logs]# ls
access.log  error.log  nginx.pid  nginx.pid.oldbin
[root@server1 logs]# cd /opt/
[root@server1 opt]# vim nginx_log.sh
[root@server1 opt]# chmod +x  nginx_log.sh
[root@server1 opt]# ./nginx_log.sh 
[root@server1 opt]# cd /usr/local/nginx/logs/
[root@server1 logs]# ls
access_2021-04-03.log  access.log  error.log  nginx.pid  nginx.pid.oldbin
[root@server1 opt]# cat nginx_log.sh 
#!/bin/bash
cd /usr/local/nginx/logs && mv access.log access_$(date +%F -d -1day).log
kill -USR1 `cat /usr/local/nginx/logs/nginx.pid`

禁用不必要的日志采集,减少内存消耗
在这里插入图片描述

站点目录和文件的限制

在这里插入图片描述
此时无法访问
在这里插入图片描述

中文乱码

在这里插入图片描述
出现乱码现象:
在这里插入图片描述
如何修复?
加入以下参数
在这里插入图片描述
nginx -s reload
在这里插入图片描述

防止域名恶意解析到服务器IP&重定向

在这里插入图片描述
nginx -s reload
在这里插入图片描述
重定向:
在这里插入图片描述
rewrite ^(.*) http://www.westos.org permanent;
nginx -s reload

访问被重定向到指定路径
在这里插入图片描述

80端口定向到443端口

在这里插入图片描述
rewrite ^/(.*)$ https://www.westos.org/$1 permanent;
nginx -s reload
在这里插入图片描述

五、Nginx限流

官方文档查看!
在这里插入图片描述
在这里插入图片描述

[root@server1 html]# mkdir download
[root@server1 html]# ls
50x.html  download  example.php  index.html  index.php  memcache.php  phpadmin  test.html
[root@server1 html]# cd download/
lftp 172.25.254.250:/pub/docs> get vim.jpg 
453575 bytes transferred                    
[root@server1 download]# ls
vim.jpg
[root@server1 download]# du - h *
444	vim.jpg

在这里插入图片描述
限制带宽:
在这里插入图片描述
444M 限速50K,大约8秒
在这里插入图片描述
限制单位时间内请求次数:
在这里插入图片描述
在这里插入图片描述
压测:一秒只处理一次请求,其他9次被拒绝
在这里插入图片描述
可处理5个
When the zone is full, excessive requests will be queued (burst), the size of this queue is 5 requests. Request processing in the queue is delayed in such a way that the overall rate is not greater than specified. Requests above the burst limit will be rejected with the 503 error.
在这里插入图片描述

nginx -s reload

进行排队,处理一个请求要1秒
在这里插入图片描述
不等待,只处理5个
在这里插入图片描述
压测:
在这里插入图片描述

六、goccess日志可视化

[root@server1 ~]#yum install GeoIP-devel-1.5.0-13.el7.x86_64.rpm 
[root@server1 ~]# cd goaccess-1.4/
[root@server1 goaccess-1.4]# ls
ABOUT-NLS   config        configure.ac  install-sh   NEWS       TODO
aclocal.m4  config.guess  COPYING       m4           po
AUTHORS     config.rpath  depcomp       Makefile.am  README
ChangeLog   config.sub    goaccess.1    Makefile.in  resources
compile     configure     INSTALL       missing      src
[root@server1 goaccess-1.4]# ./configure --enable-utf8 --enable-geoip=legacy
[root@server1 goaccess-1.4]# make && make install
[root@server1 logs]# goaccess access.log -o /usr/local/nginx/html/report.html --log-format=COMBINED --real-time-html

访问测试:
在这里插入图片描述

七、tomcat结合memcache

2、3上均部署

[root@server2 ~]# ls
apache-tomcat-7.0.37.tar.gz  jdk-8u121-linux-x64.rpm
[root@server2 ~]# rpm -ivh  jdk-8u121-linux-x64.rpm 
[root@server2 ~]# tar zxf apache-tomcat-7.0.37.tar.gz 
[root@server2 ~]# mv apache-tomcat-7.0.37 /usr/local/tomcat
[root@server2 ~]# cd /usr/local/tomcat
[root@server2 tomcat]# ls
bin   lib      logs    RELEASE-NOTES  temp     work
conf  LICENSE  NOTICE  RUNNING.txt    webapps
[root@server2 tomcat]# bin/startup.sh 
Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
[root@server2 tomcat]# netstat -antlupe | grep :8080
tcp6       0      0 :::8080                 :::*                    LISTEN      0          36876      4172/java      

在这里插入图片描述
server3上类似

我们想实现client–>nginx:80–>*.jsp–>tomcat
在这里插入图片描述

在这里插入图片描述
nginx -t
nginx -s reload
在这里插入图片描述

[root@server2 ROOT]# ls | grep test
test.jsp
[root@server2 ROOT]# scp test.jsp server3:/usr/local/tomcat/webapps/ROOT/
root@server3's password: 
test.jsp                                      100%  968     1.1MB/s   00:00   

访问测试,默认是轮询的方式 12、13切换
每次递交原本信息会被覆盖
在这里插入图片描述
修复:
在这里插入图片描述
nginx -t
nginx -s reload
在这里插入图片描述
Session:

[root@server2 ~]# ls
apache-tomcat-7.0.37.tar.gz  jar  jdk-8u121-linux-x64.rpm
[root@server2 ~]# cd jar/
[root@server2 jar]# ls
asm-3.2.jar                              memcached-session-manager-tc7-1.6.3.jar
kryo-1.04.jar                            minlog-1.2.jar
kryo-serializers-0.10.jar                msm-kryo-serializer-1.6.3.jar
memcached-session-manager-1.6.3.jar      reflectasm-1.01.jar
memcached-session-manager-tc6-1.6.3.jar  spymemcached-2.7.3.jar
[root@server2 jar]# rm -fr memcached-session-manager-1.6.3.jar 
[root@server2 jar]# cp * /usr/local/tomcat/lib/
[root@server2 jar]# cd /usr/local/tomcat/lib/
[root@server2 lib]# cd ..
[root@server2 tomcat]# bin/startup.sh 
Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
[root@server2 tomcat]# netstat -antlupe
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       User       Inode      PID/Program name    
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      0          23735      3174/sshd           
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      0          24669      3351/master         
tcp        0      0 172.25.56.12:22         172.25.56.105:51836     ESTABLISHED 0     
[root@server3 ~]# cd /usr/local/tomcat/lib/
[root@server3 lib]# ls
annotations-api.jar  jsp-api.jar                              tomcat-api.jar
asm-3.2.jar          kryo-1.04.jar                            tomcat-coyote.jar
catalina-ant.jar     kryo-serializers-0.10.jar                tomcat-dbcp.jar
catalina-ha.jar      memcached-session-manager-tc6-1.6.3.jar  tomcat-i18n-es.jar
catalina.jar         memcached-session-manager-tc7-1.6.3.jar  tomcat-i18n-fr.jar
catalina-tribes.jar  minlog-1.2.jar                           tomcat-i18n-ja.jar
ecj-4.2.1.jar        msm-kryo-serializer-1.6.3.jar            tomcat-jdbc.jar
el-api.jar           reflectasm-1.01.jar                      tomcat-util.jar
jasper-el.jar        servlet-api.jar
jasper.jar           spymemcached-2.7.3.jar
[root@server3 lib]# cd ..
[root@server3 tomcat]# bin/shutdown.sh 
Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar

[root@server2 conf]# vim context.xml
在这里插入图片描述

<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
memcachedNodes="n1:172.25.56.12:11211,n2:172.25.56.13:11211"
failoverNodes="n1"
requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"
transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"
/>

[root@server2 conf]# yum install -y memcached
[root@server2 conf]# systemctl enable --now memcached.service

3上相同:
在这里插入图片描述

[root@server3 conf]# yum install memcached -y
[root@server3 tomcat]# systemctl enable --now memcached.service 
[root@server3 tomcat]# bin/startup.sh 
Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值