zoukankan      html  css  js  c++  java
  • rewrite和return的简单需求

    Rewrite 需求作业

    背景:现在我有4个网站

    www.linux.com访问主页面

    friend.linux.com访问交友页面

    blog.linux.com访问博客页面

    download.linux.com访问博客页面

    在nginx上部署三套代码

    使用rewrite和return两种方式完成以下需求

    1、通过www.linux.com/download访问到下载页面

    2、通过www.linux.com/friend访问到交友页面

    3、通过www.linux.com/blog访问到博客页面

    以下代码可以部署到负载均衡上,可以起到为某一台服务器分流的目的

    部署网站

    [root@web03 ~]# vim /etc/nginx/conf.d/ln.conf 
    server {
            listen 80;
            server_name www.linux.com;
            
            location / { 
                    root /code/dist;
                    index index.html;
            }
    }
    
    server {
            listen 80;
            server_name friend.linux.com;
    
            location / {
                    root /code/friend;
                    index friend.html;
            }
    }
    
    server {
            listen 80;
            server_name blog.linux.com;
    
            location / {
                    root /code/blog;
                    index blog.html;
            }
    }
    
    server {
            listen 80;
            server_name download.linux.com;
    
            location / {
                    root /code/download;
                    index down.html;
            }
    }
    
    [root@web01 conf.d]# mkdir /code -p && cd /code
    #上传前端代码文件rz
    [root@web01 code]# unzip 下载页面.zip 
    [root@web01 code]# unzip 主页面.zip
    [root@web01 code]# unzip  交友页面.zip
    [root@web01 code]# unzip 博客页面.zip
    
    [root@web01 code]# nginx -sreload
    [root@web01 code]# nginx 
    

    QQ截图20200602222350.png

    rewrite重定向

    [root@web03 code]# vim /etc/nginx/conf.d/ln.conf
    server {
            listen 80;
            server_name www.linux.com;
    
            location / {
                    root /code/dist;
                    index index.html;
            }
    
            location ~* ^/(download|friend|blog) {
                    rewrite ^/(.*)$ http://$1.linux.com redirect;
            }
    }
    
    server {
            listen 80;
            server_name friend.linux.com;
    
            location / {
                    root /code/friend;
                    index friend.html;
            }
    }
    
    server {
            listen 80;
            server_name blog.linux.com;
    
            location / {
                    root /code/blog;
                    index blog.html;
            }
    }
    
    server {
            listen 80;
            server_name download.linux.com;
    
            location / {
                    root /code/download;
                    index down.html;
            }
    }
    
    

    QQ截图20200602221959.png

    return重定向

    [root@web03 code]# vim /etc/nginx/conf.d/ln.conf
    
    server {
            listen 80;
            server_name www.linux.com;
    
            location / {
                    root /code/dist;
                    index index.html;
            }
    
            location ~* ^/(download|friend|blog) {
                    return 302 http://$request_uri.linux.com;
            }
    }
    
    server {
            listen 80;
            server_name friend.linux.com;
    
            location / {
                    root /code/friend;
                    index friend.html;
            }
    }
    
    server {
            listen 80;
            server_name blog.linux.com;
    
            location / {
                    root /code/blog;
                    index blog.html;
            }
    }
    
    server {
            listen 80;
            server_name download.linux.com;
    
            location / {
                    root /code/download;
                    index down.html;
            }
    }
    
    [root@web03 code]# nginx -sreload
    
    

    tN5VkF.md.png

  • 相关阅读:
    LeetCode --- Climbing Stairs
    LeetCode --- Best Time to Buy and Sell Stock II
    LeedCode --- Best Time to Buy and Sell Stock
    git命令总结
    LeetCode --- Jump Game II
    Hdu 4497
    数据库lib7第4题创建存储过程
    Hdu 4496
    Hdu 4493
    快速排序
  • 原文地址:https://www.cnblogs.com/syy1757528181/p/13034324.html
Copyright © 2011-2022 走看看