一、今天有一个临时性的项目、前端2个项目PC端、H5端,后端一个接口。
二、不想进行2个服务进行搭建、采用nginx判断是否是移动端或者手机端、总共2种方法
三、第一种:

user  root;
worker_processes  2;
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
#pid        logs/nginx.pid;
events {worker_connections  1024;
}
http {include       mime.types;default_type  application/octet-stream;#log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '#                  '$status $body_bytes_sent "$http_referer" '#                  '"$http_user_agent" "$http_x_forwarded_for"';#access_log  logs/access.log  main;sendfile        on;#tcp_nopush     on;#keepalive_timeout  0;keepalive_timeout  65;gzip  on;server {listen       80;server_name  localhost;#charset koi8-r;#access_log  logs/host.access.log  main;#add phonelocation / {root /home/web/pc;if ($http_user_agent ~ "(MIDP)|(WAP)|(UP.Browser)|(Smartphone)|(Obigo)|(Mobile)|(AU.Browser)|(wxd.Mms)|(WxdB.Browser)|(CLDC)|(UP.Link)|(KM.Browser)|(UCWEB)|(SEMC-Browser)|(Mini)|(Symbian)|(Palm)|(Nokia)|(Panasonic)|(MOT-)|(SonyEricsson)|(NEC-)|(Alcatel)|(Ericsson)|(BENQ)|(BenQ)|(Amoisonic)|(Amoi-)|(Capitel)|(PHILIPS)|(SAMSUNG)|(Lenovo)|(Mitsu)|(Motorola)|(SHARP)|(WAPPER)|(LG-)|(LG/)|(EG900)|(CECT)|(Compal)|(kejian)|(Bird)|(BIRD)|(G900/V1.0)|(Arima)|(CTL)|(TDG)|(Daxian)|(DAXIAN)|(DBTEL)|(Eastcom)|(EASTCOM)|(PANTECH)|(Dopod)|(Haier)|(HAIER)|(KONKA)|(KEJIAN)|(LENOVO)|(Soutec)|(SOUTEC)|(SAGEM)|(SEC-)|(SED-)|(EMOL-)|(INNO55)|(ZTE)|(iPhone)|(Android)|(Windows CE)|(Wget)|(Java)|(curl)|(Opera)") {root  /home/web/h5;}try_files $uri /index.html;}location /api {proxy_pass http://127.0.0.1:8001/;}#error_page  404              /404.html;# redirect server error pages to the static page /50x.html#error_page   500 502 503 504  /50x.html;location = /50x.html {root   html;}# proxy the PHP scripts to Apache listening on 127.0.0.1:80##location ~ \.php$ {#    proxy_pass   http://127.0.0.1;#}# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000##location ~ \.php$ {#    root           html;#    fastcgi_pass   127.0.0.1:9000;#    fastcgi_index  index.php;#    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;#    include        fastcgi_params;#}# deny access to .htaccess files, if Apache's document root# concurs with nginx's one##location ~ /\.ht {#    deny  all;#}}# another virtual host using mix of IP-, name-, and port-based configuration##server {#    listen       8000;#    listen       somename:8080;#    server_name  somename  alias  another.alias;#    location / {#        root   html;#        index  index.html index.htm;#    }#}# HTTPS server##server {#    listen       443 ssl;#    server_name  localhost;#    ssl_certificate      cert.pem;#    ssl_certificate_key  cert.key;#    ssl_session_cache    shared:SSL:1m;#    ssl_session_timeout  5m;#    ssl_ciphers  HIGH:!aNULL:!MD5;#    ssl_prefer_server_ciphers  on;#    location / {#        root   html;#        index  index.html index.htm;#    }#}
}

四、第二种:

user  root;
worker_processes  2;
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
#pid        logs/nginx.pid;
events {worker_connections  1024;
}
http {include       mime.types;default_type  application/octet-stream;#log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '#                  '$status $body_bytes_sent "$http_referer" '#                  '"$http_user_agent" "$http_x_forwarded_for"';#access_log  logs/access.log  main;sendfile        on;#tcp_nopush     on;#keepalive_timeout  0;keepalive_timeout  65;gzip  on;server {listen       80;server_name  localhost;#charset koi8-r;#access_log  logs/host.access.log  main;#add phoneset $mobile_rewrite do_not_perform;if ($http_user_agent ~* "(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows ce|xda|xiino") {set $mobile_rewrite perform;}if ($http_user_agent ~* "^(1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1 u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp( i|ip)|hs\-c|ht(c(\-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac( |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc\-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas\-|your|zeto|zte\-)") {set $mobile_rewrite perform;}location / {root /home/web/pc;#进行判断if ($mobile_rewrite = perform) {root  /home/web/h5;}try_files $uri /index.html;}location /api {proxy_pass http://127.0.0.1:8001/;}#error_page  404              /404.html;# redirect server error pages to the static page /50x.html#error_page   500 502 503 504  /50x.html;location = /50x.html {root   html;}# proxy the PHP scripts to Apache listening on 127.0.0.1:80##location ~ \.php$ {#    proxy_pass   http://127.0.0.1;#}# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000##location ~ \.php$ {#    root           html;#    fastcgi_pass   127.0.0.1:9000;#    fastcgi_index  index.php;#    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;#    include        fastcgi_params;#}# deny access to .htaccess files, if Apache's document root# concurs with nginx's one##location ~ /\.ht {#    deny  all;#}}# another virtual host using mix of IP-, name-, and port-based configuration##server {#    listen       8000;#    listen       somename:8080;#    server_name  somename  alias  another.alias;#    location / {#        root   html;#        index  index.html index.htm;#    }#}# HTTPS server##server {#    listen       443 ssl;#    server_name  localhost;#    ssl_certificate      cert.pem;#    ssl_certificate_key  cert.key;#    ssl_session_cache    shared:SSL:1m;#    ssl_session_timeout  5m;#    ssl_ciphers  HIGH:!aNULL:!MD5;#    ssl_prefer_server_ciphers  on;#    location / {#        root   html;#        index  index.html index.htm;#    }#}
}

