Nginx配置移动端和PC端自动跳转

本文介绍如何使用Nginx配置文件实现PC端与移动端网站的自动跳转,通过判断User-Agent信息,确保用户从正确的域名访问对应设备的网站版本。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Nginx配置PC端和移动端自动跳转

一、域名准备阶段

客户端域名描述
pc端www.yxf.com用于pc端访问的域名
移动端m.yxf.com用于移动端访问的域名

问题描述:pc端不管是访问www.yxf.com域名还是m.yxf.com域名都需要跳转到www.yxf.com域名下。

移动端不管是访问 m.yxf.com还是www.yxf.com下都需要要跳转到 m.yxf.com域名下

二、下面我们就来配置nginx

  1. pc端nginx的conf配置
server {
        listen       443;
        server_name  www.yxf.com;

        ssl on;
        ssl_certificate      cert/common.pem;
        ssl_certificate_key  cert/common.key;

        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;

        ssl_ciphers  ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
        ssl_prefer_server_ciphers  on;
	if ($http_user_agent ~* '(Android|webOS|iPhone|iPod|BlackBerry)') {
             rewrite ^(.*) http://m.yxf.com$1 permanent;
        }


        location / {
            root   /home/yxf/pc;
            index  index.html;
        }
    }

  1. 移动端的nginx的conf配置
server {
        listen       443;
        server_name m.yxf.com;
	
	if ($http_user_agent !~* '(Android|webOS|iPhone|iPod|BlackBerry)') {
            rewrite ^(.*) https://www.yxf.com$1 redirect;
       }

        ssl on;
        ssl_certificate      cert/common.pem;
        ssl_certificate_key  cert/common.key;

        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;

        ssl_ciphers  ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
        ssl_prefer_server_ciphers  on;

        location / {
            root   /home/yxf/mobile;
            index  index.html;
        }
    }

上述需要注意的是,如果想让pc 跳转到移动 或者移动跳转到 pc 是302 临时重定向,可以修改 permanent 为 redirect

  1. redirect – 返回临时重定向的HTTP状态302
  2. permanent – 返回永久重定向的HTTP状态301
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值