写location最重要的就是hosts文件,添加一次域名跳转就得添加一条hosts文件
hosts文件:
192.168.200.120 www.a.com 192.168.200.119 www.b.com 192.168.200.119 www.gx.com 192.168.200.119 www.liuxiang.com 192.168.200.119 www.img.com
例题:
第一题:输入www.b.com时网页跳转到www.jd.com
第二题:输入www.gx.xom时网页跳转到www.taobao.com
第三题:输入www.liuxiang.com/cai.com时网页跳转到自定义的cao.html
第四题:输入www.img.com/images/时网页跳转到自定义的img.html下
[root@localhost html]# vim cao.html //自定义的页面
<h1>caomengxia</h1>
[root@localhost html]# ls
50x.html cao.html error.jpg img.html index.html
[root@localhost html]# vim ../conf/nginx.conf //不需再添加一个server直接写if或者location即可
server {
listen 80;
server_name locahost;
charset uft-8;
access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
if ($host = "www.b.com") { //第一题
rewrite (.*) http://www.jd.com;
}
if ($host = "www.gx.com") { //第二题
rewrite (.*) http://www.taobao.com;
}
location ~ cai.html { //第三题
autoindex on;
rewrite (.*) http://www.liuxiang.com/cao.html;
}
location ^~ /images/ { //第四题
rewrite (.*) http://www.img.com/img.html;
}