Use the Nginx implementation to specify Url_nginx based on IP matching

Source: Internet
Author: User
Tags lua

Business requirements

Business and development colleagues need my side to do a rule, all access IP for non-Shanghai, Guangzhou Office extranet ip,url for http://test.com/fuck/index.html request to jump to http://test.com/index.html. Then all the extranet IP access http://test.com/fuck/index.html in Shanghai and Guangzhou office is still http://test.com/fuck/index.html. This allows for isolation in production and does not affect the services of other users.

Note: Because the current production of Nginx does not do LUA support, so can not use LUA to achieve this requirement, nor install GEOIP, so also can not use modules to support, only native.

The original Nginx configuration

Upstream Service_test {
     server 127.0.0.1:8080;
}
Server
 {
  listen    ;
  server_name test.com;
  Index index.html index.php;
  root/tmp/test.com;
  Error_page 404 http://test.com/404.html;
  Error_page 502 http://test.com/502.html;
  Error_page http://test.com/500.html;
  Location ~* \. (GIF|JPG|JPEG|PNG|CSS|JS|ICO|TXT|SVG|WOFF|TTF|EOT) $
  {
    rewrite ^ (. *) $/static$1 break;
    root/tmp/test.com; # 
    expires 1d;
  }
  Location ~* \. (html|htm) $
  {
    rewrite ^ (. *) $/static$1 break;
    roo/tmp/test.com; # 
    expires 900s;
  }
  Location/{
     Proxy_pass http://service_test;
     include/opt/conf/nginx/proxy.conf;
  }

Modified Nginx Configuration

Upstream Service_test {
     server 127.0.0.1:8080;
}
Server
 {
  listen    ;
  server_name test.com;
  Index index.html index.php;
  root/tmp/test.com;
  Error_page 404 http://test.com/404.html;
  Error_page 502 http://test.com/502.html;
  Error_page http://test.com/500.html;
  Location ~* \. (GIF|JPG|JPEG|PNG|CSS|JS|ICO|TXT|SVG|WOFF|TTF|EOT) $
  {
    rewrite ^ (. *) $/static$1 break;
    root/tmp/test.com; # 
    expires 1d;
  }
  Location ~* \. (html|htm) $
  {
    rewrite ^ (. *) $/static$1 break;
    roo/tmp/test.com; # 
    expires 900s;
  }
  Set $flag 0;
  if ($request _uri ~* "^/fuck/\w+\.html$") {
      set $flag "${flag}1";
  }
  if ($remote _addr!~* "192.168.0.50|192.168.0.51|192.168.0.56") {
    set $flag "${flag}2";
  }
  if ($flag = "012") {
    rewrite ^/index.html permanent;
  }
  Location/{
     Proxy_pass http://service_test;
     include/opt/conf/nginx/proxy.conf;
  }

Issues that arise in the process of implementing requirements

If the if instruction and Proxy_pass are placed under location, the contents of the if instruction will not be executed, only the Proxy_pass will be executed.

Location/{
   if ($remote _addr!~* "192.168.0.50|192.168.0.51|192.168.0.56") {
      rewrite ^/index.html permanent;< c3/>}
   proxy_pass http://service_test;
   include/opt/conf/nginx/proxy.conf;
}

Use the proxy_pass instruction problem under the IF directive

Use like the following to make an error, the wrong way:

    if ($remote _addr ~* "192.168.0.50|192.168.0.51|192.168.0.56") {
      proxy_pass http://test.com/fuck;
    }

The right way:

    if ($remote _addr ~* "192.168.0.50|192.168.0.51|192.168.0.56") {
      proxy_pass http://test.com$request_uri;
    }

Or

    if ($remote _addr ~* "192.168.0.50|192.168.0.51|192.168.0.56") {
      proxy_pass http://test.com;
    }


If you are directly starting a location, for example, start the following location:

  Location/fund {
     if ($remote _addr!~* "192.168.0.50|192.168.0.51|192.168.0.56") {
        rewrite ^/index.html Permanent
     }
  }

Such a way is not supported, when using IP 192.168.0.50 access, did not meet our business needs, will be the error 400

Note: You have other good suggestions, welcome to explore.

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.