Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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;
# Login Short Cut
if ($uri ~* "/login") {
    rewrite ^/login(/.*)? /wp-admin$1;
share|improve this question

2 Answers 2

up vote 1 down vote accepted

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_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.

share|improve this answer
thank you so much for you help, I really appreciate it. –  chrishough Nov 25 '11 at 20:18

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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.