nginx根据ip进行转发

今天有这样一个需求:

http://xxxx/yyy

如果请求来自于某个特殊ip,则访问:

http://xxxx/yyyy_test

之前想的是nginx中的proxy很easy就可以实现,判断一下来源,进行转发。

类似于:

location /yyyy/ {
                if ( $remote_addr ~* "1.2.3.4" ){
                        proxy_pass http://127.0.0.1/yyyy_test;
                        break;
                }
        }

结果发现 yyyy_test uri的变动无法应用至 if 代码段:

nginx: [emerg] "proxy_pass" cannot have URI part in location given by regular expression, or inside named location, or inside "if" statement, or inside "limit_except" block in /opt/nginx/conf/nginx.conf:125

最终修改为如下:

另开一个81端口的server进行跳转一下

        location /moonsrc/ {
                if ( $remote_addr ~* "1.2.3.4" ){
                        proxy_pass http://127.0.0.1:81;
                        break;
                }
                if ( $http_x_forwarded_for ~* "1.2.3.4" ){
                        proxy_pass http://127.0.0.1:81;
                        break;
                }
        }
    }

    server {
        listen       81;
        server_name  abc.com;

        location / {
            root   html;
            index  index.html index.htm index.php;
                autoindex on;
        }

        location /yyyy/ {
                proxy_pass http://127.0.0.1:81/yyyy_test/;
        }

    }
发表评论?

0 条评论。

发表评论


注意 - 你可以用以下 HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>


Warning: Use of undefined constant XML - assumed 'XML' (this will throw an Error in a future version of PHP) in /opt/wordpress/wp-content/plugins/wp-syntaxhighlighter/wp-syntaxhighlighter.php on line 1048