Nginx未知指令“if($ domain”

时间:2013-11-29 12:55:44

标签: nginx

Nginx抱怨我配置的以下部分:

nginx: [emerg] unknown directive "if($domain" in /etc/nginx/nginx.conf:38
nginx: configuration file /etc/nginx/nginx.conf test failed

这是它正在讨论的内容:

server_name ~^(?:(?<subdomain>\w*)\.)?(?<domain>\w+)\.(?<tld>(?:\w+\.?)+)$;

    if($domain = "co") {
        set $domain "${subdomain}";
        set $subdomain "www";
        set $tld "co.${tld}";
    }

    if ($subdomain = "") {
        set $subdomain "www";
    }
    root /var/www/sites/$domain.$tld/$subdomain;

    location / {
        index  index.php index.html index.htm;
    }

以下是配置文件的完整服务器部分:

server {
listen 80;
    server_name ~^(?:(?<subdomain>\w*)\.)?(?<domain>\w+)\.(?<tld>(?:\w+\.?)+)$;

    if($domain = "co") {
        set $domain "${subdomain}";
        set $subdomain "www";
        set $tld "co.${tld}";
    }

    if ($subdomain = "") {
        set $subdomain "www";
    }
    root /var/www/sites/$domain.$tld/$subdomain;

    location / {
        index  index.php index.html index.htm;
    }
    }

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

}

问题是什么?

1 个答案:

答案 0 :(得分:61)

希望你现在已经解决了这个问题,但对于那些正在努力解决类似问题的人来说。您需要在if语句和左括号之间包含一个空格。

因此,在您的示例中,您需要更改行

if($domain = "co") {

if ($domain = "co") {

一切都应该正常。