Nginx specify IP or specify user_agent access question

set $flag 0;
location ~ /my.php.* { 
    if ( $http_user_agent !~* "user agent") {
        set $flag "${flag}1";
    }
    if ( $remote_addr != ip ){
        set $flag "${flag}2";
    }
            
    if ($flag = "012") {
        return 403;  
    }
}

but this has no effect, which god can give us some advice

Jun.10,2022
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b3b422-2bac7.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b3b422-2bac7.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?