Synology CardDAV Setup
Tags: synology, carddav
Published: 2020-12-20 00:00:00

---
title: "Synology CardDAV Setup"
date: "2020-12-20"
categories: ["synology", "home network"]
description: "Setting up a CardDAV server on my Synology NAS and connecting clients to it for a centralized solution to managing my contacts while maintaining ownership over my data"
---
## What is CardDAV
CardDAV is a simple protocol that operates over HTTP/HTTPS - in fact, it's built on top of the generic WebDAV protocol that is itself built on HTTP/HTTPS.  
It allows you to have a web server implementing the protocol to provide vCard contact info a standardized format. Having a centralized server of course means that you can sync that data elsewhere.
The standard was first proposed and made popular by Apple with iOS7, from there it was adopted or supported by more services making it a fairly ubiquitous service.

## Why do this?
I personally have a desire to own as much of my own data as possible, sometimes that's not easy (like running an email server), but this seemed relatively doable due to pre-built support and the network infrastructure I already have set up.

If I wanted to avoid going through the trouble of running my own server I could make use of the fact that Apple's contact management is already built on CardDAV and could sync my contacts through that account.

## Setting up CardDAV on Synology Server
Synology has a great package ecosystem for their devices, so this step was extremely simple - just open the package manager on the device and install the CardDAV Server package.

## Adding HTTPS by default
I have a .dev domain so everything must use HTTPS since that is a requirement set by Google as owner of the TLD, so updating this was as simple as checking the box in the Synology config to redirect all HTTP traffic to the HTTPS port.

## Updating proxy config
My home network and all the services running on it are set up on my .dev domain. 
Now isn't the time for details, but the gist is that I have a nginx web server sitting in front of all my services in order to access things by a clean URI (no ports). This also allows me to keep my SSL setup on one machine.

Since the CardDAV server is running on its own port on the Synology that will require setting it up as its own virtual host - paths are technically possible, but far too often services don't handle rewritten URL paths and won't function properly.

Adding the following (and replacing things in angle brackets with real endpoints) as a new enabled host, and restarting the server does the trick:
```
# Handle redirects to HTTPS coming through the web server
# Synology would also do this, but better to do it earlier in the stream
server {
        listen 80;
        listen [::]:80;

        server_name <new server name>;

        if ($host = <new server name>) {
                return 301 https://$host$request_uri;
        }
}

# Set up the proxy virtual host
server {
        listen 443 ssl;
        listen [::]:443 ssl;

        include <path to shared SSL config>;

        server_name <new server name>;

        location / {
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_pass https://<IP address of synology>:<HTTPS port of CardDAV server>/;
                proxy_redirect https://<IP address of synology>:<HTTPS port of CardDAV server> https://<new server name>;
        }
}
```

The last two steps to making this work are to make it accessible by the new host name on my internal network by adding a host record to my pihole (pointing it at the web server), and also a host record to the DNS config of my domain provider. I'm leaving this service open to the internet because the Synology has built in password security around accessing the service, and I want to make sure my contacts will sync up when I'm not on home wifi.

## Syncing to other devices
Now that the server is setup we'll want to make use of it to get the same contact list every where!

### iPhone
Since Apple's contact management is already built on top of vCards and CardDAV servers, it's a relatively simple process to point your phone to a personal server.

First step is to add the CardDAV account by going to Settings > Contacts > Add Account. From there you can fill out your server, user name, and password - if not going through standard HTTP/HTTPS ports like configured on the Synology device by default you can configure that under the advanced settings. You don't want to fill out the account URL because that will be populated by querying the server.

Second, since I've had my phone for a few years all of my contacts were stored locally, so I had to find a way to get them uploaded. To do this I made sure my contacts were synced to my iCloud account - then, once they were all uploaded exported the full list as a csv file. From there I was able to take the csv and upload it directly into the synology interface.

Next I set the default contact account on my phone as the account added by step 1 - and now everything should be synced through the personal server!

### Outlook
Outlook was a bit trickier to set up because it does not have built in support for CardDAV (Exchange is the most popularly connected service to outlook which manages contacts through ActiveSync protocols).
That being said, it's easy to get add ins that enabled syncing for CardDAV and CalDAV servers. I personally used [Open Protocols Connector](https://www.arpdev.com) as that just happened to be what turned up when searching for add-ins. But there are certainly [more out there](https://caldavsynchronizer.org).
The Open Protocols Connector allows free use, but the product still requires registration by getting a registration code from the site.

The add in adds a Server ribbon tab to the workspace, which has the 'Connection' button to configure server info.

I was unfortunately not able to directly set up the server as simply as the iPhone because it was not recognizing the root server as a CardDAV resource.
It turns out it wasn't asking for the generic server as the iPhone does in order to query for the account URL - it needs the actual account URL directly.
Since i had the iPhone setup already done, I was able to pull the account URL from those settings and copy that over the computer in order to get things synced up.

After getting that last piece setup, I saw my contacts start to flow in, and then tested creating a new one to make sure it was working in both directions. Voila! Shared contacts between my two primary locations for sending / receiving email!