Nginx If condition judgment

Source: Internet
Author: User

Nginx If condition judgment:

1. The company's website on-line has this demand:

Because the company website domain name from the HTTP to the HTTPS transfer, in the testing phase needs the company internal testing, the company internal LAN access to enforce access to the encrypted HTTPS service, external users access to the normal HTTP service.

The first method:

if ($http _x_forwarded_for ~ ^106\. . . | . . 103\.  - ) {        return301 https://Www.xxx.cn$request_uri;         } #有的说nginx不支持这种if写法, but I was in the Nginx configuration is successful, may be the version after the high, Nginx also support it. My nginx is 1. 8. version 0.

The second method:

#用变量的方式来间接实现Set$flag0;if($http _x_forwarded_for ~ ^106\. -\. -\. the| About\. A\.103\. -) {    Set$flag"${flag}1";}if($flag =" on") {    return 301Https//Www.xxx.cn$request_uri;}

$remote _addr to get the client access address, use $http_x_forwarded_for if the site uses a front-end proxy or load balancer

####################################################################################################

2. When accessing a PHP application, I just want the IP address to be 106.38.53.130 access, and the other IP to jump to another page. As follows:

#访问/cms/index.php, and the IP address is not 106.38.53.130 jump to https://www.xxoo.cn

Set$ssl _80"'; if($request _uri ~*/cms/index.php) {//full request path for client requests                Set$ssl _80 A; }        if($http _x_forwarded_for!~* ^106\. -\. -\. the.* ) {//client IP address with load-balanced front end               Set$ssl _80"${ssl_80}b"; }        #if($remote _addr!~* ^10\.105\.99\.158.*) {//Client IP Address        #       Set$ssl _80"${ssl_80}c"; #}        if($ssl _80 =AB) {                #return 403; Rewrite^ (. *) $ https://www.xxoo.cn Permanent;}

$request _uri is the full path of the client request
$http _user_agent is the browser parameter for the client to make the request

$args parameter values in the request

Reference Documentation:

Http://www.360doc.com/content/15/0119/14/15398874_442036739.shtml

Http://www.cnblogs.com/raichen/p/5121262.html

https://my.oschina.net/boonya/blog/287265

Nginx If condition judgment

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.