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.