DDEV: Redirect http to https using nginx-fpm and on various domains

Viewed 39

I'm moving some small websites in production to DDEV and, some of them has multiple domains with a 301 redirection to the main HTTPS site.

This config was working well with the "natural" Nginx when I was using a .conf file to manage the domains that should be redirect to the main site on this way:

server {
    listen 80;
    server_name     .domain1.com
            .domain2.com
            .domain3.com
    ;
    return 301 https://www.maindomain.com;
}

I tried to create a new domains.conf file and add it inside the .ddev/nginx_full directory to be loaded in the restart process but seems the Nginx didn't recognize such file.

In the main "natural" Nginx config file I has this server to redirect all requests coming from HTTP to HTTPS:

server {
        listen 80;
        access_log off;
        error_log off;
        server_name maindomain.com www.maindomain.com;
        return 301 https://www.$host$request_uri;
}

I tried to add these configs inside the .ddev/nginx_full/nginx-site.conf file but the server start to be crazy, doing sometimes infinite redirections and sometimes, not recognize the domains.

Inside the config.yaml file I have:

additional_fqdns:
- domain1.com
- domain2.com
- domain3.com
- maindomain.com
- www.maindomain.com
use_dns_when_possible: false

I'm sure that's a "right way" to handle this situation but, looking the docs, I didn't find and answer for that. On this way, I ask if someone here have the catch for that.

Thanks a lot

2 Answers

I think this will work for you.

Add the file .ddev/nginx/redirect.conf with these contents:

    if ($http_x_forwarded_proto = "http") {
      return 301 https://$host$request_uri;
    }

This uses a DDEV nginx snippet, it could also be done with a full nginx config.

The ddev-router acts as a reverse proxy that terminates SSL/443 and passes along requests on port 80 to the web container.

You see the infinite redirects because it sees the request always on port 80.

Related