qepwqlpf 发表于 2013-7-9 11:10:22

军哥,您好,编程的问题。

在conf文件里,我想同时满足二个条件
if ( $http_user_agent ~* "compatible; MSIE 6.0; Windows NT 5.1; SV1;"|$ http_referer ~* "news") {

            return 403;

                }



禁止ie6的用户访问包含news关键词的url

同时满足这二个条件的才生效,但是我这样写报错。军哥能否帮我纠正一下。很重要。谢谢。

licess 发表于 2013-7-9 16:32:03

$http_referer是记录从哪个地址过来的
$request 才是用户访问的url

同时满足是与运算用&

qepwqlpf 发表于 2013-7-10 13:57:30

军哥好,语法有错误。

if ( $http_user_agent ~* "compatible; MSIE 6.0; Windows NT 5.1; SV1;" & $request~* "news") {

            return 403;

                }

这样写会报错,麻烦再帮我看看。
/usr/local/nginx/sbin/nginx -t
nginx: unexpected "&" in /usr/local/nginx/conf/sitestar.conf:10
nginx: configuration file /usr/local/nginx/conf/nginx.conf test failed

licess 发表于 2013-7-10 20:53:56

if ( $http_user_agent ~* "compatible; MSIE 6.0; Windows NT 5.1; SV1;") && ( $request~* "news" ) {
这样试试

qepwqlpf 发表于 2013-7-11 17:05:27

军哥。改成这样还不行,麻烦再帮我看一下。万份感谢了啊。

# /etc/init.d/nginx   restart
Restarting nginx daemon: nginxnginx: invalid condition "$http_user_agent                      " in /usr/local/nginx/conf/sitestar.conf:10
nginx already running.

军哥。改成这样还不行,麻烦再帮我看一下。万份感谢了啊。

if ( $http_user_agent ~* "compatible; MSIE 6.0; Windows NT 5.1; SV1;") && ( $request~* "news" ) {

               return http://www.weimeigu.com/tip/;

                }

licess 发表于 2013-7-11 18:28:32

看了一下nginx的if判断貌似不支持与或运算也不支持if语句嵌套
那就
set $rule 0;
                if ($request~* ’news‘)
                {
                        set $rule "${rule}1";
                }
                if ($http_user_agent ~* 'MSIE 6')
                {
                     set $rule "${rule}2";
                }
                if ($rule = "012")
                {
                        deny all;
                }

这样试试看吧

qepwqlpf 发表于 2013-7-12 13:17:23

万份感谢了
页: [1]
查看完整版本: 军哥,您好,编程的问题。