天天看点

LNMP - nginx域名跳转

域名重定向 301 302

一个网站有多个域名,一个为主,其他为辅,

我的discuz网站有两个域名: 1、www.test.com 2、www.aa.com

现在的目的是当我访问 www.aaa.com的时候永久重定向到www.test.com

1、修改虚拟主机配置文件

[[email protected] ~]# vim /usr/local/nginx/conf/vhosts/test.conf

server

{

    listen 80;

    server_name www.test.com www.aaa.com;

    if ($host != 'www.test.com')

    {

     rewrite ^/(.*)$ http://www.test.com/$1 permanent;

    }

    index index.html index.htm index.php;

    root /data/www;

    location ~ .*admin\.php$ {

       auth_basic "caimz auth";

     auth_basic_user_file /usr/local/nginx/conf/.htpasswd;

        include fastcgi_params;

        fastcgi_pass unix:/tmp/php-fcgi1.sock;

        fastcgi_index index.php;

        fastcgi_param SCRIPT_FILENAME /data/www$fastcgi_script_name;

    }

    location ~ \.php$ {

        include fastcgi_params;

        fastcgi_pass unix:/tmp/php-fcgi1.sock;

        fastcgi_index index.php;

        fastcgi_param SCRIPT_FILENAME /data/www$fastcgi_script_name;

    }

}

LNMP - nginx域名跳转

2、检测配置文件正确性

[[email protected] ~]# /usr/local/nginx/sbin/nginx -t

nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok

nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

3、重新加载配置文件

[[email protected] ~]# /usr/local/nginx/sbin/nginx -s reload

[[email protected] ~]# 

4、测试

[[email protected] ~]# curl -x127.0.0.1:80 www.aaa.com/dkwdw -I

HTTP/1.1 301 Moved Permanently

Server: nginx/1.6.2

Date: Sun, 25 Oct 2015 03:44:16 GMT

Content-Type: text/html

Content-Length: 184

Connection: keep-alive

Location: http://www.test.com/dkwdw  #自动跳转到www.test.com/dkwdw  

[[email protected] ~]# curl -x127.0.0.1:80 www.bbb.com/dkwdw -I    #因为不存在这个域名所以访问出错

HTTP/1.1 403 Forbidden

Server: nginx/1.6.2

Date: Sun, 25 Oct 2015 03:44:29 GMT

Content-Type: text/html

Content-Length: 168

Connection: keep-alive

[[email protected] ~]# curl -x127.0.0.1:80 www.test.com -I 

HTTP/1.1 301 Moved Permanently

Server: nginx/1.6.2

Date: Sun, 25 Oct 2015 03:54:03 GMT

Content-Type: text/html; charset=UTF-8

Connection: keep-alive

X-Powered-By: PHP/5.6.6

location: forum.php

课后扩展:

首先看一个完整代码示例,关于nginx 301 302跳转的。

301跳转设置:

server {

listen 80;

server_name 123.com;

rewrite ^/(.*) http://456.com/$1 permanent;

access_log off;

}

302跳转设置:

server {

listen 80;

server_name 123.com;

rewrite ^/(.*) http://456.com/$1 redirect;

access_log off;

}

在看下关于nginx 301 302跳转的详细说明文档

server {

server_name test.com;

rewrite ^/(.*) http://www.test1.com/$1 permanent;

}

last – 基本上都用这个Flag。

break – 中止Rewirte,不在继续匹配   跳转的时候主页的网址不变

redirect – 返回临时重定向的HTTP状态302

permanent – 返回永久重定向的HTTP状态301

Nginx的重定向用到了Nginx的HttpRewriteModule,下面简单解释以下如何使用的方法:

rewrite命令

nginx的rewrite相当于apache的rewriterule(大多数情况下可以把原有apache的rewrite规则加上引号就可以直接使用),它可以用在server,location 和IF条件判断块中,命令格式如下:

rewrite 正则表达式 替换目标 flag标记

flag标记可以用以下几种格式:

last – 基本上都用这个Flag。

break – 中止Rewirte,不在继续匹配

redirect – 返回临时重定向的HTTP状态302

permanent – 返回永久重定向的HTTP状态301

特别注意:

last和break用来实现URL重写,浏览器地址栏的URL地址不变,但是在服务器端访问的路径发生了变化;

redirect和permanent用来实现URL跳转,浏览器地址栏会显示跳转后的URL地址;

例如下面这段设定nginx将某个目录下面的文件重定向到另一个目录,$2对应第二个括号(.*)中对应的字符串:

location /download/ {

rewrite ^(/download/.*)/m/(.*)\..*$ $1/nginx-rewrite/$2.gz break;

}

nginx重定向的IF条件判断

在server和location两种情况下可以使用nginx的IF条件判断,条件可以为以下几种:

正则表达式

如:

匹配判断

~ 为区分大小写匹配; !~为区分大小写不匹配

~* 为不区分大小写匹配;!~为不区分大小写不匹配

例如下面设定nginx在用户使用ie的使用重定向到/nginx-ie目录下:

if ($http_user_agent ~ MSIE) {

rewrite ^(.*)$ /nginx-ie/$1 break;

}

文件和目录判断

-f和!-f判断是否存在文件

-d和!-d判断是否存在目录

-e和!-e判断是否存在文件或目录

-x和!-x判断文件是否可执行

例如下面设定nginx在文件和目录不存在的时候重定向:

if (!-e $request_filename) {

proxy_pass http://127.0.0.1;

}

return

返回http代码,例如设置nginx防盗链:

location ~* \.(gif|jpg|png|swf|flv)$ {

valid_referers none blocked www.test.com www.test1.com;

if ($invalid_referer) {

return 404;

}

转载于:https://blog.51cto.com/caimengzhi/1706016