nginx配置伪静态和适配客户端的方法步骤

nginx配置伪静态和适配客户端的方法步骤,第1张

nginx配置伪静态和适配客户端的方法步骤

后端用的thinkphp3.2.3框架,如果您是其他的语言或者,注意适当修改下

server {
    listen    80;
    server_name www.morgen.club;

    #charset koi8-r;
    
    #access_log logs/host.access.log main;

网站根目录

    location / {
      root  html;
      index index.html index.htm;
      #设置访问设备
      set $source_device 'index';
      #判断为手机客户端或ipad
      if ($http_user_agent ~* (mobile|nokia|iphone|ipad|android|samsung|htc|blackberry)) {
        set $source_device 'phone';
      }
      #根目录
      if ($request_uri ~* ^/$){
        rewrite ^(.*)$ /index.php?s=/Home/$source_device/index.html last;
        break;
      }
      #电脑首页
      if ($request_uri ~* ^/Home/index/index.html$) {
        rewrite ^/Home/index/index.html$ /web/ permanent;
        break;
      }
      #电脑首页分页
      if ($request_uri ~* ^/Home/index/index/page/(\d*).html$) {
        rewrite ^/Home/index/index/page/(\d*).html$ /web/pages// permanent;
        break;
      }
      #电脑首页分类首页
      if ($request_uri ~* ^/Home/index/index/cate/(\d*).html$) {
        rewrite ^/Home/index/index/cate/(\d*).html$ /web/cates// permanent;
        break;
      }
      #电脑首页分类分页
      if ($request_uri ~* ^/Home/index/index/cate/(\d*)/page/(\d*).html$) {
        rewrite ^/Home/index/index/cate/(\d*)/page/(\d*).html$ /web/cates//pages// permanent;
        break;
      }
      #电脑首页搜索
      if ($request_uri ~* ^/Home/index/index.html(.*)) {
        rewrite ^/Home/index/index.html(.*) /web/search?$query_string permanent;
        break;
      }
      #电脑书籍页面
      if ($request_uri ~* ^/Home/index/books/book/(\d*).html$) {
        rewrite ^/Home/index/books/book/(\d*).html$ /web/books// permanent;
        break;
      }
      #电脑章节页面
      if ($request_uri ~* ^/Home/index/contents/books/(\d*)/chapters/(\d*).html$) {
        rewrite ^/Home/index/contents/books/(\d*)/chapters/(\d*).html$ /web/books//chapters// permanent;
        break;
      }
      #手机首页
      if ($request_uri ~* ^/Home/phone/index.html$) {
        rewrite ^/Home/phone/index.html$ /phone/ last;
        break;
      }
      #手机首页分页
      if ($request_uri ~* ^/Home/phone/index/page/(\d*)(.*)$) {
        rewrite ^/Home/phone/index/page/(\d*).html /phone/pages// permanent;
        break;
      }
      #手机首页分类首页
      if ($request_uri ~* ^/Home/phone/index/cate/(\d*).html$) {
        rewrite ^/Home/phone/index/cate/(\d*).html$ /phone/cates// permanent;
        break;
      }
      #手机首页分类分页
      if ($request_uri ~* ^/Home/phone/index/cate/(\d*)/page/(\d*).html$) {
        rewrite ^/Home/phone/index/cate/(\d*)/page/(\d*).html$ /phone/cates//pages// permanent;
        break;
      }
      #手机首页搜索
      if ($request_uri ~* ^/Home/phone/index.html?(.*)) {
        rewrite ^/Home/phone/index.html(.*) /phone/index.html permanent;
        break;
      }
      #手机书籍页面
      if ($request_uri ~* ^/home/phone/books/book/(\d*).html$) {
        rewrite ^/home/phone/books/book/(\d*).html$ /phone/books// permanent;
        break;
      }
      #手机章节页面
      if ($request_uri ~* ^/home/phone/contents/books/(\d*)/chapters/(\d*).html$) {
        rewrite ^/home/phone/contents/books/(\d*)/chapters/(\d*).html$ /phone/books//chapters// permanent;
        break;
      }
      
      if ($request_uri ~* ^/Home/book/(.*)$) {
        rewrite ^(.*)$ /index.php?s= last;
        break;
      }

      if (!-e $request_filename) {
        return 404;
      }
    }

电脑浏览器端

    location /web {
      root html;
      index index.html index.htm;
      #如果设备为手机或ipad
      if ($http_user_agent ~* (mobile|nokia|iphone|ipad|android|samsung|htc|blackberry)) {
        rewrite ^/web(.*) /phone permanent;
      }
        #电脑首页搜索
        if ($request_uri ~ ^/web/search(.*)$) {
          rewrite ^(.*) /index.php?s=/Home/index/index.html last;
          break;
        }
        #电脑首页
        set $index false;
        if ($request_uri ~* ^/web$) {
          set $index true;
        }
        if ($request_uri ~* ^/web/$) {
          set $index true;
        }
        if ($index = true){
          rewrite ^/web(.*) /index.php?s=/Home/index/index.html last;
          break;
        }
        #电脑首页分页
        set $index_page false;
        if ($request_uri ~* ^/web/pages/(\d*)$) {
          set $index_page true;
        }
        if ($request_uri ~* ^/web/pages/(\d*)/$) {
          set $index_page true;
        }
        if ($index_page = true){
          rewrite ^/web/pages/(\d*)(.*) /index.php?s=/Home/index/index/page/.html last;
          break;
        }
        #电脑分类首页
        set $cate false;
        if ($request_uri ~* ^/web/cates/(\d*)$) {
          set $cate true;
        }
        if ($request_uri ~* ^/web/cates/(\d*)/$) {
          set $cate true;
        }
        if ($cate = true){
          rewrite ^/web/cates/(\d*)(.*) /index.php?s=/Home/index/index/cate/.html last;
          break;
        }
        #电脑分类分页
        set $cate_page false;
        if ($request_uri ~* ^/web/cates/(\d*)/pages/(\d*)$) {
          set $cate_page true;
        }
        if ($request_uri ~* ^/web/cates/(\d*)/pages/(\d*)/$) {
          set $cate_page true;
        }
        if ($cate_page = true){
          rewrite ^/web/cates/(\d*)/pages/(\d*)(.*) /index.php?s=/Home/index/index/cate//page/.html last;
          break;
        }
        
        #电脑书籍页面
        set $book false;
        if ($request_uri ~* ^/web/books/(\d*)$) {
          set $book true;
        }
        if ($request_uri ~* ^/web/books/(\d*)/$) {
          set $book true;
        }
        if ($book = true){
          rewrite ^/web/books/(\d*)(.*) /index.php?s=/Home/index/books/book/.html last;
          break;
        }
        #电脑章节页面
        set $chapter false;
        if ($request_uri ~* ^/web/books/(\d*)/chapters/(.*)$) {
          set $chapter true;
        }
        if ($request_uri ~* ^/web/books/(\d*)/chapters/(.*)/$) {
          set $chapter true;
        }
        if ($chapter = true){
          rewrite ^/web/books/(\d*)/chapters/(.*)/ /index.php?s=/Home/index/contents/books//chapters/.html last;
          break;
        }

        if (!-e $request_filename) {
          return 404;
        }
    }

手机和平板端

    location /phone {
      root html;
      index index.html index.htm;
      #如果设备不是手机或ipad
      if ($http_user_agent !~* (mobile|nokia|iphone|ipad|android|samsung|htc|blackberry)) {
        rewrite ^/phone(.*) /web permanent;
      }
        #电脑首页搜索
        if ($request_uri ~ ^/phone/search(.*)$) {
          rewrite ^(.*) /index.php?s=/Home/phone/index.html last;
          break;
        }
        #手机首页
        set $index false;
        if ($request_uri ~* ^/phone$) {
          set $index true;
        }
        if ($request_uri ~* ^/phone/$) {
          set $index true;
        }
        if ($index = true){
          rewrite ^/phone(.*) /index.php?s=/Home/phone/index.html last;
          break;
        }
        #手机首页分页
        set $index_page false;
        if ($request_uri ~* ^/phone/pages/(\d*)$) {
          set $index_page true;
        }
        if ($request_uri ~* ^/phone/pages/(\d*)/$) {
          set $index_page true;
        }
        if ($index_page = true){
          rewrite ^/phone/pages/(\d*)(.*) /index.php?s=/Home/phone/index/page/.html last;
          break;
        }
        #手机分类首页
        set $cate false;
        if ($request_uri ~* ^/phone/cates/(\d*)$) {
          set $cate true;
        }
        if ($request_uri ~* ^/phone/cates/(\d*)/$) {
          set $cate true;
        }
        if ($cate = true){
          rewrite ^/phone/cates/(\d*) /index.php?s=/Home/phone/index/cate/.html last;
          break;
        }
        #手机分类分页
        set $cate_page false;
        if ($request_uri ~* ^/phone/cates/(\d*)/pages/(\d*)$) {
          set $cate_page true;
        }
        if ($request_uri ~* ^/phone/cates/(\d*)/pages/(\d*)/$) {
          set $cate_page true;
        }
        if ($cate_page = true){
          rewrite ^/phone/cates/(\d*)/pages/(\d*)(.*) /index.php?s=/Home/phone/index/cate//page/.html last;
          break;
        }
        #手机书籍页面
        set $book false;
        if ($request_uri ~* ^/phone/books/(\d*)$) {
          set $book true;
        }
        if ($request_uri ~* ^/phone/books/(\d*)/$) {
          set $book true;
        }
        if ($book = true){
          rewrite ^/phone/books/(\d*)(.*) /index.php?s=/Home/phone/books/book/.html last;
          break;
        }
        #手机书籍分页页面
        set $book false;
        if ($request_uri ~* ^/phone/books/(\d*)/pages/(\d*)$) {
          set $book true;
        }
        if ($request_uri ~* ^/phone/books/(\d*)/pages/(\d*)/$) {
          set $book true;
        }
        if ($book = true){
          rewrite ^/phone/books/(\d*)/pages/(\d*) /index.php?s=/Home/phone/books/book//page/.html last;
          break;
        }
        #手机章节页面
        set $chapter false;
        if ($request_uri ~* ^/phone/books/(\d*)/chapters/(.*)$) {
          set $chapter true;
        }
        if ($request_uri ~* ^/phone/books/(\d*)/chapters/(.*)/$) {
          set $chapter true;
        }
        if ($chapter = true){
          rewrite ^/phone/books/(\d*)/chapters/(.*)/ /index.php?s=/Home/phone/contents/books//chapters/.html last;
          break;
        }

        if (!-e $request_filename) {
          return 404;
        }
    }
    location ~ .xml(.*) {
      root html/ttt/;
    }
 
    location ~ .txt(.*) {
      root html/ttt/;
    }
    location ~ .gif(.*) {
      root html/ttt/;
    }
    location ~ .jpg(.*) {
      root html/ttt/;
    }
    location ~ .css(.*) {
      root html/ttt/;
    }
    location ~ .js$ {
      root html/ttt/;
    }
    location ~ .php$ {
      root html/ttt;
      fastcgi_pass 127.0.0.1:9000;
      fastcgi_index index.php;
      fastcgi_buffers 8 256k;
      fastcgi_connect_timeout 300s;
      fastcgi_send_timeout 300s;
      fastcgi_read_timeout 300s;
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
      include fastcgi_params;
    }
  }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/898419.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-15
下一篇 2022-05-15

发表评论

登录后才能评论

评论列表(0条)

保存