Nginx rewrite练习
1、访问ip/xcz,返回400状态码,要求用rewrite匹配/xcz
a、访问/xcz返回400
b、访问/hello时正常访问xcz.html页面
server {
listen 192.168.99.137:80;
server_name 192.168.99.137;
charset utf-8;
root /var/www/html;
location / {
root /var/www/html;
rewrite ^/xcz$ /q.html last;
rewrite ^/hello$ /xcz.html last;
index index.html index.htm index.php;
}
location = /q.html {
return 400;
}
}
2、访问http://kgc.com/ 时跳转至 http://jd.com
server {
listen 192.168.99.137:80;
server_name kgc.com;
charset utf-8;
root /var/www/html;
location / {
root /var/www/html;
if ($host ~* kgc.com ) {
rewrite .* http://jd.com permanent;
}
index index.html index.htm index.php;
}
}
windows hosts文件添加
192.168.99.137 kgc.com
3、访问http://kgc.com/a/1.html时跳转至http://jd.com/a/1.html
server {
listen 192.168.99.137:80;
server_name kgc.com;
charset utf-8;
root /var/www/html;
location / {
root /var/www/html;
if ($host ~* kgc.com ) {
rewrite /a/1.html http://jd.com/a/1.html permanent;
}
index index.html index.htm index.php;
}
}
4、通过http://kgc.com访问nginx根目录下的index.html
通过http://alice.kgc.com访问http://kgc.com/alice
通过http://jack.kgc.com访问http://kgc.com/jack
cd /var/www/html
mkdir jack alice
echo jack.... > jack/index.html
echo alice... > alice/index.html
windows hosts文件添加
192.168.99.137 kgc.com
192.168.99.137 jack.kgc.com
192.168.99.137 alice.kgc.com
ngnix.conf配置
server {
listen 192.168.99.137:80;
server_name kgc.com;
charset utf-8;
root /var/www/html;
location / {
root /var/www/html;
if ($host = kgc.com ) {
break;
}
if ( $host ~* "^(.*)\.kgc\.com$" ) {
set $user $1;
rewrite .* http://kgc.com/$user permanent;
}
}
location /jack {
root /usr/share/nginx/html;
index index.html index.hml;
}
location /alice {
root /usr/share/nginx/html;
index index.html index.hml;
}
}
通过http://alice.kgc.com访问http://kgc.com/alice
访问jack.kgc.com
5、将所有URL 重定向到加上 .html 后缀的 URL,例:aa.com/a==>aa.com/a.html。
server {
listen 192.168.99.137:80;
server_name aa.com;
charset utf-8;
root /var/www/html;
location / {
root /var/www/html;
if ($request_uri !~* "\.html$") {
rewrite ^/(.*)$ /$1.html break;
}
}
}
6、将所有 .html 结尾的 URL 重定向到去掉 .html 后缀的 URL,例:aa.com/a.html==>aa.com/a。
if ($request_uri ~* "\.html$") {
rewrite ^/(.*)\.html$ /$1 break;
}
7、将所有 /blog/post/<id>
的请求重定向到 /blog/article/<id>
,id为数字。
server {
listen 192.168.0.116:80;
server_name www.clean.com;
charset utf-8;
root /var/www/html/clean;
location ~ ^/blog/post/(\d+)/$ {
root /var/www/html/clean;
rewrite ^/blog/post/(\d+)/$ /blog/article/$1 last;
index index.html index.htm index.php;
}
location /article {
root /var/www/html/clean/blog;
index index.html index.htm index.php;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
访问192.168.0.116/post/1/
脚本:
#!/usr/bin/bash
read -p "测试题目号码:" num
#检查nginx配置文件是否正确,并根据结果重启nginx服务。
function nginx_test {
nginx -t &> /dev/null
if [ $? -eq 0 ]; then
systemctl restart nginx
echo 'nginx配置文件正确'
else
echo "nginx配置文件有误"
exit $num
fi
}
case $num in
1)
echo '访问ip/xcz,返回400状态码,要求用rewrite匹配/xcz,要求:a、访问/xcz返回400
b、访问/hello时正常访问xcz.html页面'
# 生成Nginx配置,重定向/xcz到/q.html返回400,/hello到/xcz.html
cat >/etc/nginx/conf.d/test0506.conf <<EOF
server {
listen 192.168.99.137:80;
server_name 192.168.99.137;
charset utf-8;
root /var/www/html;
location / {
root /var/www/html;
rewrite ^/xcz$ /q.html last;
rewrite ^/hello$ /xcz.html last;
index index.html index.htm index.php;
}
location = /q.html {
return 400;
}
}
EOF
# 创建xcz.html页面内容
cat >/var/www/html/xcz.html <<EOF
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>xcz</h1>
</body>
</html>
EOF
nginx_test
;;
2)
echo '访问http://kgc.com/ 时跳转至 http://jd.com'
cat >/etc/nginx/conf.d/test0506.conf <<EOF
server {
listen 192.168.99.137:80;
server_name kgc.com;
charset utf-8;
root /var/www/html;
location / {
root /var/www/html;
if ( \$host ~ kgc.com ) {
rewrite .* http://jd.com permanent;
}
index index.html index.htm index.php;
}
}
EOF
nginx_test
;;
3)
echo '访问http://kgc.com/a/1.html时跳转至http://jd.com/a/1.html'
cat >/etc/nginx/conf.d/test0506.conf <<EOF
server {
listen 192.168.99.137:80;
server_name kgc.com;
charset utf-8;
root /var/www/html;
location / {
root /var/www/html;
if ( \$host ~ kgc.com ) {
rewrite /a/1.html http://jd.com/a/1.html permanent;
}
index index.html index.htm index.php;
}
}
EOF
nginx_test
;;
4)
echo '通过http://kgc.com访问nginx根目录下的index.html
通过http://alice.kgc.com访问http://kgc.com/alice
通过http://jack.kgc.com访问http://kgc.com/jack'
cd /var/www/html
mkdir jack alice
echo jack.... >jack/index.html
echo alice... >alice/index.html
# 配置基于域名的根目录变更,测试并重启Nginx
cat >/etc/nginx/conf.d/test0506.conf <<EOF
server {
listen 192.168.99.137:80;
server_name kgc.com;
charset utf-8;
root /var/www/html;
location / {
root /var/www/html;
if ( \$host = kgc.com ) {
break;
}
if ( \$host ~* "^(.*)\.kgc\.com$" ) {
set \$user \$1;
rewrite .* http://kgc.com/\$user permanent;
}
}
location /jack {
root /usr/share/nginx/html;
index index.html index.hml;
}
location /alice {
root /usr/share/nginx/html;
index index.html index.hml;
}
}
EOF
nginx_test
;;
5)
# 题目5:处理URL添加.html后缀的重定向规则
echo '将所有URL 重定向到加上 .html 后缀的 URL,例:aa.com/a==>aa.com/a.html。'
cat >/etc/nginx/conf.d/test0506.conf <<EOF
server {
listen 192.168.99.137:80;
server_name aa.com;
charset utf-8;
root /var/www/html;
location / {
root /var/www/html;
if (\$request_uri !~* "\.html$") {
rewrite ^/(.*)$ /\$1.html break;
}
}
}
EOF
# 创建示例页面jkl.html,进行配置、测试、重启
cat >/var/www/html/jkl.html <<EOF
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>jkl</h1>
</body>
</html>
EOF
nginx_test
;;
6)
# 题目6:处理URL移除.html后缀的重定向规则
echo '将所有 .html 结尾的 URL 重定向到去掉 .html 后缀的 URL,例:aa.com/a.html==>aa.com/a'
cat >/etc/nginx/conf.d/test0506.conf <<EOF
server {
listen 192.168.99.137:80;
server_name aa.com;
charset utf-8;
root /var/www/html;
location / {
root /var/www/html;
rewrite ^/(.*)\.html$ /\$1 permanent;
}
location /jk {
root /var/www/html;
index index.html index.htm index.php;
}
}
EOF
mkdir -p /var/www/html/jk
touch /var/www/html/jk/index.html
cat >/var/www/html/jk/index.html <<EOF
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>jk666</h1>
</body>
</html>
EOF
nginx_test
;;
7)
echo '将所有 /blog/post/<id> 的请求重定向到 /blog/article/<id>,id为数字。'
cat >/etc/nginx/conf.d/test0506.conf <<EOF
server {
listen 192.168.99.137:80;
server_name 192.168.99.137;
charset utf-8;
root /var/www/html;
location ~ ^/blog/post/(\d+)/$ {
root /var/www/html;
rewrite ^/blog/post/(\d+)/$ /blog/article/\$1 last;
index index.html index.htm index.php;
}
location /post {
root /var/www/html/blog;
index index.html index.htm index.php;
}
location /article {
root /var/www/html/blog;
index index.html index.htm index.php;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
EOF
# 创建必要的目录结构和索引文件,配置重定向规则,测试并重启
mkdir -p /var/www/html/blog/article/1
touch /var/www/html/blog/article/1/index.html
mkdir -p /var/www/html/blog/post/1
touch /var/www/html/blog/post/1/index.html
cat >/var/www/html/blog/article/1/index.html <<EOF
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>1</h1>
</body>
</html>
EOF
nginx_test
;;
*)
# 如果输入的不是1-7中的任意一个数字,显示错误信息并退出
echo "一共7题,输入错误"
exit 8
;;
esac