Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nginx unknown directive "if($domain"

Tags:

Nginx Complains about the following part of my configuration:

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

Here is the bit it is talking about:

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;
    }

Here is the full server section of my configuration file:

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;
    }

}

What is the issue ?

like image 230
James Campbell Avatar asked Nov 29 '13 12:11

James Campbell


1 Answers

Hopefully you have this solved by now but for anyone else who is struggling with a similar issue. You need to include a space between the if statement and the opening parenthesis.

So in your example you need to change the line

if($domain = "co") {

to

if ($domain = "co") {

And everything should work fine.

like image 71
StephenD Avatar answered Sep 18 '22 15:09

StephenD