菜鸟笔记
提升您的技术认知

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