Lime
Lime

Reputation: 65

HTTPS to HTTPS redirect Nginx

How can I redirect an HTTPS to another HTTPS (I only got one SSL certificate for a single domain)?

For example, how to redirect from https://example.org to https://example.com?

I already tried to search Google and other forums, but nothing I found worked.

Upvotes: 6

Views: 19023

Answers (2)

Rohit Parte
Rohit Parte

Reputation: 4036

Below code is worked properly for me.

server {
    listen      80;
    server_name example.com;
    rewrite     ^   https://$server_name$request_uri? permanent;
}

'rewrite' forcefully update redirect rule.

Steps:

1.Add above code in ngnix filename.conf below the server name
2.sudo service ngnix restart
3.Remove rewrite     ^   https://$server_name$request_uri? permanent;
4.sudo service ngnix restart

Example

server {
  listen 80;
  server_name example.com;
  rewrite     ^   https://$server_name$request_uri? permanent;

  root /home/ubuntu/example/dist;

  try_files $uri $uri/ /index.html;

  location /api/ {
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_set_header X-NginX-Proxy true;
    proxy_pass http://localhost:3012/;
  }

  listen 443 ssl;
  ssl_certificate /etc/ssl/example/certificate.crt;
  ssl_certificate_key /etc/ssl/example/private.key;
}

Upvotes: 0

Anatoly
Anatoly

Reputation: 15530

It is super easy to do:

server {
    listen       443 ssl;
    server_name  example.org;
    return       301 http://www.example.com$request_uri;
}

See the docs, it describes a difference between rewrite and 301 redirect (recommended since Nginx 0.9.1)

UPDATE:

http {
    ssl_certificate /etc/nginx/ssl/example.com/10599/server.crt;
    ssl_certificate_key /etc/nginx/ssl/example.com/10599/server.key;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers 'ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:AES:CAMELLIA:DES-CBC3-SHA:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!aECDH:!EDH-DSS-DES-CBC3-SHA:!EDH-RSA-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA';
    ssl_prefer_server_ciphers on;
    ssl_dhparam /etc/ssl/dhparams.pem;


    server {
        listen       80;
        listen       443 ssl;
        server_name  example.eu www.example.eu example.org www.example.org www.example.com;
        return       301 https://example.com$request_uri;
    }


    server {
        listen 443 ssl;
        server_name example.com;
        root /home/forge/example.com;

        index index.html index.htm index.php;

        client_max_body_size 20m;

        charset utf-8;

        location / {
            try_files $uri $uri/ /index.php?$query_string;
        }

        location = /favicon.ico { access_log off; log_not_found off; }
        location = /robots.txt  { access_log off; log_not_found off; }

        access_log off;
        error_log  /var/log/nginx/example.com-error.log error;

        error_page 404 /index.php;

        location ~ \.php$ {
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_index index.php;
            include fastcgi_params;
        }

        location ~ /\.ht {
            deny all;
        }
    }

}

Upvotes: 5

Related Questions