马哥博客作业第十九周

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

  在Nginx配置文件中新建一个虚拟主机的子配置文件,配置信息如下:

  [root@localhost nginx]#vim conf/conf.d/pc.conf 

  server {
    listen 80;
    server_name www.magedu.org;
    root /apps/nginx/html;
    index index.html;   

    location / {
      return 301 https://www.magedu.com;

      }

     }

  检查Nginx配置文件语法:nginx -t

  重新载入Nginx配置文件使其生效:nginx -s reload

 

 

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

  修改Nginx虚拟主机的配置文件,配置信息如下: 

  [root@localhost nginx]#vim conf/conf.d/pc.conf 

  server {
    listen 80;
    server_name www.magedu.org;
    root /apps/nginx/html;
    index index.html;   

    location / {
      if ( !-e $request_filename ) {

      rewrite .* https://www.magedu.com redirect;

        }

      }

     }

  检查Nginx配置文件语法:nginx -t

  重新载入Nginx配置文件使其生效:nginx -s reload

 

 

3、用 nginx 做一个代理服务器,server_name 为 www.magedu.org,代理后端两台 apache 服务器。并且要求使用最少连接调度算法实现,这样才能做到后端 apache 服务器的压力达到均衡

  环境说明:

  10.0.0.212——Nginx反向代理服务器

  10.0.0.214——后端apache服务器

  10.0.0.215——后端apache服务器

  在10.0.0.212上配置www.magedu.org虚拟主机的子配置文件,配置信息如下:

  [root@localhost nginx]#vim conf/conf.d/pc.conf

  http {    

    upstream backend {

    least_conn;
    server 10.0.0.214 weight=1;
    server 10.0.0.215 weight=1;

      }

    server {
    listen 80;
    server_name www.magedu.org;
    root /apps/nginx/html;
    index index.html;   

    location / {
      proxy_pass http://backend;

      }

      }

    }

  

  检查Nginx配置文件语法:nginx -t

  重新载入Nginx配置文件使其生效:nginx -s reload

 

 

 

 

 

原文地址:https://www.cnblogs.com/babyblue3/p/13806816.html