场景

域名 描述
pc端 www.one.com 用于pc端访问官网
移动端 m.one.com 用于移动端访问

现在的需求是这样,在pc端访问www.one.comm.one.com都跳转到www.one.com
而在移动端访问www.one.comm.one.com都跳转到m.one.com

参考,github上的这篇文章很详细,但是比较复杂,很多场景我们用不到,所以参考这个,我修改如下。

pc端:www.one.com

  server {
listen 80;
server_name www.one.com; #charset koi8-r;
#access_log logs/host.access.log main;
# 下面根据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;
}

移动端:m.one.com

  server {
listen 80;
server_name m.one.cn; #charset koi8-r;
#access_log logs/host.access.log main;
#非移动端跳转到 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;
}

至此完成了相关配置

实例配置:

PC端网站配置文件

 server {
listen 80 default_server;
listen [::]:80 default_server;
server_name weifeng.com;
root /usr/share/nginx/html;
rewrite ^(.*)$ https://${server_name}$1 permanent; include /etc/nginx/default.d/*.conf; if ($http_user_agent ~* (mobile|nokia|iphone|ipad|android|samsung|htc|blackberry)) {
rewrite ^(.*) https://m.weifeng.com$1 permanent;
} location / {
} error_page 404 /404.html;
location = /40x.html {
} error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
} server {
listen 443;
server_name weifeng.com;
ssl on;
root /usr/share/nginx/html;
index index.html index.htm;
ssl_certificate /cert/weifeng.com.pem;
ssl_certificate_key /cert/weifeng.com.key;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on; if ($http_user_agent ~* (mobile|nokia|iphone|ipad|android|samsung|htc|blackberry)) {
rewrite ^(.*) https://m.weifeng.com$1 permanent;
} location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
}

  

移动端nginx配置文件

    server {
listen 80;
server_name m.weifeng.com;
root /usr/share/nginx/html-mobile;
rewrite ^(.*)$ https://${server_name}$1 permanent; location / {
} error_page 404 /404.html;
location = /40x.html {
} error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
} server { listen 443;
server_name m.weifeng.com;
ssl on;
root /usr/share/nginx/html-mobile;
index index.html index.htm;
ssl_certificate /cert/weifeng.com.pem;
ssl_certificate_key /cert/weifeng.com.key;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on; location / {
root /usr/share/nginx/html-mobile;
index index.html index.htm;
}
}

  

nginx 移动端和pc端自动跳转的更多相关文章

  1. asp.net Core 使用过滤器判断请求客户端是否为移动端,并实现PC端和移动端请求映射和自动跳转

    很多时候我们做网站时单纯的用bootstrap等前端框架实现的前端自适应带给用户的体验并不太好,所以为了提高用户体验会专门针对PC端网页重新设计一套移动端网页,但是怎么才能做到在移动端访问PC页面的时 ...

  2. 【Javascript Demo】移动端访问PC端网页时跳转到对应的移动端网页

    不想通过CSS自适应在PC端和移动端分别显示不同的样式,那么只能通过在移动端访问PC端网页时跳转到对应的移动端网页了,那么怎么跳转呢,网上也有很多文章说明,下面是本人测试有效的方式. 1.效果图 PC ...

  3. iis 如何搭建url 重定向,实现无线端和pc端不同的跳转

    第一步,下载安装ARR(Application Request Routing), http://www.iis.net/downloads/microsoft/application-request ...

  4. JavaScript判断移动端及pc端访问不同的网站

    JavaScript判断移动端及pc端访问不同的网站 现在很多网站都是分为两个版本,一个pc端的一个移动端的(响应式除外),针对这两个版本,就需要对访问的设备进行判断,如果是pc,就直接访问pc网站, ...

  5. js判断是移动端还是pc端

    运行页面的时候,执行到js会判断来自于移动端还是pc端,如果是移动端则跳转制定链接地址,这样在手机端会有额外的不必要浪费的加载时间 var browser={ versions:function(){ ...

  6. js常见的判断移动端或者pc端或者安卓和苹果浏览器的方法总结

    1.js常见的判断移动端或者pc端或者安卓和苹果浏览器的方法总结 : http://www.haorooms.com/post/js_pc_iosandmobile 2.Js判断客户端是否为PC还是手 ...

  7. js判断移动端还是PC端

    function isMobile(){ var sUserAgent= navigator.userAgent.toLowerCase(), bIsIpad= sUserAgent.match(/i ...

  8. js判断游览器是移动端还是PC端

    js判断网页游览器是移动端还是PC端 <script type="text/javascript"> function browserRedirect() { var ...

  9. 检测当前运行环境——移动端与PC端。

    方法1: $(function checkBrowser(){ var browser={ versions:function(){ var u = navigator.userAgent, app ...

  10. 关于移动端和PC端的交互的区别

    对于现在的移动端设备的普及,移动端上的用户体验成了一个重要的关注点. 看了一些网上的关于移动端的交互和用户体验的知识,这里总结了一些.若有不足的地方,希望大家能够积极补充. PC端和移动端的产品的设计 ...

随机推荐

  1. 项目中有私有仓库模块时,使用 npm ci 命令的安装步骤

    项目中有私有仓库模块时,使用 npm ci 命令的安装步骤: 先安装私有仓库模块:npm install <npm包名> --registry=<npm包源> 再运行命令:np ...

  2. abp记录1

    1在AbpWebApplication中的的构造函数中创建abpBootstrapper 实例,在Application_Start执行AbpBootstrapper值初始化方式 2AbpBootst ...

  3. Gym - 247731E :room(最小费用流裸题)

    题意:有N个宿舍(N<200),给出第一年每个宿舍有哪4个同学.现在给出N个4元组y[][4],表示这4个人想住一起,问最少多少人需要换宿舍. 思路:费用流,每个4元组y[]到每个宿舍连边,流量 ...

  4. WHAT IS THE DIFFERENCE BETWEEN REACT.JS AND REACT NATIVE?

    Amit Ashwini - 09 SEPTEMBER 2017 React.js was developed by Facebook to address its need for a dynami ...

  5. react native 开发问题分类

    链接.编译.部署.运行: 一.语法问题:编译问题 二.链接问题: yarn add react-navigation react-native link react-navigation 三.部署问题 ...

  6. win10怎么查看当前用户账号

    https://jingyan.baidu.com/article/9225544679ab37851648f489.html

  7. 最小球覆盖——模拟退火&&三分套三分套三分

    题目 给出 $N(1 \leq N \leq 100)$ 个点的坐标 $x_i,y_i,z_i$($-100000 \leq x_i,y_i,z_i \leq 100000$),求包围全部点的最小的球 ...

  8. three arrays HDU - 6625 (字典树)

    three arrays \[ Time Limit: 2500 ms \quad Memory Limit: 262144 kB \] 题意 给出 \(a\),\(b\) 数组,定义数组 \(c[i ...

  9. [RN] React Native :Error: Cannot find module 'asap/raw'

    今天在使用 react-native-dropdownmenus 的时候,安装没问题,但Link的时候 报: Error: Cannot find module 'asap/raw' 朋友们莫慌,一步 ...

  10. linux命令之------Mv命令

    Mv命令 1)作用:用来为文件或目录改名/或将文件或目录一如其他位置 2)-i:若指定目录已有同名文件,则先询问是否覆盖旧文件: 3)-f:在mv操作要覆盖某已有的目标文件时,不给任何指示: 4)案例 ...