域名重定向是經過設置web服務的配置文件,將本來訪問A域名的請求訪問到B域名。php
一個比較簡單的例子:以前作了一個網站,域名又長又複雜,如今申請了一個新的域名,又短又容易記住(或者是域名能跟品牌匹配),想把新的域名作成主域名來推廣,可是有些用戶可能將老的域名收藏了,習慣老的域名訪問。這時候就能夠作域名重定向,將老的域名重定向到新的域名。這樣無論是老用戶經過老的域名訪問,仍是新用戶經過新的域名訪問,最終跳轉到的域名都是新的域名。html
server { listen 80; server_name blog.abc.com www.lcblog.com;
像上面這樣作(假設兩個域名均添加到dnspot中),兩個域名是均可以正常訪問網站的,可是它們並無重定向,也就是搜索引擎中給它們的權重不會發生變化,這樣對咱們新的域名的推廣不太友好。linux
server { listen 80; server_name blog.abc.com www.lcblog.com; if ( $host = blog.abc.com ) { rewrite /(.*) http://www.lcblog.com/$1 permanent; }
在配置文件中添加了if以及後面的這些內容。意思就是當訪問blog.abc.com時,將blog.abc.com重定向到www.lcblog.com 後面的內容保持不變,好比blog.abc.com/index.php→www.lcblog.com/index.php; permanent就是定義的301,表示永久重定向。 nginx -t nginx -s reload 驗證有無語法錯誤,從新加載配置文件nginx
[root@localhost blog.abc.com]# curl -x127.0.0.1:80 -I blog.abc.com/1.test 使用的是舊域名 HTTP/1.1 301 Moved Permanently Server: nginx/1.14.2 Date: Sun, 17 Feb 2019 06:30:57 GMT Content-Type: text/html Content-Length: 185 Connection: keep-alive Location: http://www.lcblog.com/1.test 實際訪問的是新域名下的內容
爲了站點的安全,能夠經過修改配置文件來針對一些重要的目錄(站點後臺地址)進行用戶認證git
location ~ admin.php { auth_basic "Auth"; auth_basic_user_file /etc/nginx/user_passwd; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /data/wwwroot/blog.abc.com$fastcgi_script_name; include fastcgi_params; }
[root@localhost blog.abc.com]# yum install httpd-tools -y [root@localhost blog.abc.com]# htpasswd -c /etc/nginx/user_passwd lisi New password: Re-type new password: Adding password for user lisi
[root@localhost blog.abc.com]# htpasswd -m /etc/nginx/user_passwd zhangsan
nginx location優先級github
location / 優先級比 location ~ 要低,也就是說,若是一個請求(如,aming.php)同時知足兩個location location /amin.php location ~ *.php$ 會選擇下面的 nginx location 文檔: https://github.com/aminglinux/nginx/tree/master/location