天天看點

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