第19周作业
1、实现永久重定向,当用户访问 www.magedu.org 这个域名时我想让他跳转到 www.magedu.com 的主页面,请写出配置过程
[root@centos8 ~]#vi /apps/nginx/conf/conf.d/pc.conf server { listen 80; server_name www.magedu.org; error_page 404 /index.html; location / { root /data/nginx/html/pc; rewrite / http://www.magedu.com permanent; }
2、rewrite案例-判断文件是否存在,要求:当用户访问到公司网站的时输入了一个错误的 URL ,可以将用户重定向至 www.magedu.com 官网首页。请写出配置过程
[root@centos8 ~]#vi /apps/nginx/conf/conf.d/pc.conf location / { root /data/nginx/html/pc; index index.html; if (!-e $request_filename) { rewrite .* http://www.magedu.org/index.html; } }
3、用 nginx 做一个代理服务器,server_name 为 www.magedu.org,代理后端两台 apache 服务器。并且要求使用最少连接调度算法实现,这样才能做到后端 apache 服务器的压力大到均衡
原理图:

[root@centos8 ~]#vi /apps/nginx/conf/nginx.conf http { upstream webserver { least_conn; server 10.0.0.18:80 weight=1 fail_timeout=5s max_fails=3; server 10.0.0.28:80 weight=1 fail_timeout=5s max_fails=3; } } #以上只能修改主配置文件,不能修改conf.d下的自定义配置文件 [root@centos8 ~]#cat /apps/nginx/conf/conf.d/pc.conf server { listen 80; server_name www.magedu.org; location / { index index.html index.php; root /data/nginx/html/pc; } location /web { index index.html; proxy_pass http://webserver/; } } [root@centos8 ~]#nginx -s reload
#访问测试
[root@redhat6 ~]#while :;do curl www.magedu.org/web;sleep 1;done
web1 site on 10.0.0.18
web2 site on 10.0.0.28
web1 site on 10.0.0.18
web2 site on 10.0.0.28
web1 site on 10.0.0.18
web2 site on 10.0.0.28
web1 site on 10.0.0.18

浙公网安备 33010602011771号