nginx mobile terminal automatically jump and the pc

Scenes

item domain name description
pc end www.one.com Visit the official website for the pc
Mobile end m.one.com For a mobile terminal to access

Now demand is so, visit the pc www.one.comand m.one.comhave to jump towww.one.com
and access the mobile terminal www.one.comand m.one.comboth jump tom.one.com

Reference , this article on github very detailed, but more complex, many scenes we have no use, so this reference, I modified as follows.

pc end: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;
     }

}

Acting as part of the code:

 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;
 }

Mobile end: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; } } 

Acting as part of the code:

 if ($http_user_agent !~* (mobile|nokia|iphone|ipad|android|samsung|htc|blackberry)) { rewrite ^(.*) http://www.one.com$1 permanent; } 

This completes the configuration

 

Examples of configuration:

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;
 }
}

  

Movable end profile 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;
 }
}

  

Guess you like

Origin www.cnblogs.com/weifeng1463/p/11672706.html