場景
項(xiàng) | 域名 | 描述 |
---|---|---|
pc端 | www.one.com |
用于pc端訪問官網(wǎng) |
移動(dòng)端 | m.one.com |
用于移動(dòng)端訪問 |
現(xiàn)在的需求是這樣掖举,在pc端訪問www.one.com
和m.one.com
都跳轉(zhuǎn)到www.one.com
而在移動(dòng)端訪問www.one.com
和m.one.com
都跳轉(zhuǎn)到m.one.com
參考懂傀,github上的這篇文章很詳細(xì),但是比較復(fù)雜,很多場景我們用不到视译,所以參考這個(gè)怠硼,我修改如下。
pc端:www.one.com
server {
listen 80;
server_name www.one.com;
#charset koi8-r;
#access_log logs/host.access.log main;
# 下面根據(jù)user_agent可以獲取
if ($http_host !~ "^www.one.cn$") {
rewrite ^(.*) http://www.one.cn$1 permanent;
}
if ($http_user_agent ~* (mobile|nokia|iphone|ipad|android|samsung|htc|blackberry)) {
rewrite ^(.*) http://m.one.com$1 permanent;
}
location / {
root /home/build/rampage-home-front/dist/html;
index index.html index.htm;
}
}
作用部分代碼如下:
if ($http_host !~ "^www.one.cn$") {
rewrite ^(.*) http://www.one.cn$1 permanent;
}
if ($http_user_agent ~* (mobile|nokia|iphone|ipad|android|samsung|htc|blackberry)) {
rewrite ^(.*) http://m.one.com$1 permanent;
}
移動(dòng)端:m.one.com
server {
listen 80;
server_name m.one.cn;
#charset koi8-r;
#access_log logs/host.access.log main;
#非移動(dòng)端跳轉(zhuǎn)到 www.one.com
if ($http_user_agent !~* (mobile|nokia|iphone|ipad|android|samsung|htc|blackberry)) {
rewrite ^(.*) http://www.one.com$1 permanent;
}
location / {
root /home/build/rampage-mobile-front/dist;
index index.html index.htm;
}
}
作用部分代碼如下:
if ($http_user_agent !~* (mobile|nokia|iphone|ipad|android|samsung|htc|blackberry)) {
rewrite ^(.*) http://www.one.com$1 permanent;
}
至此完成了相關(guān)配置