Nginx:pc端和移动端网站适配

目的:电脑浏览器访问展示pc端的页面,手机端浏览器访问展示手机端的页面,两套页面,两个域名。

server {
    listen       80;
    server_name  domain.com www.domain.com;
    client_max_body_size 10m;

    set $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|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|phone)|xda|xiino") {
        set $mobile_rewrite perform;
    }

    if ($mobile_rewrite = perform) {
        rewrite ^ http://m.domain.com$request_uri? redirect;
        break;
    }

    location / {
        root   /usr/share/nginx/project_pc;
        try_files $uri $uri/ /index.html
        index  index.html;
    }
}

server {
    listen       80;
    server_name  m.domain.com;
    client_max_body_size 10m;
    root /usr/share/nginx/project_mobile;
    index index.html;

    location ^~ *.html {
        add_header Cache-Control "no-cache, no-store, must-revalidate";
    }

    location ^~ /static/ {
        add_header Cache-Control "public,max-age=31536000";
    }
}

 

你可能感兴趣的:(Nginx:pc端和移动端网站适配)