nginx跳转实现方法

一、跳转实现方法
1、别名:在配置文件里server段加入wolf.com即可实现
2、重写(rewrite)
第一种方法地址栏不变,第二种方法地址栏会跳转变化。


二、实验
1、配置文件加入wolf.com
[root@nginx conf]# vi nginx.conf
\


worker_processes  2;
events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  www.wolf.com wolf.com;  (这里加入wolf.com)
        location / {
            root   html/www;
            index  index.html index.htm;
        }
    }
        server {
        listen       80;
        server_name  bbs.wolf.com;
        location / {
            root   html/bbs;
            index  index.html index.htm;
        }
    }
        server {
        listen       80;
        server_name  blog.wolf.com;
        location / {
            root   html/blog;
            index  index.html index.htm;
        }
    }
客户端测试加入host表
http://wolf.com   (最好是登录网页测试http://wolf.com )




2、重写301跳转
rewrite ^/(.*) http://www.wolf.com/$1 permanent;


[root@nginx conf]# cat nginx.conf         


worker_processes  2;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  wolf.com;
        location / {
            root   html/www;
            index  index.html index.htm;
            rewrite ^/(.*) http://www.wolf.com/$1 permanent;
        }
    }


        server {
        listen       80;
        server_name  www.wolf.com;
        location / {
            root   html/www;
            index  index.html index.htm;
        }
    }
        server {
        listen       80;
        server_name  bbs.wolf.com;
        location / {
            root   html/bbs;
            index  index.html index.htm;
        }
    }
        server {
        listen       80;
        server_name  blog.wolf.com;
        location / {
            root   html/blog;
            index  index.html index.htm;
        }
    }
##status
   server {
        listen       80;
        server_name  status.wolf.com;
        location / {
            stub_status on;
            access_log off;
        }
    }
}
再输入wolf.com,自动跳转到www.wolf.com
现在输入192.168.3.49,自动跳转到www.wolf.com


如果没有跳转段
    server {
        listen       80;
        server_name  wolf.com;
        location / {
            root   html/www;
            index  index.html index.htm;
            rewrite ^/(.*) http://www.wolf.com/$1 permanent;
        }
    }
使用ip访问默认为第一个server


3、防止恶意绑定域名
加入下面这段
server {
        listen       80;
        location / {
          deny all;
        }
    }
nginx: the configuration file /data/nginx1.8.1/conf/nginx.conf syntax is ok
nginx: configuration file /data/nginx1.8.1/conf/nginx.conf test is successful
[root@nginx conf]# ../sbin/nginx -s reload


测试curl或者ie访问都会报403错误
[root@nginx conf]# curl -I http://192.168.3.49/
HTTP/1.1 403 Forbidden
Server: nginx/1.8.1
Date: Wed, 16 Nov 2016 05:49:12 GMT
Content-Type: text/html
Content-Length: 168
Connection: keep-alive


hosts表里添加记录(恶意解析)
192.168.3.49 www.sex.com
在本地ie打开网页
会发现跳转到403界面













  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值