从nginx的官方文档 documentation, 正确的nginx https 301跳转到带www域名方法的方法如下:
HTTP 301跳转到带www域名方法
复制代码 代码如下: server {
listen 80;
server_name example.org;
return 301 http://www.example.org$request_uri;
}
server {
listen 80;
server_name www.example.org;
...
}
HTTPS 301跳转到带www域名方法
复制代码 代码如下: server {
listen 80;
server_name www.domain.com;
// $scheme will get the http protocol
// and 301 is best practice for tablet, phone, desktop and seo
return 301 $scheme://domain.com$request_uri;
}
server {
listen 80;
server_name domain.com;
// here goes the rest of your config file
// example
location / {
rewrite ^/cp/login"codetitle">复制代码 代码如下:server {
server_name www.domain.com;
rewrite ^(.*) http://domain.com$1 permanent;
}
server {
server_name domain.com;
#The rest of your configuration goes here#
}
所以需要两个server段。
从ksharpdabu.info 跳转到 www.ksharpdabu.info
复制代码 代码如下:server {
server_name domain.com;
rewrite ^(.*) http://www.domain.com$1 permanent;
}
server {
server_name www.domain.com;
#The rest of your configuration goes here#
}
按上面设置后,用rewrite的方法跳转到指定的域名下,利于SEO
下面是我举例,从www.google.com 跳转到 google.com的部分nginx配置内容:
复制代码 代码如下:server {
server_name www.google.com;
rewrite ^(.*) http://google.com$1 permanent;
}
server {
listen 80;
server_name google.com;
index index.php index.html;
####
# now pull the site from one directory #
root /var/www/www.google.com/web;
# done #
location = /favicon.ico {
log_not_found off;
access_log off;
}
}
网上还有一种不用rewirte的 方法,如下:
复制代码 代码如下:server {
#listen 80 is default
server_name www.example.com;
return 301 $scheme://example.com$request_uri;
}
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 www.example.com;
return 301 $scheme://example.com$request_uri;
}
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, ...):
复制代码 代码如下:#
# 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...
}
#
# 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...
}
Nginx,跳转
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
更新日志
- 试音宝典《试机四十号K2HD》[低速原抓WAV+CUE]
- 【原神】V5.2攻略 | 暴击爆伤配比 怎样伤害最大化?
- 【原神】V5.2攻略 | 全火元素5星角色·一图流培养
- 【原神】盘点《原神》容易被玩家忽略的大佬角色,很多玩家都不愿意抽他们
- 李玉刚.2017-刚好遇见你【玉泽东方】【WAV+CUE】
- 温拿乐队.1992-永远的记忆【宝丽金】【WAV+CUE】
- 群星.1996-风月电影原声带【滚石】【WAV+CUE】
- Blast Slam S1参赛名单出炉:XG被直邀
- 《英雄联盟》Doinb想让Tian当教练:世纪大和解?
- 《忆蚀》Subliminal:揭秘后室之谜,路知行献声Weplay文化展
- 那英《征服NEWXRCD台湾版》日本压制[WAV+CUE]
- 群星《金曲百分百上》3CD(香港版)[WAV+CUE]
- 刘欢《雨中的树(新歌加精选)2CD》德国HD24K金碟[WAV+CUE]
- 郑源 《世间情歌》6N纯银SQCD[WAV+CUE][1G]
- 群星《粤潮2HQII》头版限量编号[低速原抓WAV+CUE][991M]