五、原理:判断客户端的设备类型
六、要想让网站适配PC和手机设备,首先要能做出准确的判断。HTTP请求的Header中的User-Agent可以区分客户端的浏览器类型,可以通过User-Agent来判断客户端的设备。
七、无论是PC还是手机,由于操作系统、浏览器的多样性,自己来实现这个判断并不容易。好在国外有一套开源的通过User-Agent区分PC和手机的解决方案,可以直接使用:
http://detectmobilebrowsers.com/
《参考:https://blog.csdn.net/xiao__gui/article/details/46680863》

nginx配置移动端与PC端访问规则相关推荐

  1. nginx配置区分移动端和PC端,访问不同域名

    在群里遇到一个朋友,想实现移动端和PC端访问相同域名,但是页面效果有区别(这里先排除自适应,因为他已经写完了两个项目,移动端和PC端分开写的),所以我就提供了如下方案:访问同一域名,在nginx里判断 ...

  2. java 判断手机访问_java后台如何判断是移动端还是pc端的访问请求

    java后台如何判断是移动端还是pc端的访问请求 主要是根据  HttpServletRequest request  中的请求头所带的参数 user-agent来获取: String userAge ...

  3. js判断移动端,pc端,安卓,苹果浏览器的方法

    js 判断安卓或者ios 之indexOf方式(一) //判断访问终端 var browser={versions:function(){var u = navigator.userAgent, ap ...

  4. 360浏览器打不开网页_苹果移动端、PC端safari浏览器打不开网页的解决方案!

    你还在为苹果自带的safari浏览器打不开网页而苦恼吗,接下来的答案会帮助你解决苦恼. 苹果自带safari浏览器的设备分为两种:移动端.PC端.因此不同设备出现safari浏览器打不开网页的情况需要 ...

  5. 移动端和PC端的pdf预览与下载

    34.移动端和PC端的pdf的预览与下载 1.预览 需求:在手机端实现pdf的文件与下载,主要是zlb_app中 实现过程:在研究了vue-pdf.pdfjs.pdfh5之后,选择了vue-pdf-s ...

  6. vuecli3 实现 移动端和pc端 界面切换(两套代码)

    适合场景 多页面多系统应用 所有系统都在同一目录下.配置多入口多出口.每个系统之间可以链接.每个系统内依然采用Vue单页应用开发. 产品需求:一套代码 兼容pc端和移动端 , 移动端和pc端的样式布局 ...

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

    //判断客户端是移动端还是pc端 //userAgent用户代理 function IsPC() { var userAgentInfo = navigator.userAgent; var Agen ...

  8. Vue 移动端、PC 端适配

    Vue 移动端.PC 端适配可以使用 lib-flexible.amfe-flexible.postcss-pxtorem.postcss-px2rem 和 postcss-px-to-viewpor ...

  9. 记录一下曾经写过的P2P金融项目-理财端(PC端)纯代码(续-服务消费者工程)

    pom文件: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3 ...

  10. vue移动端和pc端切换显示两套不同的代码

    首先在App.vue中 <template ><div id="app" ><!-- <router-view />--><r ...

最新文章

  1. ecplise 使用 git
  2. jmeter获取时间_time 函数
  3. tvpvar模型的建模步骤_风控建模六部曲
  4. MyBatis + MyBatis Plus + MySQL——查询语句中字段名为MySQL关键字问题解决方案
  5. java项目经验怎么写_模具工简历项目经验怎么写
  6. 双线性插值算法c 语言实现,双线性插值 - CristianoC的个人空间 - OSCHINA - 中文开源技术交流社区...
  7. ajax传json格式数据
  8. 关注细节但不陷入细节
  9. 设置element-iu中table滚动条位置
  10. pytorch数据读取之Dataset与DataLoader
  11. 【干货】史上最全的Tensorflow学习资源汇总(转)
  12. 苹果xsmax怎么开机_苹果XS/xs max按钮浮标怎么设置?
  13. linux的vi命令详解,linux vi命令详解
  14. 网络安全设备-认识运维安全管理与审计系统(堡垒机)
  15. CAD:excel 批量画矩形
  16. 前端JS时间验证,结束时间不早于开始时间
  17. 国产时钟芯片应用探讨,CLB2305对标CY2305, CLB30110兼容IDT:8L30110,TI:CDCLVC1310
  18. Ubuntu SecureCRT安装/破解
  19. android开发面试题!360°深入了解Flutter,面试总结
  20. kali 安装星火应用商店(半成品

热门文章

  1. 高职计算机奖金核算表怎么做,学校在岗人员工资核算系统设计及实现.docx
  2. “做中学学中做”教学模式在计算机基础教学中的应用探索
  3. CentOS系统下Tomcat无法正常启动解决(8005端口不能启动)
  4. 数据存储之Archiver、Unarchiver、偏好设置
  5. nacos 配置Sentinel 熔断规则
  6. linux mint在笔记本上设置wifi热点
  7. 西电杨宗凯调研计算机学院,校长杨宗凯到计算机学院调研指导学院建设工作
  8. 百万数据查询优化技巧三十则,新增5条
  9. 面试题:写两个线程,一个线程打印1~26,另一个线程打印字母A-Z,交替打印数字和字母
  10. 2022-2028全球细胞系鉴定和表征测试行业调研及趋势分析报告