1、实现永久重定向,当用户访问 www.magedu.org 这个域名时我想让他跳转到 www.magedu.com 的主页面,请写出配置过程
第一台服务器:www.tianze.org
[root@centos8-1 conf.d]$vim /apps/nginx/conf/conf.d/pc.conf
location / {
root /data/nginx/html/pc;
index index.html;
[root@centos8-1 conf.d]$nginx -s reload
[root@centos8-1 ~]$vim /data/nginx/html/pc/index.html ##配置Web页面
pc web is www.tianze.org
第二台服务器:www.tianzee.org
[root@centos8-2 ~]$vim /etc/nginx/nginx.conf
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name www.tianzee.org;
location / {
index index.html;
root /data/nginx/html/tianzee;
}
[root@centos8-2]$nginx -s reload
[root@centos8-2 ~]$vim /data/nginx/html/tianzee/index.html ##配置Web页面
<h1>web site on 10.0.0.18 www.tianzee.org WEB2</h1>
客户端测试两个网站,正常访问
[root@centos7-1 ~]$curl www.tianze.org
pc web is www.tianze.org
[root@centos7-1 ~]$curl www.tianzee.org
<h1>web site on 10.0.0.18 www.tianzee.org WEB2</h1>
第一台服务器:www.tianze.org 配置永久重定向
[root@centos8-1 conf.d]$vim /apps/nginx/conf/conf.d/pc.conf
location / {
root /data/nginx/html/pc;
index index.html;
rewrite / http://www.tianzee.org permanent; ##添加rewrite 设定访问www.tianze.org的/时 永久重定向到www.tianzee.org
[root@centos8-1 conf.d]$nginx -s reload ##重新加载nginx生效配置
再次访问www.tianze.org,看到首部字段出现301永久重定向
[root@centos7-1 ~]$curl www.tianze.org -iL
HTTP/1.1 301 Moved Permanently
Server: nginx/1.18.0
Date: Tue, 13 Oct 2020 04:25:45 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive
Location: http://www.tianzee.org
X-Via: 10.0.0.8
X-Accel: www.tianze.org
HTTP/1.1 200 OK
Server: nginx/1.14.1
Date: Tue, 13 Oct 2020 04:25:45 GMT
Content-Type: text/html
Content-Length: 37
Last-Modified: Mon, 12 Oct 2020 03:35:57 GMT
Connection: keep-alive
ETag: "5f83cf1d-25"
Accept-Ranges: bytes
<h1>web site on 10.0.0.18 WEB2</h1> ##来自重定向到www.tianzee.org的web页面内容
2、rewrite案例-判断文件是否存在,要求:当用户访问到公司网站的时输入了一个错误的 URL ,可以将用户重定向至 www.magedu.com 官网首页。请写出配置过程
配置Reweite 避免用户输入错误URL,出现404造成用户不好的浏览体验
在Nginx服务器的配置文件添加如下配置:
[root@centos8-1 ~]$vim /apps/nginx/conf/conf.d/pc.conf
location / {
root /data/nginx/html/pc;
index index.html;
if (!-e $request_filename) {
rewrite .* http://www.tianze.org/index.html;
}
[root@centos8-1 ~]$nginx -s reload
测试访问,自动跳转到rewrite自定义的URL
3、用 nginx 做一个代理服务器,server_name 为 www.magedu.org,代理后端两台 apache 服务器。并且要求使用最少连接调度算法实现,这样才能做到后端 apache 服务器的压力大到均衡
Nginx服务器配置
[root@centos8-1 ~]$vim /apps/nginx/conf/nginx.conf
http {
include mime.types;
default_type application/octet-stream;
proxy_cache_path /data/nginx/proxycache levels=1:1:1 keys_zone=proxycache:20m inactive=120s max_size=1g;
upstream webserver { ##定义主机组
hash $cookie_name; ##利用cookie做hash绑定
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;#后端服务器状态监测
location /web { ##用户访问www.tianze.org/web
index index.html;
proxy_pass http://webserver/; ##转给后端定义的webserver组里的服务器
}
[root@centos8-1 ~]$nginx -s reload
后端第一台服务器
##编辑Web页面,方便测试访问
[root@centos8-2 ~]$vim /var/www/html/index.html
<h1>web site on 10.0.0.18 WEB1</h1>
后端第二台服务器
##编辑Web页面,方便测试访问
[root@centos8-3 ~]$vim /var/www/html/index.html
<h1>web site on 10.0.0.28 WEB2</h1>
客户端访问测试
[root@centos7-1 ~]$curl www.tianze.org/web
<h1>web site on 10.0.0.28 WEB2</h1>
[root@centos7-1 ~]$curl www.tianze.org/web
<h1>web site on 10.0.0.18 WEB1</h1>