1

This setup is for multiple domain sharing a single CMS platform. It will allow any domain to point to my server ip.

I can use nginx server_name _; to catch all.

But then I would like to 301 redirect to www (appending www. infront) if www is not present. If www is present, it's fine.

How can i achieve this?

1 Answer 1

1

Something like this could work (not tested!):

server {
    server_name  ~^www\.;
    <deliver content here>
}
server {
    server_name  ~^(?!www\.);
    location / {
        return 302 $scheme://www.$host$request_uri;
    }
}

There are other options missing now! Like "listen" and "location".
You should probably add another server block with the public ip of the server which redirects non vhost traffic to a default site.
You should pay attention to the order in which the server_name directive is processed: http://nginx.org/en/docs/http/server_names.html

You must log in to answer this question.

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