搜尋
首頁運維Nginxnginx怎麼設定偽靜態和適配客戶端

后端用的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/$1/ permanent;
        break;
      }
      #电脑首页分类首页
      if ($request_uri ~* ^/home/index/index/cate/(\d*).html$) {
        rewrite ^/home/index/index/cate/(\d*).html$ /web/cates/$1/ permanent;
        break;
      }
      #电脑首页分类分页
      if ($request_uri ~* ^/home/index/index/cate/(\d*)/page/(\d*).html$) {
        rewrite ^/home/index/index/cate/(\d*)/page/(\d*).html$ /web/cates/$1/pages/$2/ 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/$1/ permanent;
        break;
      }
      #电脑章节页面
      if ($request_uri ~* ^/home/index/contents/books/(\d*)/chapters/(\d*).html$) {
        rewrite ^/home/index/contents/books/(\d*)/chapters/(\d*).html$ /web/books/$1/chapters/$2/ 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/$1/ permanent;
        break;
      }
      #手机首页分类首页
      if ($request_uri ~* ^/home/phone/index/cate/(\d*).html$) {
        rewrite ^/home/phone/index/cate/(\d*).html$ /phone/cates/$1/ permanent;
        break;
      }
      #手机首页分类分页
      if ($request_uri ~* ^/home/phone/index/cate/(\d*)/page/(\d*).html$) {
        rewrite ^/home/phone/index/cate/(\d*)/page/(\d*).html$ /phone/cates/$1/pages/$2/ permanent;
        break;
      }
      #手机首页搜索
      if ($request_uri ~* ^/home/phone/index.html?(.*)) {
        rewrite ^/home/phone/index.html(.*) /phone/index.html$1 permanent;
        break;
      }
      #手机书籍页面
      if ($request_uri ~* ^/home/phone/books/book/(\d*).html$) {
        rewrite ^/home/phone/books/book/(\d*).html$ /phone/books/$1/ permanent;
        break;
      }
      #手机章节页面
      if ($request_uri ~* ^/home/phone/contents/books/(\d*)/chapters/(\d*).html$) {
        rewrite ^/home/phone/contents/books/(\d*)/chapters/(\d*).html$ /phone/books/$1/chapters/$2/ permanent;
        break;
      }
      
      if ($request_uri ~* ^/home/book/(.*)$) {
        rewrite ^(.*)$ /index.php?s=$1 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$1 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/$1.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/$1.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/$1/page/$2.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/$1.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/$1/chapters/$2.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$1 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/$1.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/$1.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/$1/page/$2.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/$1.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/$1/page/$2.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/$1/chapters/$2.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;
    }
  }

以上是nginx怎麼設定偽靜態和適配客戶端的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文轉載於:亿速云。如有侵權,請聯絡admin@php.cn刪除
NGINX與Apache:網絡託管和流量管理NGINX與Apache:網絡託管和流量管理Apr 12, 2025 am 12:04 AM

NGINX适合高并发和低资源消耗场景,Apache适用于需要复杂配置和功能扩展的场景。1.NGINX以高性能处理大量并发连接著称。2.Apache以稳定性和丰富模块支持见长。选择时需根据具体需求决定。

NGINX:現代Web應用程序的多功能工具NGINX:現代Web應用程序的多功能工具Apr 11, 2025 am 12:03 AM

NGINXisessentialformodernwebapplicationsduetoitsrolesasareverseproxy,loadbalancer,andwebserver,offeringhighperformanceandscalability.1)Itactsasareverseproxy,enhancingsecurityandperformancebycachingandloadbalancing.2)NGINXsupportsvariousloadbalancingm

NGINX SSL/TLS配置:使用HTTPS確保您的網站NGINX SSL/TLS配置:使用HTTPS確保您的網站Apr 10, 2025 am 09:38 AM

通過Nginx配置SSL/TLS來確保網站安全,需要以下步驟:1.創建基本配置,指定SSL證書和私鑰;2.優化配置,啟用HTTP/2和OCSPStapling;3.調試常見錯誤,如證書路徑和加密套件問題;4.應用性能優化建議,如使用Let'sEncrypt和會話復用。

NGINX面試問題:ACE您的DevOps/System Admin面試NGINX面試問題:ACE您的DevOps/System Admin面試Apr 09, 2025 am 12:14 AM

Nginx是高性能的HTTP和反向代理服務器,擅長處理高並發連接。 1)基本配置:監聽端口並提供靜態文件服務。 2)高級配置:實現反向代理和負載均衡。 3)調試技巧:檢查錯誤日誌和測試配置文件。 4)性能優化:啟用Gzip壓縮和調整緩存策略。

NGINX緩存技術:改善網站性能NGINX緩存技術:改善網站性能Apr 08, 2025 am 12:18 AM

Nginx缓存可以通过以下步骤显著提升网站性能:1)定义缓存区和设置缓存路径;2)配置缓存有效期;3)根据不同内容设置不同的缓存策略;4)优化缓存存储和负载均衡;5)监控和调试缓存效果。通过这些方法,Nginx缓存能减少后端服务器压力,提升响应速度和用户体验。

帶Docker的NGINX:部署和縮放容器化應用程序帶Docker的NGINX:部署和縮放容器化應用程序Apr 07, 2025 am 12:08 AM

使用DockerCompose可以簡化Nginx的部署和管理,通過DockerSwarm或Kubernetes進行擴展是常見的做法。 1)使用DockerCompose定義和運行Nginx容器,2)通過DockerSwarm或Kubernetes實現集群管理和自動擴展。

高級NGINX配置:掌握服務器塊和反向代理高級NGINX配置:掌握服務器塊和反向代理Apr 06, 2025 am 12:05 AM

Nginx的高級配置可以通過服務器塊和反向代理實現:1.服務器塊允許在一個實例中運行多個網站,每個塊獨立配置。 2.反向代理將請求轉發到後端服務器,實現負載均衡和緩存加速。

NGINX性能調整:針對速度和低潛伏期進行優化NGINX性能調整:針對速度和低潛伏期進行優化Apr 05, 2025 am 12:08 AM

Nginx性能調優可以通過調整worker進程數、連接池大小、啟用Gzip壓縮和HTTP/2協議、使用緩存和負載均衡來實現。 1.調整worker進程數和連接池大小:worker_processesauto;events{worker_connections1024;}。 2.啟用Gzip壓縮和HTTP/2協議:http{gzipon;server{listen443sslhttp2;}}。 3.使用緩存優化:http{proxy_cache_path/path/to/cachelevels=1:2k

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解鎖Myrise中的所有內容
3 週前By尊渡假赌尊渡假赌尊渡假赌

熱工具

Dreamweaver Mac版

Dreamweaver Mac版

視覺化網頁開發工具

MantisBT

MantisBT

Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

將Eclipse與SAP NetWeaver應用伺服器整合。

VSCode Windows 64位元 下載

VSCode Windows 64位元 下載

微軟推出的免費、功能強大的一款IDE編輯器

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )專業的PHP整合開發工具