NGINX 301 DOMAIN REDIRECT -- 更新域名,旧域名重定向
网站换了新域名,如何使用 Nginx 设置将新域名添加到服务器,将旧域名重新定向到新域名?
After changing the domain, how to redirect or rewrite the old domain to new domain through port 80 and 443 ssl?
以凹大卜的网站为例来展示如何操作:
Here is an example showing how to update and redirect domains:
#旧域名
old domain: sla-dk.com
#新域名
new domain: aodabo.tech
First step, set up your new domain in Nginx, including two servers. One server listen to port 80, and redirect all the http requests to https. The other server listen to port 443 ssl and response all the https requests.
第一步,先要设置新的域名在Nginx服务。其中包括了监听并重定向80端口,和监听443端口:
# 设置新域名
# 监听80端口
server {
# 监听80端口,作用是将用户http的请求转发到https
listen 80;
# 绑定的域名,将带www和不带www都写上,以防疏漏
server_name aodabo.tech www.aodabo.tech;
rewrite ^(.*)$ https://aodabo.tech permanent;
}
# 监听443端口
server {
listen 443;
server_name aodabo.tech www.aodabo.tech;
ssl on;
#ssl认证秘钥,填写两个秘钥文件在服务器上的绝对路径
ssl_certificate /cert/xxxxxxxxxxxxxxxx.pem;
ssl_certificate_key /cert/xxxxxxxxxxxxxxxx.key;
#以下的设置要根据你域名供应商提供的设置信息进行更换
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
# 然后根据你服务器的文件结构设置访问路径等内容
root /srv/awesome/www;
access_log /srv/awesome/log/access_log;
error_log /srv/awesome/log/error_log;
...............................
...............................
}
After setting up the new domain, we will do the old domain redirect. It include port 80 (http) redirect, and 443 (https) redirect:
设置好新域名后,接下来设置旧域名的重定向。也分两步:80端口重定向和 433 端口重定向。也可以将之合到一个 server里:
# 设置旧域名,重新定向到新域名
server {
listen 80;
listen 443 ssl;
server_name sla-dk.com www.sla-dk.com;
ssl_certificate /cert/xxxxxxxxxx.crt;
ssl_certificate_key /cert/xxxxxxxxxx.key;
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
ssl_prefer_server_ciphers on;
# 将80端口的http请求 301 重定向到新域名
if ( $scheme = "http" ) {
return 301 https://aodabo.tech$request_uri;
}
# 将443端口的https请求 rewrite 重定向到新域名
location / {
rewrite ^(.*)$ https://aodabo.tech permanent;
}
}
Finally, we can reload conf file, or restart the Nginx service according to your platform/system. The Ubuntu code is as followed: 完成以后重新加载配置文件, Ubuntu的Nginx配置文件重加载代码如下:
$ sudo /etc/init.d/nginx reload
Last step, we will check if these redirections work:
http://aodabo.tech
http://www.aodabo.tech
https://aodabo.tech
https://www.aodabo.tech
http://sla-dk.com
http://www.sla-dk.com
https://sla-dk.com
https://www.sla-dk.com
What is 301 Redirect? 什么是301重定向?
A 301 redirect is a permanent redirect from one URL to another. 301 redirects send site visitors and search engines to a different URL than the one they originally typed into their browser or selected from a search engine results page. These redirects also link various URLs under one umbrella so search engines rank all of the addresses based on the domain authority from inbound links.
页面永久性移走(301重定向)是一种非常重要的“自动转向”技术。网址重定向最为可行的一种办法。当用户或搜索引擎向网站服务器发出浏览请求时,服务器返回的HTTP数据流中头信息(header)中的状态码的一种,表示本网页永久性转移到另一个地址。
Why Set Up a 301 Redirect? 301重定向的意义是什么?
A 301 redirect is key to maintaining a website's domain authority and search rankings when the site's URL is changed for any reason. It easily sends visitors and search engines to a different URL than the one they originally requested -- without having to actually type in a different URL.
旧域名重新定向的意义在于:旧域名的外链资源不会浪费都被永久的强制301重定向到了新域名下,这对新域名快速获得旧域名的收录和权重都有很大的帮助,就是告诉搜索引擎这两个域名都是一样的内容只是域名进行了更换,这样无论是搜索引擎里是否提供了更换域名的服务,随着时间的推移,慢慢的搜索引擎就会将旧域名都指向新域名,这样更换域名造成的SEO方面的损失就会讲到最低。