Search code examples
nginx-configddev

How can I configure nginx to use 2 docroots (with different hostnames) with ddev


I want to use 2 web sites inside a ddev web container but can't find out how to configure nginx the right way. I have set up the additional_hostname to site-a and site-b to let me access the web site with site-a.ddev.site and site-b.ddev.site. The web sites are in different local directories site-a and site-b.

How can I define the "sites-available" and "sites-enabled" configuration for nginx? In the documentation I found only the way to create a .ddev/nginx-site.conf file, but with this I can only configure one web site.

I know that I can "ddev ssh" into the container and configure it by hand, but it should configure automatic when I checkout the project and made a "ddev configure" and "ddev start".


Solution

  • Two different hostnames is really easy, you just use additional_hostnames or additional_fqdns in .ddev/config.yaml. See docs.

    But from the context of your question, I assume you want to have two different docroots in the nginx config, which respond to different hostnames. If that's right, here are some options:

    1. Just use two different ddev projects. This is the easiest technique if the code for the two projects isn't already a mono-repo and tightly coupled.
    2. Add a .ddev/nginx-site.conf to override the nginx configuration. (This is the "legacy" technique in docs). There is an example of the exact technique you want in https://github.com/drud/ddev/issues/1889#issuecomment-543306759 - There @jcrichto adds an additional http server to the default configuration like this (but read the entire issue):
    #######################################################
    # EXAMPLE SITE ADDED TO DEFAULT NGINX
    # THIS ASSUMES server_name: matches a name added to 
    # additional_hostnames: in your config.yaml
    # 
    # CANADA - EN
    #######################################################
    server {
        listen 80;
    
        server_name ca.example.com.ddev.site;
    
        #redifne your root
        root /var/www/html/lang/en_ca;
    
        include /etc/nginx/nginx_default.conf;
    
        # this directive is unique to our set up, but demonstrates
        # you can do more directives per site like this
        location ~ \.php$ {
            root /var/www/html/lang/en_ca;
        }
    }
    

    The same issue is tackled the same way in https://github.com/drud/ddev/issues/1850#issuecomment-547461968 for more context.