Nginx Multi-condition if judgment after rewrite, reduce back-end PHP work pressure (essay)

Source: Internet
Author: User
Tags new set

Nginx configuration file can be regarded as a program, a programmer in accordance with the logic of the habit of the installation of the Nginx configuration program, writing simple and easy to understand, and with a variety of variables and if judgments and other directives can be flexible to do various policy settings.

Work, whenever a request come over, will be executed once the configuration file, configuration file variables will be accompanied by a new set of emphasis, that is, each request is independent of the existence of space, and the configuration file of the instruction set of the number of different according to the compilation module, sometimes you configure a command, with Nginx- T test does not pass, not necessarily the instruction or syntax error, but the corresponding module is not compiled, you can use Nginx-m to view the corresponding module.

These two days in a seven-tier strategy to optimize access requests, there are a large number of Android over the 302, 301 is through the backend PHP code, in order to reduce the workload of the back-end PHP, while reducing the output of the log, try to put the processing on the Nginx layer. The idea is that when matching to a URL, and UA is a mobile phone Android, a rewrite operation, using the most basic Nginx configuration file implementation, and do not use LUA script or the like.

      assumes that the URL to match is http://abc.def.com/aaa.html    http://www.hig.com, android and mobile keyword, in fact, the last is 4 layers of judgment, the four-layer judgment at the same time set up rewrite, these four conditions are:

$host = "Abc.def.com" $request _uri = "aaa.html" $http _user_agent ~* "mobile" $http _user_agent ~* "Android"

After querying, nginx configuration file is not supported if nested, and does not support shell-like && and | | , so you have to think of other ways to solve, yes, can be variable flag flag to solve the way, there are many examples on the net, and finally I am writing the following:

Set $flag 0;if ($host = "abc.def.com") {set $flag "1";   }if ($request _uri = "/aaa.html") {set $flag "${flag}1";   }if ($http _user_agent ~* "mobile") {set $flag "${flag}1";   }if ($http _user_agent ~* "Android") {set $flag "${flag}1";   }if ($flag = "1111") {rewrite ^/(. *) $ http://www.hig.com redirect; }

among them note: There are spaces after the if directive, and a few 1 indicate several judgments.

This article is from the "Running Linux" blog, so be sure to keep this source http://benpaozhe.blog.51cto.com/10239098/1793733

Nginx Multi-condition if judgment after rewrite, reduce the back-end PHP work pressure (essay)

Related Article

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.