Redirect using Nginx HttpAccessModule
As Nginx supports conditional configurations, first let’s look at an example of how to redirect traffic using HttpAccessModule running a regex against $remote_addr variable.server { if ($remote_addr = 1.2.3.4) { rewrite ^ http://www.yourwebsite.com/otherpage.htm; } }In this example, 1.2.3.4 is the IP address you want to redirect.
What if you are using Nginx as front end proxy for Apache?
In this case you can do almost the same thing, redirecting the traffic to the backend servers:if ( $remote_addr = 1.2.3.4 ) { proxy_pass http://www.yourwebsite.com/otherpage.htm; }Real life examples of this include uploading a maintenance page for the rest of your office connections, and driving all the traffic there while working on your live website. Or to allow access to protected admin areas just for your IP, leaving the rest of the network without access. (From a security point of view, you should also add user and password protection.)
Redirect using Nginx Geo Module
The second alternative we have to do this is by using the Nginx Geo module. This will create variables depending on your visitor’s IP address.Usage example:
geo $bad_user { default 0; 1.2.3.4/32 1; } server { if ($bad_user) { rewrite ^ http://www.yourwebsite.com/otherpage.html; }In this example, default 0 means the value is set to the variable if the client address doesn’t match any of the specified addresses. The other value is the IP address you want to match that will be redirected later.
Không có nhận xét nào:
Đăng nhận xét