經(jīng)過一系列的審核,耗時(shí)近一個(gè)月的網(wǎng)站備案終于通過,便迫不及待地進(jìn)行了域名解析。需要分別對(duì)PC和手機(jī)進(jìn)行配置,具體如下。
一、需求
| 客戶端 | 域名 | 描述 | 訪問目錄 |
|---|---|---|---|
| PC端 | www.harriszhang.cn | 用于PC端訪問 | /var/www/space/space/index.html |
| 手機(jī)端 | m.harriszhang.cn | 用于移動(dòng)端訪問 | /var/www/space/spaceMobile/index.html |
當(dāng)在PC端訪問www.harriszhang.cn或m.harriszhang.cn時(shí),跳轉(zhuǎn)到www.harriszhang.cn。
當(dāng)在移動(dòng)端訪問www.harriszhang.cn或m.harriszhang.cn時(shí),跳轉(zhuǎn)到m.harriszhang.cn
二、Nginx配置
2.1 PC訪問配置
修改前:
server {
listen 80;
server_name localhost;
location / {
root /var/www/space/space;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
...
}
修改后:
server {
listen 80;
server_name www.harriszhang.cn;
if ($http_host !~ "www.harriszhang.cn$") {
rewrite ^(.*) http://www.harriszhang.cn$1 permanent;
}
if ($http_user_agent ~* '(Android|webOS|iPhone|iPod|BlackBerry)') {
rewrite ^(.*) http://m.harriszhang.cn$1 permanent;
}
location / {
root /var/www/space/space;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
...
}
2.2 移動(dòng)端訪問配置
修改前:
server {
listen 80;
server_name localhost;
location / {
root /var/www/space/spaceMobile;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
...
}
修改后:
server {
listen 80;
server_name m.harriszhang.cn;
if ($http_user_agent !~* '(Android|webOS|iPhone|iPod|BlackBerry)') {
rewrite ^(.*) http://www.harriszhang.cn$1 permanent;
}
location / {
root /var/www/space/spaceMobile;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
...
}
三、重啟Nginx
通過nginx -s reload命令重新啟動(dòng) Nginx,即可看到設(shè)置已經(jīng)生效。