时间:2021-05-08 09:06:28 | 栏目:Nginx | 点击:次
从nginx的官方文档 documentation, 正确的nginx https 301跳转到带www域名方法的方法如下:
HTTP 301跳转到带www域名方法
server {
listen 80;
server_name www.example.org;
...
}
HTTPS 301跳转到带www域名方法
要先用 nginx -v 命令检查你所说使用的nginx的版本. 下面是对于旧版本的nginx301跳转到带www域名方法从www.ksharpdabu.info 跳转到 ksharpdabu.info
server {
server_name domain.com;
#The rest of your configuration goes here#
}
所以需要两个server段。
从ksharpdabu.info 跳转到 www.ksharpdabu.info
server {
server_name www.domain.com;
#The rest of your configuration goes here#
}
按上面设置后,用rewrite的方法跳转到指定的域名下,利于SEO
下面是我举例,从www.google.com 跳转到 google.com的部分nginx配置内容:
网上还有一种不用rewirte的 方法,如下:
server {
#listen 80 is default
server_name example.com;
## here goes the rest of your conf...
}
因为return可以用于所有的版本,而rewrite可能因为版本的不同,导致301出错。而且可以直接停止执行匹配和搜索。
下面包含了http和https的。同一个服务器。
server {
listen 80;
listen 443 ssl;
server_name example.com;
# rest goes here...
}
$scheme 变量只会包含http 如果你的服务器只监听80端口(默认是80端口)同时监听的选项中不含ssl关键字 . 不适用这个变量,就不能获得你所想的要的跳转结果。
将所有http强制跳到https, SSL (personal config on UNIX with IPv4, IPv6, SPDY, ...):
return 301 https://example.com$request_uri;
}
#
# Redirect all non-encrypted to encrypted
#
server {
server_name example.com;
listen *:80;
listen [::]:80;
return 301 https://example.com$request_uri;
}
#
# There we go!
#
server {
server_name example.com;
ssl_certificate ssl/example.com/crt;
ssl_certificate_key ssl/example.com/key;
listen *:443 ssl spdy;
listen [::]:443 ssl spdy;
# rest goes here...
}
#
# Redirect all www to non-www
#
server {
server_name www.example.com;
ssl_certificate ssl/example.com/crt;
ssl_certificate_key ssl/example.com/key;
listen *:80;
listen *:443 ssl spdy;
listen [::]:80 ipv6only=on;
listen [::]:443 ssl spdy ipv6only=on;
return 301 https://example.com$request_uri;
}
#
# Redirect all non-encrypted to encrypted
#
server {
server_name example.com;
listen *:80;
listen [::]:80;
return 301 https://example.com$request_uri;
}
#
# There we go!
#
server {
server_name example.com;
ssl_certificate ssl/example.com/crt;
ssl_certificate_key ssl/example.com/key;
listen *:443 ssl spdy;
listen [::]:443 ssl spdy;
# rest goes here...
}