Nginx PC端和移動端區分
阿新 • • 發佈:2020-12-02
server { listen 80; server_name mike.com; index index.php index.html index.htm default.html default.htm default.php; root /www/site/mike.com; add_header Set-Cookie "site_type=1;Path=/"; set $is_mobile no; if ($http_user_agent ~* "(mobile|nokia|iphone|ipad|android|samsung|htc|blackberry)") { set $is_mobile yes; } location / { add_header Access-Control-Allow-Origin *; if ($is_mobile = "yes") { root /www/site/mike.com/m; } ssi on; } location/m { ssi on; } location /uploads { proxy_pass http://www.baidu.com/uploads/; } location /site_app/ { alias /www/site/app/; } location = /favicon.ico { log_not_found off; access_log off; } location ~ /\. { deny all; access_log off; log_not_found off; } }
其中主要區分PC端和手機端的配置是以下這裡
set $is_mobile no; if ($http_user_agent ~* "(mobile|nokia|iphone|ipad|android|samsung|htc|blackberry)") { set $is_mobile yes; } location / { add_header Access-Control-Allow-Origin *; if ($is_mobile = "yes") { root /www/site/mike.com/m; } ssi on; } location /m { ssi on; }
根據網站根路徑下目錄進行區分的,PC 端路徑程式碼是 /www/site/mike.com,移動端端路徑程式碼是 /www/site/mike.com/m
這樣就可以電腦開啟自動判斷到PC端路徑下的程式碼,手機開啟直接判斷到 移動端路徑下的程式碼
實現功能:
PC端輸入開啟 mike.com URL自動跳到PC端程式碼下
移動端輸入開啟 mike.com URL自動跳到移動端程式碼下
這樣做的好處都是同一個域名,不用申請或者使用過多的二級域名~~~
本文分享完畢,感謝支援點贊~~