Nignx配置301重定向,http301跳转https详细教程
发布日期:2019年09月03日,网站分类:网站新闻
nignx建站以后很多朋友都不会正确的配置301跳转,尤其是www和@,http跳转https这一步,也就是所谓的80端口跳转443端口,因为做SEO优化的朋友都知道,如果俩个域名同时收录会分散权重,虽然不会导致排名下降,但是还是会有一些影响,那么做seo优化该怎么设置301才是最好的呢?
PS:当然融网建站的工程师也不建议大家停止解析其中的一个域名,比如主域名www,却停止了@的解析,这样你会损失一部分流量。
首先我们先看一下有哪几种域名跳转方法:
所有域名跳转@下
rewrite ^/(.*) https://www.kufan.cn/$1 permanent;
80端口跳转到443,http跳转https
if ($server_port = 80) {
rewrite ^(.*)$ https://$host$1 permanent;
}www跳转@
if ($host != 'www.rongkeji.cn' ) {
rewrite ^/(.*)$ https://www.kufan.cn/$1 permanent;
}所有域名301重定向@
return 301 https://rongekji.cn$request_uri;
一般常规跳转的话,这都是正确的,也都可以正常跳转,不影响收录,如果你百度站长平台https认证不通过,那么就要选择 所有域名301重定向@ ,因为这才是搜索引擎最标准的301跳转规则。

那么我们开始nignx配置301重定向教程,深度一点的,含http跳转https,完美通过百度平台https认证。
增加http301https,这里说的是@和www301跳转到https://rongkeji.cn
server {
listen 80;
server_name rongkeji.cn www.rongkeji.cn;
return 301 https://rongkeji.cn$request_uri;
}增加https://www 301跳转 https://rongkeji.cn
server {
listen 443 ssl http2;
server_name www.rongkeji.cn;
return 301 https://rongkeji.cn$request_uri;
}http301重定向https详细代码,第三段为https正常访问,无跳转。
server
{
listen 80;
server_name www.rongkeji.cn rongkeji.cn;
return 301 https://rongkeji.cn$request_uri;
}
server
{
listen 443 ssl http2;
server_name www.rongkeji.cn;
ssl_certificate /域名SSL证书.cer;
ssl_certificate_key /域名SSL证书.key;
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
ssl_prefer_server_ciphers on;
ssl_ciphers "TLS13-AES-256-GCM-SHA384:TLS13-CHACHA20-POLY1305-SHA256:TLS13-AES-128-GCM-SHA256:TLS13-AES-128-CCM-8-SHA256:TLS13-AES-128-CCM-SHA256:EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5";
ssl_session_cache builtin:1000 shared:SSL:10m;
# openssl dhparam -out /usr/local/nginx/conf/ssl/dhparam.pem 2048
ssl_dhparam /usr/local/nginx/conf/ssl/dhparam.pem;
return 301 https://rongkeji.cn$request_uri;
}
server
{
listen 443 ssl http2;
#listen [::]:443 ssl http2;
server_name rongkeji.cn;
index index.html index.htm index.php default.html default.htm default.php;
root /rongkeji;
ssl_certificate /域名SSL证书.cer;
ssl_certificate_key /域名SSL证书.key;
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
ssl_prefer_server_ciphers on;
ssl_ciphers "TLS13-AES-256-GCM-SHA384:TLS13-CHACHA20-POLY1305-SHA256:TLS13-AES-128-GCM-SHA256:TLS13-AES-128-CCM-8-SHA256:TLS13-AES-128-CCM-SHA256:EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5";
ssl_session_cache builtin:1000 shared:SSL:10m;
# openssl dhparam -out /usr/local/nginx/conf/ssl/dhparam.pem 2048
ssl_dhparam /usr/local/nginx/conf/ssl/dhparam.pem;
include rewrite/rongkeji.conf;
#error_page 404 /404.html;
# Deny access to PHP files in specific directory
#location ~ /(wp-content|uploads|wp-includes|images)/.*\.php$ { deny all; }
include enable-php.conf;
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 12h;
}
location ~ /.well-known {
allow all;
}
location ~ /\.
{
deny all;
}
access_log off;
}重点来了,如果你是二级单域名,如 cdn.rongkeji.cn,该如何设置呢?
这里我们只需要一个80端口识别跳转即可,如下:
if ($server_port = 80) {
rewrite ^(.*)$ https://$host$1 permanent;
}这里我们只需要把listen 80,listen 443 ssl,在server内存放到一起,然后增加80端口跳转443端口即可。
server
{
listen 80;
#listen [::]:80;
listen 443 ssl http2;
#listen [::]:443 ssl http2;
server_name cdn.rongkeji.cn;
index index.html index.htm index.php default.html default.htm default.php;
root /rcloud;
ssl_certificate /域名SSL证书.cer;
ssl_certificate_key /域名SSL证书.key;
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
ssl_prefer_server_ciphers on;
ssl_ciphers "TLS13-AES-256-GCM-SHA384:TLS13-CHACHA20-POLY1305-SHA256:TLS13-AES-128-GCM-SHA256:TLS13-AES-128-CCM-8-SHA256:TLS13-AES-128-CCM-SHA256:EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5";
ssl_session_cache builtin:1000 shared:SSL:10m;
# openssl dhparam -out /usr/local/nginx/conf/ssl/dhparam.pem 2048
ssl_dhparam /usr/local/nginx/conf/ssl/dhparam.pem;
if ($server_port = 80) {
rewrite ^(.*)$ https://$host$1 permanent;
}
include rewrite/rongkeji.conf;
#error_page 404 /404.html;
# Deny access to PHP files in specific directory
#location ~ /(wp-content|uploads|wp-includes|images)/.*\.php$ { deny all; }
include enable-php.conf;
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 12h;
}
location ~ /.well-known {
allow all;
}
location ~ /\.
{
deny all;
}
access_log off;
}还是那句话,nignx配置301重定向其实还是很简单,只是看自己怎么会判断它的逻辑,在修改前一定要记得备份,如果你设置错误,可能会出现重定向循环导致无法访问,如果对nignx的https配置不了解,那么你可以去看下《Nginx环境下正确配置SSL安全加密证书》,里面有详细的https教程解说。
转载请注明来自:https://www.kufan.cn/news/452.html
下一篇:企业网站建设SEO优化的经验心得
