1)301重定向,把blog.moonsbird.com和moonsbirl.com合併,並把以前的域名也一併合併.html
有兩種實現方法,第一種方法是判斷nginx核心變量host(老版本是http_host):nginx
第一種方法:curl
server { server_name blog.moonsbird.com moonsbird.com; if ($host = 'moonsbird.com' ) { rewrite ^/(.*)$ http://blog.moonsbird.com/$1 permanent; } ... }
第二種方法:測試
server { server_name blog.moonsbird.com moonsbird.com; if ($host != 'blog.moonsbird.com' ) { rewrite ^/(.*)$ http://www.moonsbird.com/$1 permanent; } ... }
這兩種方法中, permanent是關鍵,詳細說明見nginx重定向規則說明。url
last – 基本上都用這個Flag。
break – 停止Rewirte,不在繼續匹配
redirect – 返回臨時重定向的HTTP狀態302
permanent – 返回永久重定向的HTTP狀態301code
2)檢查配置文件語法server
/usr/local/nginx/sbin/nginx -t
提示:
the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok configuration file /usr/local/nginx/conf/nginx.conf test is successful
測試成功, 重啓nginx,輸入命令:
/usr/local/nginx/sbin/nginx -s reload
3)測試一是否成功設定完成
curl –I moonsbird.com
會輸出
HTTP/1.1 301 Moved Permanently Server: nginx Date: Sat, 15 Apr 2017 03:38:52 GMT Content-Type: text/html Content-Length: 178 Connection: keep-alive Location: http://blog.moonsbird.com/ Strict-Transport-Security: max-age=15768000