大网数据广告活动
服务器运维问答

nginx: [emerg] "if" directive is not allowed here

· 52CE 我爱测

Can you help me? I'm trying to block all requests outside my domain, and allow the request if a specified parameter exists location ~* \.(mp4|vtt|mp3|mkv|avi)$ { if ( $http_referer !~* 'mywebsite.com' ) { # check http_referer if …

问题原文

原帖链接 · Vixarc · CC BY-SA 4.0

Can you help me? I'm trying to block all requests outside my domain, and allow the request if a specified parameter exists

location ~* \.(mp4|vtt|mp3|mkv|avi)$ {
        if ( $http_referer !~* 'mywebsite.com' ) { # check http_referer
               if ($arg_78up = '') { #check if parameter 78up is empty
                        return 404; # then redirect to 404 page
               }
        }
}

And I'm getting this error when nginx starts:

nginx: [emerg] "if" directive is not allowed here in /etc/nginx/sites-enabled/default:17

What should I do to fix this issue? Thank you!

社区高分解答

原帖链接 · Tero Kilkanen · CC BY-SA 4.0

nginx does not allow nested if statements. You need to use map functionality to achieve your goal.

In the http level, add the map:

map "$http_referer:$arg_78up" $refernotok {
    default 1;
    ~ mywebsite.com.*:.*$ 0;
    ~ ^.+:.+$ 0;
}

if ($refernotok = "1") {
    return 404;
}

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2019-02-24)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。