1

I am looking for a little advice here for a nginx noob. I have been reading that if statements are way slow in nginx so I was hoping for a little help in converting these. I have tried a bunch of different methods, all failures, so I thought I would toss up a question.

server_name *.noconformity.co noconformity.co;
# remove all sub domains and www
if ($http_host !~* "^(ftp|ssh)\.noconformity\.co$"){
    set $rule_0 1$rule_0;
}
if ($http_host ~ "^([^.]+)\.noconformity\.co$"){
    set $rule_0 2$rule_0;
}
if ($rule_0 = "21"){
    rewrite ^/(.*)$ http://noconformity.co/$1 permanent;
    break;
}
# Login Short Cut
if ($uri ~* "/login") {
    rewrite ^/login(/.*)? /wp-admin$1;
}

2 Answers 2

1

Completely untested - quite likely to have mistakes - but hopefully the concept is valid.

The config below is based what I interpret your objective to be: redirect all subdomains, except 'ftp' and 'ssh' to the main site; Redirect /login to /wp-admin

Create a server block to accept connections to the ftp and ssh subdomains - and then do something here (like deny, or return 403, etc) - since this is a static server_name, it takes precedence over the wildcard server_name.

server {
    server_name ftp.noconformity.co ssh.noconformity.co;
    #additional options
}

Your main server block - add your rewrite to a location block

server{
    server_name noconformity.co;
    #root ....; 
    #your main config

    location ~* /login/ {
        rewrite ^/login/(.*)? /wp-admin$1;
    }
}

{I had to change the /login/ rewrite to avoid getting stuck in a loop with the built in WP log in redirect}

Finally, a wildcard server_name - this will match any subdomain (except those already matched) capture the path, and rewrite to your main site.

server {
    server_name *.noconformity.co;
    rewrite ^/(.*)$ http://noconformity.co/$1 permanent;
}

The nginx page on server_names is worth reading, as it explains the priority of different server_name expressions.

0
0

The better answer to this question is using the HTTP Map Module. Your example above is almost the same case use as what they have demonstrated. Much cleaner and easier to manage.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .