Q1、rewrite几种规则案例演示区别
[root@Centos7 conf.d]# vim web.conf
server_tokens off;
server {
listen 192.168.37.87:80;
listen 192.168.37.87:443 ssl;
server_name test.magedu.net;
access_log logs/test.magedu.net.access.log json;
ssl_certificate /apps/nginx/ssl/magedu.net.crt;
ssl_certificate_key /apps/nginx/ssl/magedu.net.key;
ssl_session_cache shared:sslcache:20m;
ssl_session_timeout 10m;
location / {
root /data/site1;
index index.html;
if ( $scheme = http ) {
rewrite ^/(.*)$ https://test.magedu.net/$1 permanent; #http跳转到https
}
if ( !-f $request_filename ) {
rewrite .* http://test.magedu.net/index.html; #输入错误地址跳转到主页
}
}
location /last {
rewrite ^/last/(.*)$ /info/$1 last; #访问last目录会跳转到info目录
return 888 "last";
}
}
[root@Centos7 conf.d]# nginx -s reload
[root@Centos7 conf.d]# mkdir /data/site1/{last,info}
[root@Centos7 conf.d]# echo infomation > /data/site1/info/index.html
[root@Centos7 conf.d]# chown nginx.nginx -R /data/site1/
#客户端测试
[root@centos6 ~]$ curl test.magedu.net #跳转到https
<html>
<head><title>301 Moved Permanently</title></head>
<body>
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx</center>
</body>
</html>
[root@centos6 ~]$ curl -kL test.magedu.net
test
[root@centos6 ~]$ curl -kL test.magedu.net/1234412 #错误页面跳转到主页
test
[root@centos6 ~]$ curl -kL test.magedu.net/last/index.html #访问last跳转到info
infomation
Q2、使用proxy反向代理后端多台web服务器
[root@Centos7 conf.d]# vim web.conf
server_tokens off;
upstream local {
server 192.168.37.67;
server 192.168.37.77;
server 127.0.0.1:9001 backup;
}
server {
listen 80;
server_name test.magedu.net;
location / {
proxy_pass http://local;
}
}
server {
listen 127.0.0.1:9001;
root /data/site2;
index index.html;
access_log logs/test.magedeu.net.sorry.log;
}
[root@Centos7 conf.d]# echo sorry > /data/site2/index.html
#客户端测试
[root@centos6 ~]$ while :;do curl test.magedu.net;sleep 0.5;done
RS2
RS1
RS2
RS1
RS2
RS1
RS2
RS1
RS2
RS1
RS2
RS1
RS2
RS1
RS2
RS1
RS2
网友评论