Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
nginx adding additional domains to the same container

Adding Additional domains to the same container in nginx

  1. Refer to these instructions for setting up the first domain.

  2. Go to the path where the original nginx file configuration resides cd /etc/nginx/sites-available

  3. Copy the existing configuration file to a new file in the same directory. In my example it is called kquinn.info. I like to call it the same name as the domain name, but I don't know if it's necessary. This is my file.

server {
        server_name kquinn.info www.kquinn.info;

        location / {
                proxy_pass http://127.0.0.1:5000; #whatever port your app runs on
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection 'upgrade';
                proxy_set_header Host $host;
                proxy_cache_bypass $http_upgrade;
        }

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/kquinn.info/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/kquinn.info/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}

server {
    if ($host = www.kquinn.info) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

    if ($host = kquinn.info) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

        server_name kquinn.info www.kquinn.info;
    listen 80;
    return 404; # managed by Certbot
}
  1. Make the new file as follows. In my example, I'm using port 5001 with www.ubci.info as the domain
server {
        server_name <b>kquinn.info www.kquinn.info;</b>

        location / {
                proxy_pass http://127.0.0.1:<b>5000;</b> #whatever port your app runs on
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection 'upgrade';
                proxy_set_header Host $host;
                proxy_cache_bypass $http_upgrade;
        }
}
  1. Follow step (10 Add SSL with LetsEncrypt) from the previous linked file. This should add the rest of the items to the configuration file.
  2. Create a link in the sites-available directory to this configuration file.
cd /etc/nginx/sites-enabled
ln -s ../sites-available/kquinn.info
  1. I don't know if this is necessary but you might have to stop and restart the nginx server
  2. Repeat step 6 from the link to Setup PM2 process manager
  3. You might have to reverse steps 5 and 6. Also, if you make a mistake you can always delete the new file and link that you created.

This is my first gist so if you see any issues, please let me know and I'll correct them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.