Michael Gradek
Michael Gradek

Reputation: 2738

How to redirect naked domain when you host multiple domains

I have a nginx server which does a proxy_pass to an app running on 127.0.0.1:8080

The app serves different contents according to the domain/host it came from. Imagine something like:

So all domains DNS settings are pointing to the same IP, and ngxinx simply does a proxy_pass and the application decides what to content to serve.

Now I want to redirect (permanently) the naked domain to the www. domain.

How would I go about to do this? I've seen examples, but they all hosted only 1 domain and they set a server name which I don't think I can, can I?

Thanks

Upvotes: 0

Views: 441

Answers (1)

Ivan Tsirulev
Ivan Tsirulev

Reputation: 2811

Just add another server block to your configuaration, whose only purpose will be to take requests to your www-less domain and redirect them to its www counterpart:

server {
    listen 80;
    server_name example-spain.com;
    return 301 http://www.example-spain.com$request_uri;
}

You can add as many server blocks as your like. This method is actually recommended by the Nginx developers.

Upvotes: 1

Related Questions