nginx官网手册:docs.nginx.com/nginx/admin-guide/load-balancer/http-load-balancer/
1.nginx部署
[root@server1 ~]# ls
nginx-1.18.0.tar.gz
[root@server1 ~]# tar zxf nginx-1.18.0.tar.gz
[root@server1 ~]# ls
nginx-1.18.0 nginx-1.18.0.tar.gz
[root@server1 ~]# yum install gcc pcre-devel openssl-devel -y ##安装依赖
[root@server1 ~]# vim nginx-1.18.0/auto/cc/gcc#关了debug,会减少/usr/local/nginx的大小
# debug
#CFLAGS="$CFLAGS -g"
[root@server1 ~]# cd nginx-1.18.0
[root@server1 nginx-1.18.0]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module##config指令 会在当前目录生成Makefile
[root@server1 nginx-1.18.0]# make
[root@server1 nginx-1.18.0]# make install
[root@server1 nginx-1.18.0]# cd /usr/local/nginx/
[root@server1 nginx]# du -sh *
68K conf
8.0K html
0 logs
904K sbin
[root@server1 nginx]# du -sh .
980K .
##如果没关denug会6。8M,此时cd /usr/local/nginx/, rm -fr *, make clean, rm -fr nginx-1.18.0, 然后关掉debug,重新以上步骤
[root@server1 ~]# vim .bash_profile##加入环境变量中
PATH=$PATH:$HOME/bin:/usr/local/nginx/sbin
[root@server1 ~]# source .bash_profile ##立即生效
[root@server1 ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/root/bin:/usr/local/nginx/sbin
[root@server1 ~]# which nginx
/usr/local/nginx/sbin/nginx
[root@server1 ~]# nginx ##开启nginx
网页访问172.25.3.1
[root@server1 ~]# cd /usr/local/nginx/
[root@server1 nginx]# ls
client_body_temp fastcgi_temp logs sbin uwsgi_temp
conf html proxy_temp scgi_temp
[root@server1 nginx]# cd html/
[root@server1 html]# vim test.html
[root@server1 html]# cat test.html
www.westos.org
[root@server1 html]# nginx -s reload##变更文件后需要reload,nginx -s reload;nginx -s stop是停止
网页访问172.25.3.1/test.html
2.部署mysql
[root@server1 ~]# ls
mysql-boost-5.7.31.tar.gz nginx-1.18.0 nginx-1.18.0.tar.gz
[root@server1 ~]# tar zxf mysql-boost-5.7.31.tar.gz
[root@server1 ~]# yum install cmake ncurses-devel gcc-c++ bision -y
[root@server1 mysql-5.7.31]# cd boost/boost_1_59_0/boost/
[root@server1 boost]# pwd
/root/mysql-5.7.31/boost/boost_1_59_0/boost
[root@server1 mysql-5.7.31]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/usr/local/mysql/data -DMYSQL_UNIX_ADDR=/usr/local/mysql/data/mysql.sock -DWITH_INNOBASE_STORAGE_ENGINE=1 -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DEXTRA_CHARSETS=all -DWITH_BOOST=/root/mysql-5.7.31/boost/boost_1_59_0/
####当cmake执行错误时,需要删除 rm -fr CMakeCache.txt,然后执行cmake
[root@server1 mysql-5.7.31]# make##时间久
[root@server1 mysql-5.7.31]# make install
config 参数
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \#安装目录
-DMYSQL_DATADIR=/usr/local/mysql/data \#数据库存放目录
-DMYSQL_UNIX_ADDR=/usr/local/mysql/data/mysql.sock \ #Unix socket 文件路径
-DWITH_MYISAM_STORAGE_ENGINE=1 \#安装 myisam 存储引擎
-DWITH_INNOBASE_STORAGE_ENGINE=1 \#安装 innodb 存储引擎
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \#安装 archive 存储引擎
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \#安装 blackhole 存储引擎
-DWITH_PARTITION_STORAGE_ENGINE=1 \#安装数据库分区
-DENABLED_LOCAL_INFILE=1 \#允许从本地导入数据
-DWITH_READLINE=1 \#快捷键功能
-DWITH_SSL=yes \#支持 SSL
-DDEFAULT_CHARSET=utf8 \#使用 utf8 字符
-DDEFAULT_COLLATION=utf8_general_ci \#校验字符
-DEXTRA_CHARSETS=all \#安装所有扩展字符集
-DMYSQL_TCP_PORT=3306 \#MySQL 监听端口
[root@server1 ~]# cd /usr/local/mysql/
[root@server1 mysql]# ls
bin include LICENSE mysql-test README-test support-files
docs lib man README share
[root@server1 mysql]# cd support-files/
[root@server1 support-files]# ls
magic mysqld_multi.server mysql-log-rotate mysql.server
[root@server1 support-files]# cp mysql.server /etc/init.d/mysqld##有X权限,#根据你的主机内存复制 mysql 配置文件
[root@server1 support-files]# find -name *my.cnf
[root@server1 ~]# vim /etc/my.cnf
[root@server1 ~]# cat /etc/my.cnf
[mysqld]
datadir=/usr/local/mysql/data
socket=/usr/local/mysql/data/mysql.sock
symbolic-links=0
[root@server1 ~]# vim .bash_profile ##添加环境变量
PATH=$PATH:$HOME/bin:/usr/local/nginx/sbin:/usr/local/mysql/bin/
[root@server1 ~]# source .bash_profile #使之生效
[root@server1 ~]# useradd -u 1001 -M -d /usr/local/mysql/data -s /sbin/nologin mysql
[root@server1 ~]# cd /usr/local/mysql/
[root@server1 mysql]# mysqld --verbose --help |less
[root@server1 mysql]# mysqld --initialize --user=mysql####初始化
##临时密码
2021-03-28T07:59:47.949140Z 1 [Note] A temporary password is generated for root@localhost: #0Z5%G0Cl0q=
[root@server1 mysql]# /etc/init.d/mysqld start##只能start一次
[root@server1 mysql]# netstat -antlp|grep :3306
tcp6 0 0 :::3306 :::* LISTEN 13058/mysqld
[root@server1 mysql]# mysql_secure_installation #按提示完成mysql安全设置,生产环境推荐使用。临时密码;新密码;两个回车,一直y
Securing the MySQL server deployment.
Enter password for user root: #0Z5%G0Cl0q=
New password:westos
##临时密码;新密码;两个回车,一直y
[root@server1 mysql]# mysql -p##完成mysql部署
Enter password: westos
mysql> show databases;
3.php
1)php源码编译
[root@zhenji Downloads]# scp oniguruma-devel-6.8.2-1.el7.x86_64.rpm oniguruma-6.8.2-1.el7.x86_64.rpm php-7.4.12.tar.bz2 server1:
[root@server1 ~]# yum install -y bzip2
[root@server1 ~]# tar jxf php-7.4.12.tar.bz2
[root@server1 ~]# useradd -M -d /usr/local/nginx/ -s /sbin/nologin nginx
[root@server1 ~]# id nginx
uid=1002(nginx) gid=1002(nginx) groups=1002(nginx)
[root@server1 ~]# ps aux
[root@server1 ~]# cd /usr/local/nginx/conf/
[root@server1 conf]# vim nginx.conf
user nginx nginx;
[root@server1 conf]# nginx -s reload
[root@server1 conf]# ps aux
##安装依赖
[root@server1 ~]# yum install systemd-devel.x86_64 libxml2-devel.x86_64 sqlite-devel.x86_64 libcurl-devel libpng-devel -y
[root@server1 ~]# yum install oniguruma-6.8.2-1.el7.x86_64.rpm oniguruma-devel-6.8.2-1.el7.x86_64.rpm -y ##外网下载
2)php配置
[root@server1 php-7.4.12]# cd sapi/
[root@server1 sapi]# ls
apache2handler cgi cli embed fpm litespeed phpdbg
[root@server1 sapi]# cd fpm/
[root@server1 fpm]# ls
[root@server1 fpm]# cp init.d.php-fpm /etc/init.d/php-fpm
[root@server1 fpm]# chmod +x /etc/init.d/php-fpm
[root@server1 fpm]# /etc/init.d/php-fpm
Usage: /etc/init.d/php-fpm {start|stop|force-quit|restart|reload|status|configtest}
[root@server1 fpm]# cd /usr/local/php/etc/
[root@server1 etc]# ls
pear.conf php-fpm.conf.default php-fpm.d
[root@server1 etc]# cp php-fpm.conf.default php-fpm.conf##default是模板
[root@server1 etc]# vim php-fpm.conf
pid = run/php-fpm.pid##去掉注释
[root@server1 etc]# cd php-fpm.d/
[root@server1 php-fpm.d]# ls
www.conf.default
[root@server1 php-fpm.d]# cp www.conf.default www.conf
[root@server1 php-fpm.d]# cd
[root@server1 ~]# cd php-7.4.12/
[root@server1 php-7.4.12]# cp php.ini-production /usr/local/php/etc/php.ini##主配置文件,必须是ini
[root@server1 php-7.4.12]# /etc/init.d/php-fpm start##可以启动php
[root@server1 php-7.4.12]# netstat -antlp
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 24145/php-fpm: mast
3)nginx结合php
[root@server1 ~]# cd /usr/local/nginx/conf/
[root@server1 conf]# vim nginx.conf
location / {
root html;
index index.php index.html index.htm;##默认先找php首页
}
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;
}
##网页搜nginx systemd,编写nginx自启动脚本
[root@server1 conf]# cd /etc/systemd/system/
[root@server1 system]# vim nginx.service
[root@server1 system]# cat nginx.service
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
[root@server1 system]# nginx -s stop
[root@server1 system]# systemctl daemon-reload
[root@server1 system]# systemctl start nginx
[root@server1 system]# systemctl enable nginx
[root@server1 php-7.4.12]# cd sapi/fpm/
[root@server1 fpm]# ls
[root@server1 fpm]# cp php-fpm.service /usr/lib/systemd/system/
[root@server1 fpm]# cd /usr/lib/systemd/system/
[root@server1 system]# vim php-fpm.service
#ProtectSystem=full##//默认php-fpm启动时只读挂载/usr目录,注释掉
[root@server1 system]# systemctl daemon-reload
[root@server1 system]# systemctl start php-fpm#通过脚本启动,开机自起
[root@server1 system]# systemctl enable php-fpm
[root@server1 system]# cd /usr/local/nginx/html/
[root@server1 html]# ls
50x.html index.html test.html
[root@server1 html]# vim index.php
[root@server1 html]# cat index.php
<?php
phpinfo()
?>
网页访问172.25.3.1
改配置文件,然后直接 systemctl reload php-fpm就会生效
[root@server1 system]# cd /usr/local/php/etc/
[root@server1 etc]# vim php.ini
date.timezone = Asia/Shanghai
[root@server1 etc]# systemctl reload php-fpm
4)测试php部署完成
###MyAdminphp是php编写的一个数据库管理工具
[root@server1 ~]# /etc/init.d/mysqld start ##开启mysql
[root@server1 ~]# vim .bash_profile
PATH=$PATH:$HOME/bin:/usr/local/nginx/sbin:/usr/local/mysql/bin/:/usr/local/php/b
in
[root@server1 ~]# source .bash_profile
[root@server1 ~]# cd /usr/local/php/etc/
[root@server1 etc]# vim php.ini ##修改两个sock路径
pdo_mysql.default_socket=/usr/local/mysql/data/mysql.sock
mysqli.default_socket =/usr/local/mysql/data/mysql.sock
[root@zhenji 2021-4-3]# scp phpMyAdmin-5.0.2-all-languages.zip server1:
[root@server1 ~]# unzip phpMyAdmin-5.0.2-all-languages.zip
[root@server1 ~]# mv phpMyAdmin-5.0.2-all-languages /usr/local/nginx/html/phpadmin
[root@server1 ~]# cd /usr/local/nginx/html/phpadmin
[root@server1 phpadmin]# cd /usr/local/mysql/
[root@server1 mysql]# chmod 755 data
访问网页172.25.3.1/phpadmin 登陆 root 密码westos
4.php加memcache模块
[root@zhenji 2021-4-3]# scp memcache-4.0.5.2.tgz server1:
[root@server1 ~]# tar zxf memcache-4.0.5.2.tgz
[root@server1 ~]# cd memcache-4.0.5.2/
[root@server1 memcache-4.0.5.2]# ls
cloudbuild.yaml config.m4 CREDITS Dockerfile LICENSE php7 tests
config9.m4 config.w32 docker example.php memcache.php README
[root@server1 memcache-4.0.5.2]# phpize
[root@server1 memcache-4.0.5.2]# yum install autoconf -y
[root@server1 memcache-4.0.5.2]# phpize
Configuring for:
PHP Api Version: 20190902
Zend Module Api No: 20190902
Zend Extension Api No: 320190902
[root@server1 memcache-4.0.5.2]# ls##有configure文件
[root@server1 memcache-4.0.5.2]# ./configure --enable-memcache
[root@server1 memcache-4.0.5.2]# make
[root@server1 memcache-4.0.5.2]# make install
Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-20190902/
[root@server1 memcache-4.0.5.2]# cd /usr/local/php/lib/php/extensions/no-debug-non-zts-20190902/
[root@server1 no-debug-non-zts-20190902]# ls
memcache.so opcache.a opcache.so
[root@server1 no-debug-non-zts-20190902]# cd /usr/local/
[root@server1 local]# ls
bin games lib libexec nginx sbin src
etc include lib64 mysql php share
[root@server1 local]# php -m |grep memcache##此时模块还没有被加载
[root@server1 local]# cd php/etc/
[root@server1 etc]# vim php.ini ##php支持动态加载
extension=memcache##添加
[root@server1 etc]# systemctl reload php-fpm
[root@server1 etc]# php -m |grep memcache
memcache
[root@server1 etc]# yum install memcached -y
[root@server1 etc]# systemctl start memcached
[root@server1 etc]# systemctl start memcached
[root@server1 etc]# netstat -antlp##11211 开启
tcp6 0 0 :::11211
[root@server1 memcache-4.0.5.2]# cp example.php /usr/local/nginx/html/
[root@server1 memcache-4.0.5.2]# cp memcache.php /usr/local/nginx/html/
[root@server1 memcache-4.0.5.2]# cd /usr/local/nginx/html/
[root@server1 html]# vim memcache.php ##改两个地方
define('ADMIN_USERNAME','admin'); // Admin Username
define('ADMIN_PASSWORD','westos'); // Admin Password
$MEMCACHE_SERVERS[] = 'localhost:11211'; // add more as an array
#$MEMCACHE_SERVERS[] = 'mymemcache-server2:11211'; // add more as an array
网页访问http://172.25.3.1/memcache.php/,登陆admin westos
显示命中和未命中百分比,刷新网页http://172.25.3.1/example.php/,命中百分比会增加
##php通过memcache模块调用memcache
client->nginx->fastchi_pass->php-fpm:9000->php-memcache->memcached:11211,(nginx高性能高并发,自己有缓存,速度快)
5.构建nginx高速缓存
使用memc-nginx和srcache-nginx模块构建高效透明的缓存机制
软件下载: https://openresty.org/cn/
1)部署openresty,使用lua更适合开发
[root@server1 ~]# tar zxf openresty-1.19.3.1.tar.gz
[root@server1 ~]# cd openresty-1.19.3.1/
[root@server1 openresty-1.19.3.1]# ./configure
[root@server1 openresty-1.19.3.1]# make
[root@server1 openresty-1.19.3.1]# make install
[root@server1 openresty-1.19.3.1]# cd /usr/local/openresty/nginx/
[root@server1 nginx]# ls
conf html logs sbin
[root@server1 nginx]# systemctl stop nginx
[root@server1 nginx]# vim conf/nginx.conf##改三个地方
user nginx nginx;
location / {
root html;
index index.php index.html index.htm;
}
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;
}
[root@server1 nginx]# /usr/local/openresty/nginx/sbin/nginx -t
[root@server1 nginx]# /usr/local/openresty/nginx/sbin/nginx
[root@server1 nginx]# netstat -antlp
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 4631/nginx: master
·网页访问http://172.25.3.1 是Welcome to OpenResty!页面
2)部署openresty高速缓存策略
[root@zhenji 2021-4-3]# ab -c10 -n 10000 http://172.25.3.1/example.php
Requests per second: 1341.60 [#/sec] (mean)###压测,没有高速下的速度
[root@server1 conf]# vim nginx.conf##改3个地方
http {
upstream memcache {
server 127.0.0.1:11211;
keepalive 512;
}
include mime.types;
default_type application/octet-stream;
location /memc {
internal; //表示只接受内部访问
memc_connect_timeout 100ms;
memc_send_timeout 100ms;
memc_read_timeout 100ms;
set $memc_key $query_string; //使用内置的$query_string来作为key
set $memc_exptime 300; //表示缓存失效时间
memc_pass memcache;
}
location ~ \.php$ {
set $key $uri$args;
srcache_fetch GET /memc $key;##通过Key访问缓存在memc内的东西,速度快
srcache_store PUT /memc $key;
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
#fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi.conf;
}
[root@server1 conf]# /usr/local/openresty/nginx/sbin/nginx -t
[root@server1 conf]# /usr/local/openresty/nginx/sbin/nginx -s reload
[root@zhenji 2021-4-3]# ab -c10 -n 10000 http://172.25.3.1/example.php##压测,高速下的速度
Requests per second: 4655.69 [#/sec] (mean)
访问策略:当所请求的uri以“.php”结尾时,首先到memcache中查询有没有以 u r i uri uriargs为key的数据,如果有则直接返回;否则,执行location的逻辑,如果返回的http状态码为200,则在输出前以 u r i uri uriargs为key,将输入结果存入memcache。
6.nginx并发优化
设置进程数
[root@server1 ~]# cd /usr/local/nginx/conf/
[root@server1 conf]# vim nginx.conf
user nginx nginx;
worker_processes 2;//工作进程数
worker_cpu_affinity 01 10;//cpu有多少个核,就有几位数,1代表内核开启,0代表内核关闭
events {
use epoll;//使用epoll模型
worker_connections 65535;//
}
[root@server1 conf]# nginx -s reload
[root@server1 conf]# ps aux##有两个nginx进程
%%Linux下高并发socket最大连接数所受的各种限制
[root@server1 conf]# vim /etc/security/limits.conf ##自动生效
nginx - nofile 65535##最后添加:修改用户进程可打开文件数限制
#Linux系统级的最大打开文件数限制
fs.file-max = 188414 //和内存容量相关
#修改网络内核对TCP连接的有关限制
net.ipv4.ip_local_port_range = 1024 65535
#限制接收新 TCP 连接侦听队列的大小
net.core.somaxconn = 2048
#启用tcp连接timewait快速回收和重用
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_tw_reuse = 1
[root@server1 conf]# ulimit -a
[root@server1 conf]# sysctl -a | grep file##查看内核
[root@server1 conf]# ll /etc/sysctl.conf ##启用tcp连接timewait快速回收和重用,在文件/etc/sysctl.conf文件里编写全局变量
%nginx作为反向代理服务器时:cline请求nginx,nginx请求后端,所以除以2; max_clients=worker_processes * worker_connections/2;;;worker_processes//工作进程数;worker_connections//单个工作进程并发连接数
%nginx作为http服务器时:max_clients = worker_processes * worker_connections
##server1做负载均衡,新建server2,3,开启httpd
[root@server1 conf]# vim nginx.conf##改动两个
http {
upstream westos {
server 172.25.3.2:80;
server 172.25.3.3:80;
}
include mime.types;
default_type application/octet-stream;
#location / {
# root html;
# index index.php index.html index.htm;
#}
location / {
proxy_pass http://westos;#只要有请求,就到上面定义的westos中
}
[root@server1 conf]# nginx -s reload
[root@server1 conf]# curl 172.25.3.1##负载均衡
server2
[root@server1 conf]# curl 172.25.3.1
server3
##负载均衡
7.nginx平滑升级(不关闭nginx进程下,进行升级回滚)
Nginx如何做到热部署(平滑升级回滚)?
修改配置文件nginx.conf后,重新生成新的worker进程,当然会以新的配置进行处理请求,而且新的请求必须都交给新的worker进程,至于老的worker进程,等把那些以前的请求处理完毕后,kill掉即可。
1)下载nginx新版本软件,正常执行./configure 和make 但不要执行make instal
##下载编译一个新版本1.17.1的nginx,make,不做make install
[root@server1 ~]# tar zxf nginx-1.19.1.tar.gz
[root@server1 ~]# cd nginx-1.19.1/
[root@server1 nginx-1.19.1]# vim auto/cc/gcc##关闭debug
# debug
#CFLAGS="$CFLAGS -g"
[root@server1 nginx-1.19.1]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module
[root@server1 nginx-1.19.1]# make##不要make install,目的是得到一个nginx的目录
[root@server1 ~]# cd /usr/local/nginx/sbin/
[root@server1 sbin]# ls
nginx
[root@server1 sbin]# mv nginx nginx.1.18##备份源程序
[root@server1 sbin]# ls
nginx.1.18
[root@server1 sbin]# cd
[root@server1 ~]# cd nginx-1.19.1/objs/
[root@server1 objs]# ls
[root@server1 objs]# cp nginx /usr/local/nginx/sbin/##拷贝新程序
[root@server1 objs]# cd /usr/local/nginx/sbin/
[root@server1 sbin]# ls
nginx nginx.1.18
[root@server1 sbin]# ./nginx -v
nginx version: nginx/1.19.1
[root@server1 sbin]# ./nginx.1.18 -v
nginx version: nginx/1.18.0
[root@server1 sbin]# curl -I localhost
HTTP/1.1 200 OK
Server: nginx/1.18.0
2)升级新程序
[root@server1 sbin]# ps ax|grep nginx
4746 ? Ss 0:00 nginx: master process /usr/local/nginx/sbin/nginx
5319 ? S 0:00 nginx: worker process
5320 ? S 0:00 nginx: worker process
9397 pts/0 S+ 0:00 grep --color=auto nginx
[root@server1 sbin]# kill -USR2 4746##4746是master进程,又启动了一份新的进程
[root@server1 sbin]# ps ax|grep nginx
4746 ? Ss 0:00 nginx: master process /usr/local/nginx/sbin/nginx
5319 ? S 0:00 nginx: worker process
5320 ? S 0:00 nginx: worker process
9461 ? S 0:00 nginx: master process /usr/local/nginx/sbin/nginx
9462 ? S 0:00 nginx: worker process
9463 ? S 0:00 nginx: worker process
9470 pts/0 S+ 0:00 grep --color=auto nginx
##关闭原worker进程但保留主进程:为了回退
[root@server1 sbin]# kill -WINCH 4746##回收旧版本1.18的worker进程.原始的子进程没了,master留着是为了回滚
[root@server1 sbin]# ps ax|grep nginx
4746 ? Ss 0:00 nginx: master process /usr/local/nginx/sbin/nginx
9461 ? S 0:00 nginx: master process /usr/local/nginx/sbin/nginx
9462 ? S 0:00 nginx: worker process
9463 ? S 0:00 nginx: worker process
9495 pts/0 S+ 0:00 grep --color=auto nginx
[root@server1 sbin]# curl -I localhost
HTTP/1.1 200 OK
Server: nginx/1.19.1
3)版本回退
[root@server1 sbin]# ls
nginx nginx.1.18
[root@server1 sbin]# mv nginx nginx.1.19
[root@server1 sbin]# mv nginx.1.18 nginx
[root@server1 sbin]# ps ax|grep nginx
4746 ? Ss 0:00 nginx: master process /usr/local/nginx/sbin/nginx
9461 ? S 0:00 nginx: master process /usr/local/nginx/sbin/nginx
9462 ? S 0:00 nginx: worker process
9463 ? S 0:00 nginx: worker process
9661 pts/0 S+ 0:00 grep --color=auto nginx
[root@server1 sbin]# kill -HUP 4746##唤醒原进程1.18
[root@server1 sbin]# ps ax|grep nginx
4746 ? Ss 0:00 nginx: master process /usr/local/nginx/sbin/nginx
9461 ? S 0:00 nginx: master process /usr/local/nginx/sbin/nginx
9462 ? S 0:00 nginx: worker process
9463 ? S 0:00 nginx: worker process
9685 ? S 0:00 nginx: worker process
9686 ? S 0:00 nginx: worker process
9691 pts/0 S+ 0:00 grep --color=auto nginx
[root@server1 sbin]# kill -WINCH 9461##回收新版本1.19的worker进程
[root@server1 sbin]# ps ax|grep nginx
4746 ? Ss 0:00 nginx: master process /usr/local/nginx/sbin/nginx
9461 ? S 0:00 nginx: master process /usr/local/nginx/sbin/nginx
9685 ? S 0:00 nginx: worker process
9686 ? S 0:00 nginx: worker process
9705 pts/0 R+ 0:00 grep --color=auto nginx
[root@server1 sbin]# curl -I localhost
HTTP/1.1 200 OK
Server: nginx/1.18.0
[root@server1 sbin]# kill -QUIT 9461##关闭新版本主进程
8.虚拟主机
1)配置虚拟主机
[root@server1 ~]# cd /usr/local/nginx/conf/
[root@server1 conf]# vim nginx.conf
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
##最后添加
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 conf]# ps ax|grep nginx
4746 ? Ss 0:00 nginx: master process /usr/local/nginx/sbin/nginx
9461 ? S 0:00 nginx: master process /usr/local/nginx/sbin/nginx
9685 ? S 0:00 nginx: worker process
9686 ? S 0:00 nginx: worker process
12383 pts/0 R+ 0:00 grep --color=auto nginx
[root@server1 conf]# kill -QUIT 9461
2)虚拟主机负载均衡
[root@zhenji images]# vim /etc/hosts
172.25.3.1 server1 mfsmaster controller www.westos.org www.linux.org
[root@zhenji images]# curl www.westos.org
server2
[root@zhenji images]# curl www.westos.org
server3
[root@server1 conf]# mkdir /web1
[root@server1 conf]# cd /web1/
[root@server1 web1]# echo web1 > index.html
[root@zhenji images]# curl www.linux.org
web1
3) nginx 重定向
##需要做认证
[root@server1 ~]# cd /etc/pki/tls/certs/
[root@server1 certs]# ls
ca-bundle.crt make-dummy-cert renew-dummy-cert
ca-bundle.trust.crt Makefile
[root@server1 certs]# pwd
/etc/pki/tls/certs
[root@server1 certs]# make cert.pem
Country Name (2 letter code) [XX]:cn
State or Province Name (full name) []:shanxi
Locality Name (eg, city) [Default City]:xian
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]# vim /usr/local/nginx/conf/nginx.conf##443去掉注释
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;
}
}
[root@server1 certs]# 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
[root@server1 certs]# nginx -s reload
[root@server1 certs]# netstat -antlp | grep 443
tcp 0 0 0.0.0.0:443 0.0.0.0:* LISTEN 4746/nginx: master
4)HTTP负载平衡
nginx官网手册:docs.nginx.com/nginx/admin-guide/load-balancer/http-load-balancer/
[root@server1 certs]# cd /usr/local/nginx/conf/
[root@server1 conf]# vim nginx.conf##权重;其他server端死掉了,backup本机参与调度
http {
upstream westos {
server 172.25.3.2:80 weight=3;
server 172.25.3.3:80;
server 127.0.0.1:80 backup;
}
[root@server1 certs]# nginx -t
[root@server1 certs]# nginx -s reload
[root@zhenji images]# curl www.westos.org
[root@server1 conf]# vim nginx.conf##hash,访问时只调度到一个主机
http {
upstream westos {
ip_hash;
server 172.25.3.2:80;
server 172.25.3.3:80;
#server 127.0.0.1:80 backup;
}
[root@server1 certs]# nginx -t
[root@server1 certs]# nginx -s reload
[root@zhenji images]# curl www.westos.org
5)Sticky cookie
客户端的下一个请求包含cookie值,NGINX Plus将请求路由到响应第一个请求的上游服务器
[root@server1 ~]# tar zxf nginx-goodies-nginx-sticky-module0module-ng-08a395c66e42.zip
[root@server1 nginx-1.18.0]# make clean
[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 ##不能make install,平滑
[root@server1 nginx-1.18.0]# cd objs/
[root@server1 nginx-1.18.0]# nginx -s stop
[root@server1 objs]# cp nginx /usr/local/nginx/sbin/ -f
[root@server1 objs]# nginx -v
[root@server1 objs]# cd /usr/local/nginx/conf/
[root@server1 conf]# vim nginx.conf
http {
upstream westos {
sticky;
server 172.25.2.2:80;
server 172.25.2.3:80;
#server 127.0.0.1:80 backup;
}
[root@server1 conf]# nginx -t
[root@server1 conf]# nginx
[root@server1 conf]# nginx -s reload
网页访问www.westos.org,server2,再刷新还是server2;删除网页缓存,刷新界面会变成server3
9.安全控制
nginx官方手册:http://docs.nginx.com/nginx/admin-guide/security-controls/controlling-access-proxied-http/
1)限制一个并发
[root@server1 conf]# vim nginx.conf
#gzip on;
limit_conn_zone $binary_remote_addr zone=addr:10m;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
location /download/ {
limit_conn addr 1;
}
##下载一个图片vim.jpg放在/usr/local/nginx/html/download
[root@zhenji 2021-4-3]# ab -c 10 -n 10 http://172.25.3.1/download/vim.jpg##10并发,10个请求
Failed requests: 3
###/nginx/logs中可以看日至
2)nginx限流
[root@server1 conf]# vim nginx.conf
server {
location /download/ {
limit_conn addr 1; #限制并发数
limit_rate 50k; #限制带宽
}
[root@server1 conf]# nginx -t
[root@server1 conf]# nginx -s reload
[root@zhenji 2021-4-3]# ab -c 1 -n 10 http://172.25.3.1/download/vim.jpg
Time taken for tests: 60.078 seconds
##可以去/nginx/logs##看日至
3)队列
[root@server1 conf]# vim nginx.conf
#gzip on;
limit_conn_zone $binary_remote_addr zone=addr:10m;
limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;##1s允许请求一次
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
location /download/ {
limit_conn addr 1;
#limit_rate 50;
limit_req zone=one burst=5 nodelay;##排队,不延时
autoindex on;#自动索引,网页访问download时可以直接显示vim.jpg图片
}
[root@server1 conf]# nginx -t
[root@server1 conf]# nginx -s reload
[root@zhenji 2021-4-3]# ab -c 1 -n 10 http://172.25.3.1/download/vim.jpg
Failed requests: 4##自身一个,burst5个,所以失败4个
10.nginx配置管理
1)日志轮询
[root@server1 conf]# date +%F
2021-04-04
[root@server1 conf]# date +%F -d -1day##当前时间减一天
2021-04-03
[root@server1 logs]# cd /opt/
[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`
[root@server1 opt]# ls
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]# ll
total 60
-rw-r--r-- 1 root root 37799 Apr 4 10:45 access_2021-04-03.log
2)禁用不必要的日志记录,以节省磁盘IO的消耗
[root@server1 conf]# vim nginx.conf
##location后添加
location ~ .*\.(gif|jpg|png)$ {
expires 365d;
root html;
access_log off;
}
[root@zhenji 2021-4-3]# curl -I http://172.25.3.1/download/vim.jpg
[root@server1 logs]# ll##被访问后日至不会改变
total 68
-rw-r--r-- 1 root root 37799 Apr 4 10:45 access_2021-04-03.log
-rw-r--r-- 1 nginx root 779 Apr 4 13:55 access.log
-rw-r--r-- 1 nginx root 17752 Apr 4 13:55 error.log
-rw-r--r-- 1 root root 6 Apr 4 09:23 nginx.pid
[root@server1 conf]# vim nginx.conf
location ~ .*\.(gif|jpg|png)$ {
deny all;##限制IP
expires 365d;
root html;
access_log off;
}
[root@zhenji 2021-4-3]# curl -I http://172.25.3.1/download/vim.jpg
HTTP/1.1 403 Forbidden
3)中文乱码
[root@server1 nginx]# cd html/
[root@server1 html]# ls
50x.html download index.html index.php memcache.php phpadmin test.html
[root@server1 html]# vim index.
[root@server1 html]# vim index.html
<h1>Welcome to nginx! 中文乱码</h1>
网页访问172.25.3.1
4)重定向(两个)
1、rewrite语法:
指令语法:rewrite regex replacement[flag];
默认值:none
应用位置:server、location、if
rewrite是实现URL重定向的重要指令,他根据regex(正则表达式)来匹配内容跳转到replacement,结尾是flag标记
简单的小例子:
rewrite ^/(.*) http://www.baidu.com/ permanent; # 匹配成功后跳转到百度,执行永久301跳转
常用正则表达式:
rewrite 最后一项flag参数:
[root@server1 conf]# vim nginx.conf
server {
listen 80;
server_name localhost;
#return 500;##直接拒绝
rewrite ^(.*) http://www.westos.org permanent;##permanent301永久重定向到www.westos.org
#charset koi8-r;
[root@zhenji 2021-4-3]# curl -I http://172.25.3.1/download/vim.jpg
HTTP/1.1 301 Moved Permanently
Server: nginx/1.18.0
Date: Sun, 04 Apr 2021 06:18:47 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive
Location: http://www.westos.org
80重定向443
[root@server1 conf]# vim nginx.conf
server {
listen 80;
server_name www.westos.org;
rewrite ^/(.*)$ https://www.westos.org/$1 permanent;
#location / {
#proxy_pass http://westos;
#}
www.westos.org/bbs 重定向bbs.westos.org
[root@server1 conf]# vim nginx.conf
server {
listen 80;
server_name www.linux.org;
rewrite ^/bbs$ http://bbs.linux.org permanent;
rewrite ^/bbs/(.*)$ http://bbs.linux.org/$1 permanent;
location / {
root /web1;
index index.html;
}
}
server {
listen 80;
server_name bbs.linux.org;
location / {
root /bbs;
index index.html;
}
}
[root@server1 conf]# mkdir /bbs
[root@server1 conf]# echo bbs.westos.org > /bbs/index.html
[root@zhenji 2021-4-3]# curl -I www.linux.org/bbs
[root@zhenji 2021-4-3]# curl -I www.linux.org/bbs/linux.html
bbs.westos.org 重定向www.westos.org/bbs:
[root@server1 conf]# vim nginx.conf
server {
listen 80;
server_name www.linux.org bbs.linux.org;
rewrite ^/bbs$ http://bbs.linux.org permanent;
rewrite ^/bbs/(.*)$ http://bbs.linux.org/$1 permanent;
if ($host = "bbs.linux.org"){
rewrite ^/(.*)$ http://www.linux.org/bbs/$1 permanent;
}
location / {
root /web1;
index index.html;
}
[root@server1 conf]# mv /bbs/ /web1/
[root@server1 conf]# nginx -s reload
[root@zhenji 2021-4-3]# curl -I bbs.linux.org
HTTP/1.1 301 Moved Permanently
Server: nginx/1.18.0
Date: Sun, 04 Apr 2021 07:01:22 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive
Location: http://www.linux.org/bbs/
5)nginx 防盗链
[root@server1 conf]# vim nginx.conf##注释所有的重定向
server {
listen 80;
server_name www.westos.org;
#rewrite ^/(.*)$ https://www.westos.org/$1 permanent;
location / {
root html;
index index.html;
#proxy_pass http://westos;
}
}
server {
listen 80;
server_name www.linux.org bbs.linux.org;
#rewrite ^/bbs$ http://bbs.linux.org permanent;
#rewrite ^/bbs/(.*)$ http://bbs.linux.org/$1 permanent;
网页http://172.25.3.1/download/vim.jpg
[root@server2 ~]# cd /var/www/html/
[root@server2 html]# ls
index.html
[root@server2 html]# vim index.html
[root@server2 html]# cat index.html
<body>
<img src="http://www.westos.org/download/vim.jpg"/>
</body>
[root@server1 conf]# vim nginx.conf
server {
listen 80;
server_name www.westos.org;
#rewrite ^/(.*)$ https://www.westos.org/$1 permanent;
location / {
root html;
index index.html;
#proxy_pass http://westos;
}
location ~ \.(jpg|png)$ {
valid_referers none blocked www.westos.org;
if ($invalid_referer) {
#return 403;
rewrite ^/ http://172.25.3.3/daolian.jpg;
}
}
}
网页http://daolian.westos.org/,不是vim.jpg,而是被重定向到了daolian.jpg
6)日志可视化
[root@zhenji 2021-4-3]# scp goaccess-1.4.tar.gz server1:
[root@zhenji 2021-4-3]# scp GeoIP-devel-1.5.0-13.el7.x86_64.rpm server1:
[root@server1 ~]# wget https://tar.goaccess.io/goaccess-1.4.tar.gz
[root@server1 ~]# tar -xzvf goaccess-1.4.tar.gz
[root@server1 ~]# cd goaccess-1.4/
[root@server1 goaccess-1.4]# ./configure --enable-utf8 --enable-geoip=legacy
[root@server1 goaccess-1.4]# make
[root@server1 goaccess-1.4]# make install
[root@server1 goaccess-1.4]# cd /usr/local/nginx/conf/
[root@server1 conf]# vim nginx.conf
注释#access_log off;
[root@server1 conf]# nginx -s reload
[root@server1 conf]# cd /usr/local/nginx/logs/
[root@server1 logs]# goaccess access.log -o /usr/local/nginx/html/report.html --log-format=COMBINED --real-time-html####时时更新,ctrl+C退出
注释所有的重定向,清理网页缓存,网页访问http://172.25.3.1/report.html
7)tomcat
[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 conf lib LICENSE logs NOTICE RELEASE-NOTES RUNNING.txt temp webapps work
[root@server2 tomcat]# 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 -antlp
tcp6 0 0 :::8080 :::* LISTEN 4994/java
网页访问http://172.25.3.2:8080/
2,3上都安装tomcat,进行负载均衡
[root@server3 ~]# rpm -ivh jdk-8u121-linux-x64.rpm
[root@server3 ~]# tar zxf apache-tomcat-7.0.37.tar.gz
[root@server3 ~]# mv apache-tomcat-7.0.37 /usr/local/tomcat
[root@server3 ~]# cd /usr/local/tomcat
[root@server3 tomcat]# bin/startup.sh
[root@server1 conf]# vim nginx.conf
##改两个地方
http {
upstream westos {
sticky;
server 172.25.3.2:80;
server 172.25.3.3:80;
#server 127.0.0.1:80 backup;
}
upstream tomcat {
sticky;
server 172.25.3.2:8080;
server 172.25.3.3:8080;
}
location ~ \.jsp$ {
proxy_pass http://tomcat;
}
[root@server1 conf]# nginx -s reload
[root@server2 tomcat]# cd webapps/ROOT/
[root@server2 ROOT]# ls
asf-logo.png bg-nav-item.png favicon.ico tomcat.gif WEB-INF
asf-logo-wide.gif bg-nav.png index.jsp tomcat.png
bg-button.png bg-upper.png RELEASE-NOTES.txt tomcat-power.gif
bg-middle.png build.xml tomcat.css tomcat.svg
[root@server2 ROOT]# pwd
/usr/local/tomcat/webapps/ROOT
[root@zhenji 2021-4-3]# scp test.jsp server2:/usr/local/tomcat/webapps/ROOT
[root@zhenji 2021-4-3]# scp test.jsp server3:/usr/local/tomcat/webapps/ROOT
网页访问http://172.25.3.1/test.jsp
11.交叉存储
[root@zhenji 2021-4-3]# scp -r jar server2:
[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-tc6-1.6.3.jar
[root@server2 jar]# cp * /usr/local/tomcat/lib/
[root@server2 jar]# scp * root@172.25.3.3:/usr/local/tomcat/lib/
[root@server2 jar]# vim /usr/local/tomcat/conf/context.xml
<Context>
......
<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
memcachedNodes="n1:172.25.3.2:11211,n2:172.25.3.3:11211"
failoverNodes="n1"#在 node2 上此项设置为“n2”
requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"
transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"
/>
</Context>
[root@server2 conf]# yum install memcached -y
[root@server2 conf]# systemctl enable --now memcached
[root@server2 conf]# cd ..
[root@server2 tomcat]# bin/shudown.sh
[root@server2 tomcat]# bin/startup.sh
##正常是找对方,出现故障时找自己。3出现故障时会由2接管,2会从3上提取数据,所以2出故障的数据也不会丢
[root@server3 jar]# vim /usr/local/tomcat/conf/context.xml
<Context>
......
<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
memcachedNodes="n1:172.25.3.2:11211,n2:172.25.3.3:11211"
failoverNodes="n2"#在 node2 上此项设置为“n2”
requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"
transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"
/>
</Context>
[root@server3 conf]# yum install memcached -y
[root@server3 conf]# systemctl enable --now memcached
[root@server3 lib]# cd /usr/local/tomcat/
[root@server2 tomcat]# bin/shudown.sh
[root@server3 tomcat]# bin/startup.sh
[root@server3 tomcat]# cd logs/
[root@server2 logs]# cat catalina.out ##说明开启成功
INFO: MemcachedSessionService finished initialization, sticky true, operation timeout 1000, with node ids [n2] and failover node ids [n1]
网页增加user1,user2
[root@server3 tomcat]# bin/shutdown.sh ##关掉server3后,再关掉server2所有的数据都会丢失
网页增加user3
server3的bin/shutdown.sh 后会自动跳转到server2上,且挂掉后的数据user3依然保存
[root@server3 tomcat]# yum install telnet -y
[root@server3 tomcat]# telnet localhost 11211###有user3
Wx� �x�701x�7ux�7x#D3412FC78DF8319FA8F3650AE3EC3DD1-n2user1111user2222user3333
END
quit
get 网页上的ID
[root@server2 logs]# systemctl stop memcached
[root@server2 logs]# systemctl stop memcached##关掉server3后,再关掉server2所有的数据都会丢失,但关掉memcache不会
网页增加user4,user5
[root@server3 tomcat]# telnet localhost 11211##有user4,user5
Wx� �x�P01x�P$x�P$#D3412FC78DF8319FA8F3650AE3EC3DD1-n2user1111user2222uesr5555user3333user4444
END
quit