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

I do have an nginx server which responds to several domains and I do want to redirect all request to the main domain.

Example: website responding for xxx xxx.example.com yyy.example.com $hostname for both http and https.

I want to configure the server in such way that all requests to the other domain names will be redirected to xxx.example.com.

share|improve this question
add comment

2 Answers 2

up vote -1 down vote accepted

I thinks something along these lines should work:

set $primary_domain "xxx.example.com";
if ($host != $primary_domain) {
    rewrite ^ $scheme://$primary_domain permanent;
}
share|improve this answer
2  
This is the worst way to solve the problem. wiki.nginx.org/Pitfalls#Server_Name and nginx.org/en/docs/http/converting_rewrite_rules.html –  VBart Nov 8 '12 at 19:24
add comment

Most efficient and clean way to do this is to configure two separate server{} blocks - one to do redirects, and another one (with canonical name) to actually handle requests.

Example configuration:

server {
    listen 80;
    listen 443 ssl;

    server_name xxx yyy.example.com $hostname;

    ssl_certificate ...
    ssl_certificate_key ...

    return 302 $scheme://xxx.example.com$request_uri;
}

server {
    listen 80;
    listen 443 ssl;

    server_name xxx.example.com;

    ssl_certificate ...
    ssl_certificate_key ...

    ...
}

Documentation:

share|improve this answer
add comment

Your Answer

 
discard

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.