0

nginx redirection is not working as expected with ssl and www redirection.

I have two domains say, domain1.com and domain2.com. I need to have https with www redirection on both ie https:/www.domain1.com. and https:/www.domain2.com.

but when i take www.domain2.com its redirected to https:/www.domain1.com.

Please see the configurations

domain1.com

server {
    listen       8080;
    server_name  domain1.com;
    return       301 https://www.domain1.com$request_uri;
}

server {
    listen 443 default_server;
    ssl on;
    ssl_certificate /root/ssl/dom1/unified.crt;
    ssl_certificate_key /root/ssl/dom1/my-private-decrypted.key;

    root /var/www/dom1.com/html;
    index index.php index.html index.htm;

    server_name  www.domain1.com;

    }

domain2.com

server {
    listen       8080;
    server_name  domain2.com;
    return       301 https://www.domain2.com$request_uri;
}

server {
    listen 443;
    ssl on;
    ssl_certificate /root/ssl/dom2/unified.crt;
    ssl_certificate_key /root/ssl/dom2/my-private-decrypted.key;

    root /var/www/dom2.com/html;
    index index.php index.html index.htm;

    server_name  www.domain2.com;

        }
  • Please state your situation and problem more clearly by editing your question. Do you want to redirect both domains from http to https? Why are you listening on 8080 rather than 80? Please ensure you're posting your full nginx configuration, along with the exact request you're making - use curl or Firefox with the plugin "live http headers". Post the access and error logs for the request. This will be something to do with misconfiguration and Nginx choosing a default server since you haven't specified one. I create a separate server which is a dummy / default server. – Tim Mar 19 '16 at 22:19
  • You can download a default server config from the resources section of this page photographerstechsupport.com/tutorials/… – Tim Mar 19 '16 at 22:20
  • 1
    So basically default_server tells nginx to handle any requests it doesn't have an active config for here. So when domain2.com is down nginx treats it as an unknown domain and processes the request at domain1.com – Drifter104 Mar 19 '16 at 22:33
  • @Tim Yes, i need both the domains to redirect from http to https. port 8080 is used since im using varnish. varnish is listening at 80 – Manu Janardhanan Mar 20 '16 at 3:17
  • Ok. Suggest you read the rest of my comment, try what I suggested, and if you still need help post the information I requested. – Tim Mar 20 '16 at 3:48
0

Posting the solution, in domain2.com the configuration is as follows. Now domain2.com and www.domain2.com redirects to https:/www.domain2.com

server {
    listen       8080;
    server_name  domain2.com www.domain2.com;
    return       301 https://www.domain2.com$request_uri;
}

server {
    listen 443 ssl;
    server_name  www.domain2.com;
    ssl_certificate /root/ssl/dom2/unified.crt;
    ssl_certificate_key /root/ssl/dom2/my-private-decrypted.key;

    root /var/www/dom2.com/html;
    index index.php index.html index.htm;


        }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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