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的版本. 下面是對于舊版本的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#
}
所以需要兩個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...
}
因為return可以用于所有的版本,而rewrite可能因為版本的不同,導(dǎo)致301出錯。而且可以直接停止執(zhí)行匹配和搜索。
下面包含了http和https的。同一個服務(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 變量只會包含http 如果你的服務(wù)器只監(jiān)聽80端口(默認(rèn)是80端口)同時監(jiān)聽的選項中不含ssl關(guān)鍵字 . 不適用這個變量,就不能獲得你所想的要的跳轉(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 + lua 實現(xiàn)WAF的詳細(xì)過程
這篇文章主要介紹了Nginx + lua 實現(xiàn)WAF的詳細(xì)過程,本文給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2024-07-07
解決Nginx啟動報錯Job for nginx.service failed
Nginx啟動報錯,需檢查配置文件是否正確(執(zhí)行nginx -t),若錯誤則修正并重載,若80端口被占用,查進(jìn)程并終止,重啟服務(wù)以解決2025-08-08
Nginx上傳文件出現(xiàn)“ 413 (499 502 404) Requ
HTTP 413 Request Entity Too Large錯誤常常出現(xiàn)在客戶端發(fā)送的請求體超過服務(wù)器允許的大小限制時,本文主要介紹了Nginx上傳文件出現(xiàn)“ 413 (499 502 404) Request Entity Too Large錯誤解決,感興趣的可以了解一下2024-07-07
詳解Nginx中的geo模塊與利用其配置負(fù)載均衡的示例
這篇文章主要介紹了詳解Nginx中的geo模塊與利用其配置負(fù)載均衡的示例,文中對模塊的geo指令使用有比較詳細(xì)的介紹,需要的朋友可以參考下2016-01-01
高并發(fā)nginx服務(wù)器的linux內(nèi)核優(yōu)化配置講解
今天小編就為大家分享一篇關(guān)于高并發(fā)nginx服務(wù)器的linux內(nèi)核優(yōu)化配置講解,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-03-03
關(guān)于Nginx配置ssl證書實現(xiàn)https安全訪問
這篇文章主要介紹了關(guān)于Nginx配置ssl證書實現(xiàn)https安全訪問,前題條件是擁有服務(wù)器與可以解析到該服務(wù)器的自己的域名,需要的朋友可以參考下2023-04-04
Nginx中實現(xiàn)訪問HTTP請求時自動跳轉(zhuǎn)到HTTPS請求
本文主要介紹了Nginx中實現(xiàn)訪問HTTP請求時自動跳轉(zhuǎn)到HTTPS請求,下面介紹了兩種方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-08-08

