马哥博客作业第十九周

1、实现永久重定向,当用户访问 www.magedu.org 这个域名时我想让他跳转到
www.magedu.com 的主页面,请写出配置过程
定义子配置文件路径:

[root@localhost ~]# vim /apps/nginx/conf/nginx.conf

http{

    ......

    include /apps/nginx/conf/conf.d/*.conf

}

修改nginx配置文件,内容如下:

[root@localhost ~]# vim /apps/nginx/conf/conf.d/pa.conf

server {

    listen 80;

    server_name www.magedu.org;

    charset utf-8;

     location / {

        root /data/www;

        index index.html index.htm;

        rewrite ^(.*)$ http://www.magedu.com permanent;

     }

}

[root@localhost ~]# nginx -t

[root@localhost ~]# nginx -s reload

2、rewrite案例-判断文件是否存在,要求:当用户访问到公司网站的时输入了一个错误的 URL ,可以将用户重定向至 www.magedu.com 官网首页。请写出配置过程

[root@localhost ~]# vim /apps/nginx/conf/conf.d/pb.conf

server {

    listen 80;

    server_name www.magedu.org;

    charset utf-8;

    location / {

        root /data/www;

        index index.html index.htm;

        if (!-e $request_filename) {

        rewrite ^(.*)$ http://www.magedu.com;

        }

    }

}

[root@localhost ~]# nginx -t

[root@localhost ~]# nginx -s reload

3、用 nginx 做一个代理服务器,server_name 为 www.magedu.org,代理后端两台 apache 服务器。并且要求使用最少连接调度算法实现,这样才能做到后端 apache 服务器的压力大到均衡
修改nginx主配置文件,内容如下:

[root@localhost ~]# vim /apps/nginx/conf/nginx.conf

upstream webserver {

    least_conn;

    server 10.0.0.11;

    server 10.0.0.12;

    }

server {

    listen 80;

    server_name www.magedu.org;

    charset utf-8;

    location / {

        proxy_pass http://webserver;

        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

    }

}

检查Nginx语法

[root@localhost ~]# nginx -t

[root@localhost ~]# nginx -s reload

posted @ 2020-10-19 20:22  空白的旋律  阅读(67)  评论(0编辑  收藏  举报