最近將wordpress部署到CentOS 7上,配置好Nginx,server_name
但總是莫名地從定向到開發的端口上
例如:
開發測試用的是http://localhost:8089
部署到服務器上,server_name設置爲http://www.chenkezhao.top
然後訪問http://www.chenkezhao.top,總被莫名地重定向到http://www.chenkezhao.top:8089
真悲劇!!!
話說我是怎麼發現是重定向的
爲了方便在服務器上使用curl測試發現的
[root@... sbin]# curl -I http://localhost
HTTP/1.1 301 Moved Permanently
Server: nginx/1.12.2
Date: Thu, 30 Nov 2017 12:26:10 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/7.1.12
Location: http://localhost:8089/
將www重定向到非www
如果要將用戶從www重定向到普通的非www域,請插入此配置:
server {
server_name www.example.com;
return 301 $scheme://example.com$request_uri;
}
重新啓動Nginx
認證:使用此curl命令確保非www域重定向到www域
curl -I http://www.example.com
你應該得到一個301 Moved Permanently響應,顯示非www重定向位置,如下所示:
HTTP/1.1 301 Moved Permanently
Server: nginx/1.4.6 (Ubuntu)
Date: Mon, 04 May 2015 18:20:19 GMT
Content-Type: text/html
Content-Length: 193
Connection: keep-alive
Location: http://example.com/
當然,您應該在網絡瀏覽器(www和非www)中訪問您的域名。
將非www重定向到www
如果要將用戶從普通的非www域重定向到www域,請添加以下服務器塊:
server {
server_name example.com;
return 301 $scheme://www.example.com$request_uri;
}
重新啓動Nginx
認證:使用此curl命令確保非www域重定向到www域
curl -I http://www.example.com
你應該得到一個301 Moved Permanently響應,顯示www重定向位置,如下所示:
HTTP/1.1 301 Moved Permanently
Server: nginx/1.4.6 (Ubuntu)
Date: Mon, 04 May 2015 18:20:19 GMT
Content-Type: text/html
Content-Length: 193
Connection: keep-alive
Location: http://www.example.com/
當然,您應該在網絡瀏覽器(www和非www)中訪問您的域名。
結論
您的Nginx永久重定向現在已經正確配置,您的用戶將能夠通過您的非www和www域訪問您的web服務器。