Nginx unknown directive "if($domain"

nginx

Solution

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.

Problem

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 ?

Original source