Nginx supports mobile applications

Source: Internet
Author: User
1. mobile phone users are usually judged by User-Agent. If you copy a copy from the Internet, all the items listed are good. In my configuration, Java, curl, and Wget are added, facilitate debugging and other internal project capture

1. Determine the mobile phone user

Generally, the User-Agent is used to determine whether to copy data from the Internet. The list is good. Java, curl, and Wget are added to my configuration, facilitate debugging and capture of other internal projects.

Because many Mobile Phone Gateways do not send User-Agent messages, most of the User-Agent messages sent by mobile phones are filtered out by the gateway, which is equivalent to a null value. After sampling, the User-Agent is empty and has a large proportion of mobile users. Some users-agents are empty for spider or spam access. These spam traffic is not that important. We hope that the Mobile Phone gateway will have a standard in the future. Do not send an empty User-Agent, that is, send a letter.

If an app has an independent domain name, you may not need to make a mobile phone decision. For example, if Sina has an independent domain name that is deeply rooted in the hearts of the people, it does not matter if it does not jump.

Nginx configuration uses the exhaustive method to list all types of mobile phone User-Agent and transfers the empty User-Agent to the mobile phone application. In other cases, skip to the Help Page.


Set$ Ismob 0;
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 ))
{
Set $ ismob 1;
Proxy_pass http://m.sudone.com;
}
If ($ http_user_agent ~ ^ $)
{

Set $ ismob 1;
Proxy_pass http://m.sudone.com;
}
If ($ ismob = 0)
{
ReWrite^. * $ Http://help.m.sudone.com/peRmAnent;
}


Another configuration method is to use the nginx browser module for configuration. For details, refer:

Http://sudone.com/nginx/nginx_browser_module.html

Note: Use the precedingRegular ExpressionThe expression method requires that the nginx version is 0.7.43 or earlier. problems may occur between version 0.7.44 and version 0.8.

2. Content-Type

Mobile browsers differ from normal computers in accessing pages. Most mobile phones do not support tExT/html: Content-Type format. In terms of web Services, apart from making pages suitable for mobile browsing, the other important thing is to correct the Content-Type. Generally, the mobile phone uses text/vnd. wap. wml and application/xhtml + xml. I heard that text/vnd. wap. wml is dedicated for vintage phones, application/xhtml + xml is the Content-Type specified by the 3G standard, and charset needs to be specified as a UTF-8.

Therefore, Content-Type should be as follows:


Content-Type: application/xhtml + xml; charset = UTF-8

That's right.

For dynamic pages, Content-Type can be set in the program.

For examplePhp:

Header (Content-Type: application/xhtml + xml; charset = UTF-8)

If jsp is used, modify the top page.


<% @ Page contentType = application/xhtml + xml; charset = UTF-8 %>

The settings in the dynamic program will be inherited by default on nginx, so you don't have to worry too much. One thing nginx needs to do is static pages, and the other is 302 redirection.

Changing the Content-Type of a static page is not troublesome. Modify mime. types:

Application/xhtml + xml html htm shtml;
Application/xhtml + xml;


Change the type of the required extension. Then in nginx. specify the charset UTF-8 in conf; and add charset_types text/vnd. wap. wml; and charset_types application/xhtml + xml;, charset_types commands are not available in nginx of version 0.6. Therefore, version 0.7 is required, and version 0.7.43 is generally used.

The trouble is that 301 and 302 jump, nginx uses rewrite rEdWhen CT and permanent jump, how to change the Content-Type will be text/html, useDd_ Header and Content-Type are changed to two rows, which cannot be achieved. Using proxies to dynamic programs is acceptable, but performance and stability become a problem. Finally, I checked the nginx source code and found that the text/html was completely written ......

The nginx version I debugged is 0.7.30. The file is:

Vi./src/http/ngx_http_special_response.c

568 // r-> headers_out.content_type_len = sizeof (text/html)-1;

569 // r-> headers_out.content_type.len = sizeof (text/html)-1;
570 // r-> headers_out.content_type.data = (u_char *) text/html;

Comment out the three lines of code 568, 569, 570, so that Content-Type is not sent during nginx jump. I found that normal jump can be performed without the Content-Type in the IE/FF browser on the computer.

Of course, to be more rigorous, write Content-Type. Now you can use add_header to define it:

Add_header Content-Type text/html;


The mobile phone is:

Add_header Content-Type application/xhtml + xml;

3. Cache

If the front-end is not nginx, but squid cache, the page or jump may be cached, resulting in access problems, you need to use the Vary header. The response header of the web page generally has a Vary header (server supportCompressionIt means that squid treats a header sent from the client differently, such as Vary: Accept-E.NcOding, the client sends Accept-Encoding: gZip(MSIE), when deflate, squid will beGzip, Deflate to make an archive. If the client does not send the Accept-Encoding header (wget), squid will make another archive for the non-Accept-Encoding. The two archive content is different, gzip and deflate are compressed content with a bunch of garbled characters. If there is no Accept-Encoding, the webpage source code is used.

Knowing the principles of Vary makes it easy to do the rest. Adding Vary: User-Agent to webpage output means that squid needs to save an archive for each User-Agent, configure nginx as follows:

Add_header Vary User-Agent;


At this time, the response header of the webpage will have two lines of Vary:

Vary: Accept-Encoding;
Vary: User-Agent;

This is harmless and can work normally.

Currently, the User-Agent has been changed by a variety of modified browsers, so many versions are available. This will create a lot of archives on squid, and the penetration will also increase accordingly. Therefore, you need to monitor it carefully.

4. Kernel configuration


On nginx, especially the proxy machine, the system administrator usually sets these two kernel parameters to 1:

/Proc/sys/net/ipv4/tCp_ Tw_recycle (net. ipv4.tcp _ tw_recycle) = 1
/Proc/sys/net/ipv4/tcp_tw_reuse (net. ipv4.tcp _ tw_reuse) = 1

However, this configuration may cause problems when the mobile phone uses CMWAP for access. Therefore, set these two parameters to 0.

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.