第一种:rewrite 更多参考另一个blog
按照常规理解,实现重定向就是要用rewrite来实现,例如demo:
[root@nginx01 ~]# vi /etc/nginx/conf.d/rewrite01.conf
server {
listen 80;
server_name cnblogs.linuxds.com;
access_log /var/log/nginx/cnblogs.access.log main;
error_log /var/log/nginx/cnblogs.error.log warn;
location / {
if ($host = 'cnblogs.linuxds.com') {
rewrite ^/(.*) http://www.cnblogs.com redirect;
}
}
}
配置解释:结合if指令来对nginx请求进行判断,若访问http://cnblogs.linuxds.com/index/1.html,即$host = 'cnblogs.linuxds.com' 的时候,进行重定向跳转,重定向至 http://www.cnblogs.com。
使用rewrite重定向,浏览器链接是会发生变化:http://www.cnblogs.com
后缀index/1.html 是没有跟上的
第二种:反向代理
有些场景需要内容重定向到另外一个url, 但是链接保持不变,这时候就可以利用反向代理配置来达到目的。cc/1.html 是不会变的
location /abc/ {
proxy_pass http://localhost:8101;
}
请求 http://myip/abc/cc/1.html 实际请求 http://localhost:8101/abc/cc/1.html
后缀是不变的
location /abc/ {
proxy_pass http://localhost:8101/;
}
请求 http://myip/abc/cc/1.html 实际请求 http://localhost:8101/cc/1.html
后缀是不变的