nginx 移動端和pc端自動跳轉

場景

域名 描述
pc端 www.one.com 用於pc端訪問官網
移動端 m.one.com 用於移動端訪問

如今的需求是這樣,在pc端訪問www.one.comm.one.com都跳轉到www.one.com
而在移動端訪問www.one.comm.one.com都跳轉到m.one.comhtml

參考,github上的這篇文章很詳細,可是比較複雜,不少場景咱們用不到,因此參考這個,我修改以下。android

pc端:www.one.comnginx

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

做用部分代碼以下:git

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.comgithub

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

做用部分代碼以下:ruby

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

至此完成了相關配置bash

 

實例配置:session

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配置文件iphone

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;
 }
}
相關文章
相關標籤/搜索