rewrite和return的简单需求

Rewrite 需求作业

背景:现在我有一个网站,www.linux.com

www.linux.com访问主页面

friend.linux.com访问交友页面

blog.linux.com访问博客页面

download.linux.com访问博客页面

在nginx上部署三套代码

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

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

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

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 ^/(.*)$ //$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;
        }

        server_name www.linux.com;

        location / {
                root /code/dist;
                index index.html;
        }

        location ~* ^/(download|friend|blog) {
                return 302 //$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

Tags: