diff --git a/build/main.c b/build/main.c
index 50b7802525627d0ec7fd87552ef2f7d7e1810039..20d5aaccb4a5a10505dfc17db06c88bedc87eb8a 100644
--- a/build/main.c
+++ b/build/main.c
@@ -449,6 +449,10 @@ static void start_mongoose(int argc, char *argv[]) {
     free(options[i + 1]);
   }
 
+  // Change current working directory to document root. This way,
+  // scripts can use relative paths.
+  chdir(mg_get_option(server, "document_root"));
+
   // Add an ability to pass listening socket to mongoose
   {
     const char *env = getenv("MONGOOSE_LISTENING_SOCKET");
diff --git a/mongoose.c b/mongoose.c
index f5d99e31f61523164e5106d188bb5ba616e85a3b..ebbfd7d2bcba82673d56d670f907ad96ce3085db 100644
--- a/mongoose.c
+++ b/mongoose.c
@@ -2048,7 +2048,6 @@ static int mg_write_chunked(struct connection *conn, const char *buf, int len) {
   return n;
 }
 
-
 static void print_dir_entry(const struct dir_entry *de) {
   char size[64], mod[64], href[MAX_PATH_SIZE * 3], chunk[MAX_PATH_SIZE * 4];
   int64_t fsize = de->st.st_size;