When the PC and mobile phones need to access different domain names, we can distinguish different clients through Nginx.
By adding a jump to the Nginx configuration file, use user-agent to determine whether the client is mobile or PC:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
server { listen 80; server_name www.nginxer.com nginxer.com; rewrite .* https://$host$request_uri last; //ssl } server { listen 443 ssl; server_name www.nginxer.com nginxer.com; \ root /home/wwwroot/nginxer; charset utf-8; ssl_certificate /usr/local/nginx/SSL/_.nginxer.com.crt; ssl_certificate_key /usr/local/nginx/SSL/_.nginxer.com.key; ... if ( $http_user_agent ~* "(Android|webOS|iPhone|iPad|BlackBerry)" ){ // Judge user-agent rewrite ^/(.*)$ https://mobile.nginxer.com$uri redirect; // redirect / permanent } |
In this way, when the mobile terminal accesses the domain name www.nginxer.com, the browser jumps to the domain name mobile.nginxer.com.
redirect means 302 jump (temporary transfer)
permanent means 301 jump (permanent transfer).
This article was first published by V on 2018-11-08 and can be reprinted with permission, but please be sure to indicate the original link address of the article :http://www.nginxer.com/records/nginx-distinguishes-between-pc-and-mobile-to-access-different-domain-names/