Nginx uses the User Agent as an example.

Source: Internet
Author: User
Tags curl lenovo

Example

Mobile browser?

The code is as follows: Copy code

Location = /{
Root/var/www/path;
If ($ http_user_agent ~ * "Nokia") {rewrite./index.html break ;}
If ($ http_user_agent ~ * "Mobile") {rewrite./index.html break ;}
If ($ http_user_agent ~ * "SAMSUNG") {rewrite./index.html break ;}}
If ($ http_user_agent ~ * "SonyEricsson") {rewrite./index.html break ;}
If ($ http_user_agent ~ * "MOT") {rewrite./index.html break ;}}
If ($ http_user_agent ~ * "BlackBerry") {rewrite./index.html break ;}
If ($ http_user_agent ~ * "LG") {rewrite./index.html break ;}}
If ($ http_user_agent ~ * "HTC") {rewrite./index.html break ;}
If ($ http_user_agent ~ * "J2") {rewrite./index.html break ;}
If ($ http_user_agent ~ * "Opera Mini") {rewrite./index.html break ;}}
Index index.html;
}

Or

The code is as follows: Copy code

1. set $ ismob 0;
2. if ($ http_user_agent ~ "^ ((. * MIDP. *) | (. * WAP. *) | (. * UP. browser. *) | (. * Smartphone. *) | (. * Obigo. *) | (. * Mobile. *) | (. * AU. browser. *) | (. * wxd. mms. *) | (. * WxdB. browser. *) | (. * CLDC. *) | (. * UP. link. *) | (. * KM. browser. *) | (. * UCWEB. *) | (. * SEMC-Browser. *) | (. * Mini. *) | (. * Symbian. *) | (. * Palm. *) | (. * Nokia. *) | (. * Panasonic. *) | (. * MOT -. *) | (. * SonyEricsson. *) | (. * NEC -. *) | (. * Alcatel. *) | (. * Ericsson. *) | (. * BENQ. *) | (. * BenQ. *) | (. * Amoisonic. *) | (. * Amoi -. *) | (. * Capitel. *) | (. * PHILIPS. *) | (. * SAMSUNG. *) | (. * Lenovo. *) | (. * Mitsu. *) | (. * Motorola. *) | (. * SHARP. *) | (. * WAPPER. *) | (. * LG -. *) | (. * LG /. *) | (. * EG900. *) | (. * CECT. *) | (. * Compal. *) | (. * kejian. *) | (. * Bird. *) | (. * BIRD. *) | (. * G900/v1.0.*) | (. * Arima. *) | (. * CTL. *) | (. * TDG. *) | (. * Daxian. *) | (. * DAXIAN. *) | (. * DBTEL. *) | (. * Eastcom. *) | (. * EASTCOM. *) | (. * PANTECH. *) | (. * Dopod. *) | (. * Haier. *) | (. * HAIER. *) | (. * KONKA. *) | (. * KEJIAN. *) | (. * LENOVO. *) | (. * Soutec. *) | (. * SOUTEC. *) | (. * SAGEM. *) | (. * SEC -. *) | (. * SED -. *) | (. * EMOL -. *) | (. * INNO55. *) | (. * ZTE. *) | (. * iPhone. *) | (. * Android. *) | (. * Windows CE. *) | (Wget. *) | (java. *) | (curl. *) | (Opera. *) $ ")
3 .{
4. set $ ismob 1;
5. proxy_pass http://www.111cn.net; 6 .}
7. if ($ http_user_agent ~ ^ $)
8 .{
9. set $ ismob 1;
10. proxy_pass http://www.111cn.net; 11 .}
12. if ($ ismob = 0)
13 .{
14. rewrite "^. * $" http://www.111cn.net/permanent; 15 .}

Example

Filter YisouSpider by User Agent

Find the root directory configuration area, add the User Agent filter and judgment statement, and find that "YisouSpider" directly returns 403

Note 1: If you need to add multiple filters, do this ($ http_user_agent ~ * "Spider1 | Spider2 | Spider3 | Spider4"), separated by |

Note 2: If you are using a sub-directory blog, you need to find a section like "location/blog/" to modify it.

The code is as follows: Copy code

Location /{
Other configurations
If ($ http_user_agent ~ * "YisouSpider "){
Return 403;
}
}

After wq is configured, reload nginx and run the following command to perform self-test. Change the address. I can't do it without installing curl. I can install apt or yum as an artifact.

The code is as follows: Copy code

Curl-I-A "YisouSpider" www.111cn.net

If 403 is returned, the configuration is successful.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.