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
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;
}
}
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