两台后端RShttpd服务器配置
#RS1 [root@rs1 ~]# yum install -y httpd [root@rs1 ~]# echo "10.0.0.7 web1" > /var/www/html/index.html [root@rs1 ~]# systemctl enable --now httpd.service [root@rs1 ~]# curl 127.0.0.1 10.0.0.7 web1
#RS2 [root@rs2 ~]# yum install -y httpd [root@rs2 ~]# echo "10.0.0.17 web2" > /var/www/html/index.html [root@rs2 ~]# systemctl enable --now httpd.service [root@rs2 ~]# curl 127.0.0.1 10.0.0.17 web2
Nginx反向代理配置
#在主配置http模块中中以下语句块 upstream webservers { #hash $request_uri consistent; #hash $cookie_sessionid; #ip_hash; #least_conn; server 10.0.0.7:80 weight=1 fail_timeout=5s max_fails=3; server 10.0.0.17:80 weight=1 fail_timeout=5s max_fails=3; server 127.0.0.1:80 weight=1 fail_timeout=5s max_fails=3 backup; }
#子配置文件夹 [root@Centos8 conf]# vim conf.d/pc.conf server { listen 80; proxy_cache off; #listen 443 ssl; #ssl_certificate /apps/nginx/certs/www.pc.com.pem; #ssl_certificate_key /apps/nginx/certs/www.pc.com.key; #ssl_session_cache shared:sslcache:20m; #ssl_session_timeout 10m; server_name www.pc.com; location / { index index.html index.php; root /data/nginx/html/pc; } location /web { index index.html root /data/nginx/html/mobile; proxy_pass http://webservers/; } }
#重启Nginx,访问并测试 [root@openEuler21 ~]# curl http://www.pc.com/web 10.0.0.7 web1 [root@openEuler21 ~]# curl http://www.pc.com/web 10.0.0.17 web2 #关闭10.0.0.7和10.0.0.17的http服务,测试nginx backup服务器可用性。 [root@openEuler21 ~]# curl http://www.pc.com/web sorry server1.2 Nginx调度算法介绍
Nginx 可以基于ngx_http_upstream_module模块提供服务器分组转发、权重分配、状态监测、调度算法等高级功能。
server address [parameters]; #配置一个后端web服务器,配置在upstream内,至少要有一个server服务器配置。 #server支持的parameters如下: weight=number #设置权重,默认为1,实现类似于LVS中的WRR,WLC等 max_conns=number #给当前后端server设置最大活动链接数,默认为0表示没有限制 max_fails=number #后端服务器的下线条件,当客户端访问时,对本次调度选中的后端服务器连续进行检测多少次,如果都失败就标记为不可用,默认为1次,当客户端访问时,才会利用TCP触发对探测后端服务器健康性检查,而非周期性的探测。 fail_timeout=time #后端服务器的上线条件,对已经检测到处于不可用的后端服务器,每隔此时间间隔再次进行检测是否恢复可用,如果发现可用,则将后端服务器参与调度,默认为10秒。 backup #设置为备份服务器,当所有后端服务器不可用时,才会启用此备用服务器。 down #标记为down状态,可以平滑下线后端服务器 resolve #当server定义的是主机名的时候,当A记录发生变化会自动应用新IP而不用重启Nginx
hash KEY [consistent];
#基于指定请求报文中首部字段或者URI等key做hash计算,使用consistent参数,将使用ketama一致性hash算法,适用于后端是Cache服务器(如varnish)时使用,consistent定义使用一致性hash运算,一致性hash基于取模运算。
[root@Centos8 conf]# vim /apps/nginx/conf/nginx.conf upstream webservers { hash $request_uri consistent; #hash $cookie_sessionid; #ip_hash; #least_conn; server 10.0.0.7:80 weight=1 fail_timeout=5s max_fails=3; server 10.0.0.17:80 weight=1 fail_timeout=5s max_fails=3; # server 127.0.0.1:80 weight=1 fail_timeout=5s max_fails=3 backup; #hash算法不支持添加backup } [root@Centos8 conf]# nginx -s reload
#后端RS服务器添加多个页面 [root@rs1 ~]# for i in {1..10} ; do echo 10.0.0.7 page$i > /var/www/html/test$i.html;done [root@rs1 ~]# ll /var/www/html/ total 44 -rw-r--r-- 1 root root 14 Dec 27 13:33 index.html -rw-r--r-- 1 root root 16 Dec 27 15:04 test10.html -rw-r--r-- 1 root root 15 Dec 27 15:04 test1.html -rw-r--r-- 1 root root 15 Dec 27 15:04 test2.html -rw-r--r-- 1 root root 15 Dec 27 15:04 test3.html -rw-r--r-- 1 root root 15 Dec 27 15:04 test4.html -rw-r--r-- 1 root root 15 Dec 27 15:04 test5.html -rw-r--r-- 1 root root 15 Dec 27 15:04 test6.html -rw-r--r-- 1 root root 15 Dec 27 15:04 test7.html -rw-r--r-- 1 root root 15 Dec 27 15:04 test8.html -rw-r--r-- 1 root root 15 Dec 27 15:04 test9.html [root@rs1 ~]# cat /var/www/html/test2.html 10.0.0.7 page2 #RS2设置如上1.2.2 基于cookie中的sessionid的key进行hash调度
基于cookie中的sessionid这个key进行hash调度,实现会话绑定
[root@Centos8 conf]# vim /apps/nginx/conf/nginx.conf
upstream webservers {
#hash $request_uri consistent;
hash $cookie_sessionid;#的key的名称
#ip_hash;
#least_conn;
server 10.0.0.7:80 weight=1 fail_timeout=5s max_fails=3;
server 10.0.0.17:80 weight=1 fail_timeout=5s max_fails=3;
# server 127.0.0.1:80 weight=1 fail_timeout=5s max_fails=3 backup; #hash算法不支持添加backup
}
[root@Centos8 conf]# nginx -s reload
源地址hash调度方法,基于的客户端的remote_addr(源地址IPv4的前24位或整个IPv6地址)做hash计
算,以实现会话保持。
[root@Centos8 conf]# vim /apps/nginx/conf/nginx.conf upstream webservers { #hash $request_uri consistent; #hash $cookie_sessionid;#的key的名称 ip_hash; #least_conn; server 10.0.0.7:80 weight=1 fail_timeout=5s max_fails=3; server 10.0.0.17:80 weight=1 fail_timeout=5s max_fails=3; # server 127.0.0.1:80 weight=1 fail_timeout=5s max_fails=3 backup; #hash算法不支持添加backup } [root@Centos8 conf]# nginx -s reload1.2.4 最少连接调度算法
least_conn;
最少连接调度算法,优先将客户端请求调度到当前连接最少的后端服务器,相当于LVS中的WLC
示例: 因业务需要,将访问源域名 www.mobile.com 的请求永久重定向到 www.newmobile.com。
2.1 永久重定向重写完成后以永久重定向方式直接返回重写后生成的新URL给客户端,由客户端重新发起请求,状态码:301。
[root@Centos8 conf]# cat conf.d/mobile.conf server{ listen 80; #listen 443 ssl; #ssl_certificate /apps/nginx/certs/www.mobile.com.pem; #ssl_certificate_key /apps/nginx/certs/www.mobile.com.key; #ssl_session_cache shared:sslcache:20m; #ssl_session_timeout 10m; server_name www.mobile.com; location / { root /data/nginx/html/mobile; index index.html; rewrite / http://www.newmobile.com permanent; } location /mobile_status { stub_status; } }2.2 临时重定向
域名临时重定向,告诉浏览器域名不是固定重定向到当前目标域名,后期可能随时会更改,因此浏览器不会缓存当前域名的解析记录,而浏览器会缓存永久重定向的DNS解析记录,这也是临时重定向与永久重定向最大的本质区别。即当nginx服务器无法访问时,浏览器不能利用缓存,而导致重定向失败。
[root@Centos8 conf]# cat conf.d/mobile.conf server{ listen 80; #listen 443 ssl; #ssl_certificate /apps/nginx/certs/www.mobile.com.pem; #ssl_certificate_key /apps/nginx/certs/www.mobile.com.key; #ssl_session_cache shared:sslcache:20m; #ssl_session_timeout 10m; server_name www.mobile.com; location / { root /data/nginx/html/mobile; index index.html; rewrite / http://www.newmobile.com redirect; } location /mobile_status { stub_status; } }三、实现反向代理客户端IP透传
后端RS服务器只能看到反向代理服务器地址
#Nginx配置 [root@Centos8 conf]# cat conf.d/pc.conf server { listen 80; proxy_cache off; #listen 443 ssl; #ssl_certificate /apps/nginx/certs/www.pc.com.pem; #ssl_certificate_key /apps/nginx/certs/www.pc.com.key; #ssl_session_cache shared:sslcache:20m; #ssl_session_timeout 10m; server_name www.pc.com; location / { index index.html index.php; root /data/nginx/html/pc; } location /web { index index.html root /data/nginx/html/mobile; proxy_set_header X-Real-IP $remote_addr; ##只添加客户端IP到 请求报文头部,转发至后端服务器 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #添加客户端IP和反 向代理服务器IP到请求报文头部 proxy_pass http://webservers/; } }
修改http服务器日志格式
#10.0.0.7,RS1 [root@rs1 ~]# vim /etc/httpd/conf/httpd.conf LogFormat "%{X-Forwarded-For}i %h %l %u %t "%r" %>s %b "%{Referer}i" "%{User-Agent}i"" combined #10.0.0.17,RS2 [root@rs2 ~]# vim /etc/httpd/conf/httpd.conf LogFormat "%{X-Real-IP}i %h %l %u %t "%r" %>s %b "%{Referer}i" "%{User-Agent}i"" combined 最后重启http服务
#环境部署 10.0.0.8:Nginx php-fpm 运行web服务 10.0.0.18:运行MySQL数据库
#10.0.0.8配置,php-fpm配置 [root@Centos8 ~]# yum install -y php php-json php-mysqlnd #php-fpm关键项设置 [root@Centos8 ~]# vim /etc/php-fpm.d/www.conf user = nginx group = nginx ;listen = /run/php-fpm/www.sock #套接字连接 listen = 127.0.0.1:9000 #tcp连接,启动服务会开启允许本地连接的9000端口 pm.status_path = /status#php验证 ping.path = /ping#php验证
#10.0.0.8 nginx虚拟主机配置 [root@Centos8 ~]# cat /apps/nginx/conf/conf.d/wordpress.conf server { listen 80; proxy_cache off; server_name blog.magedu.org; root /data/php; #若无添加,则无法自动找到PHP文件路径。 index index.php;#同上 location ~ .php$|status|ping { root /data/php; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param PATH_TRANSLATED $document_root$fastcgi_script_name; #$document_root调用此行的root指令指定的目录 include fastcgi_params; } } [root@Centos8 ~]# systemctl restart nginx.service php-fpm.service [root@Centos8 ~]# ss -ntl State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 0.0.0.0:80 0.0.0.0:* LISTEN 0 128 0.0.0.0:22 0.0.0.0:* LISTEN 0 100 127.0.0.1:25 0.0.0.0:* LISTEN 0 128 *:9000 *:* LISTEN 0 128 [::]:22 [::]:* LISTEN 0 100 [::1]:25 [::]:*
#准备WordPress文件 [root@Centos8 ~]# tar xf wordpress-5.8.2.tar.gz [root@Centos8 ~]# cp -r wordpress/* /data/php/ [root@Centos8 ~]# chown -R nginx:nginx /data/php/
#10.0.0.18 数据库服务器配置 [root@Centos8 ~]# mysql Welcome to the MariaDB monitor. Commands end with ; or g. Your MariaDB connection id is 8 Server version: 10.3.28-MariaDB MariaDB Server Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Type 'help;' or 'h' for help. Type 'c' to clear the current input statement. MariaDB [(none)]> create database wordpress; Query OK, 1 row affected (0.000 sec) MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | wordpress | +--------------------+ 4 rows in set (0.000 sec) MariaDB [(none)]> grant all on wordpress.* to wordpress@"10.0.0.%" identified by "123456"; Query OK, 0 rows affected (0.001 sec)
#授权给WordPress使用的账号及权限 [root@Centos8 ~]# mysql -uwordpress -p123456 -h 10.0.0.18 Welcome to the MariaDB monitor. Commands end with ; or g. Your MariaDB connection id is 10 Server version: 10.3.28-MariaDB MariaDB Server Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Type 'help;' or 'h' for help. Type 'c' to clear the current input statement. MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | wordpress | +--------------------+ 2 rows in set (0.001 sec)
测试登录验证
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)