Nginx服務(wù)器中HTTP 301跳轉(zhuǎn)到帶www的域名的方法
從nginx的官方文檔 documentation, 正確的nginx https 301跳轉(zhuǎn)到帶www域名方法的方法如下:
HTTP 301跳轉(zhuǎn)到帶www域名方法
listen 80;
server_name example.org;
return 301 http://www.example.org$request_uri;
}
server {
listen 80;
server_name www.example.org;
...
}
HTTPS 301跳轉(zhuǎn)到帶www域名方法
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?$ /cp/login.php last;
// etc etc...
}
}
要先用 nginx -v 命令檢查你所說使用的nginx的版本. 下面是對(duì)于舊版本的nginx301跳轉(zhuǎn)到帶www域名方法從www.ksharpdabu.info 跳轉(zhuǎn)到 ksharpdabu.info
server_name www.domain.com;
rewrite ^(.*) http://domain.com$1 permanent;
}
server {
server_name domain.com;
#The rest of your configuration goes here#
}
所以需要兩個(gè)server段。
從ksharpdabu.info 跳轉(zhuǎn)到 www.ksharpdabu.info
server_name domain.com;
rewrite ^(.*) http://www.domain.com$1 permanent;
}
server {
server_name www.domain.com;
#The rest of your configuration goes here#
}
按上面設(shè)置后,用rewrite的方法跳轉(zhuǎn)到指定的域名下,利于SEO
下面是我舉例,從www.google.com 跳轉(zhuǎn)到 google.com的部分nginx配置內(nèi)容:
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;
}
}
網(wǎng)上還有一種不用rewirte的 方法,如下:
#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...
}
因?yàn)閞eturn可以用于所有的版本,而rewrite可能因?yàn)榘姹镜牟煌?,?dǎo)致301出錯(cuò)。而且可以直接停止執(zhí)行匹配和搜索。
下面包含了http和https的。同一個(gè)服務(wù)器。
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 變量只會(huì)包含http 如果你的服務(wù)器只監(jiān)聽80端口(默認(rèn)是80端口)同時(shí)監(jiān)聽的選項(xiàng)中不含ssl關(guān)鍵字 . 不適用這個(gè)變量,就不能獲得你所想的要的跳轉(zhuǎn)結(jié)果。
將所有http強(qiáng)制跳到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...
}
相關(guān)文章
nginx內(nèi)部訪問特性如何實(shí)現(xiàn)靜態(tài)資源授權(quán)訪問
這篇文章主要介紹了nginx內(nèi)部訪問特性如何實(shí)現(xiàn)靜態(tài)資源授權(quán)訪問方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06Nginx配置多個(gè)端口進(jìn)行監(jiān)聽的實(shí)現(xiàn)
隨著容器的應(yīng)用越來(lái)越多,將nginx部署在容器中也是常有之事,本文主要介紹了Nginx配置多個(gè)端口進(jìn)行監(jiān)聽的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-07-07Ubuntu?22.04.1?LTS?編譯安裝?nginx-1.22.1的配置過程
Ubuntu安裝Nginx有兩種方式,一種是通過命令的方式,這種方式安裝的Nginx版本低,之前漏掃掃出來(lái)Nginx版本低,需要升級(jí)所以現(xiàn)在用編譯的方式安裝版本高點(diǎn)的,本文介紹Ubuntu22.04.1?LTS編譯安裝nginx1.22.1的配置過程,本文給大家介紹的非常詳細(xì),需要的朋友參考下吧2024-01-01配置nginx保證frps服務(wù)器與web共用80端口的方法
這篇文章主要介紹了frps服務(wù)端與nginx可共用80端口的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2018-06-06Linux Nginx VPS下簡(jiǎn)單解決CC攻擊
Linux Nginx VPS下簡(jiǎn)單解決CC攻擊,使用Nginx與php的朋友可以參考下。2010-12-12