动力节点首页 全国咨询热线:400-8080-105

绑定手机号,登录
手机号

验证码

微信登录
手机号登录
手机号

验证码

微信登录与注册
微信扫码登录与注册

扫码关注微信公众号完成登录与注册
手机号登录
首页 > 文章

Nginx跳转实现方法

07-22 12:34 522浏览
举报 T字号
  • 大字
  • 中字
  • 小字

跳转实现方法

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表

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界面

动力节点在线课程涵盖零基础入门,高级进阶,在职提升三大主力内容,覆盖Java从入门到就业提升的全体系学习内容。全部Java视频教程免费观看,相关学习资料免费下载!对于火爆技术,每周一定时更新!如果想了解更多相关技术,可以到动力节点在线免费观看Nginx服务器视频教程学习哦!

0人推荐
共同学习,写下你的评论
0条评论
代码小兵696
程序员代码小兵696

118篇文章贡献392976字

作者相关文章更多>

推荐相关文章更多>

RabbitMQ使用及工作原理

代码小兵86504-19 19:55

Nginx反向代理的简单实例

代码小兵49806-21 15:40

Linux删除文件夹命令

代码小兵69607-21 11:32

Nginx配置详解

代码小兵12407-22 10:47

Linux更改ip地址的三种方式

代码小兵87207-21 12:51

发评论

举报

0/150

取消