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: