Run Countly from subdirectory

Follow

Because of some reason, you might want to run your Countly instance from subdirectory, ie: yourdomain.com/countly. Surely this is possible after several configuration steps.

Let's start.

Step 1: Nginx Configuration

Find your nginx.conf file and create backup before changes. Nginx path is /etc/nginx for Ubuntu 18.04. You can find correct nginx path your Linux distribution from here.

Change your server section in nginx.conf file according below.

server {
  listen 80;
  listen [::]:80;
  server_name localhost;
  
  access_log off;
  
  # default: location = /i {
  # changed: location = /countly/i {
  # add your custom prefix here
  location = /countly/i {
    proxy_pass http://127.0.0.1:3001/i;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header Host $http_host;
  } 

  # default: location ~ ^/i($|/.*$) {
  # changed: location ~ ^/countly/i($|/.*$) {
  # add your custom prefix here
  location ~ ^/countly/i($|/.*$) {
    proxy_pass http://127.0.0.1:3001/i$1$is_args$args;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header Host $http_host;
  }

  # default: location = /countly/o {
  # changed: location = /o {
  # add your custom prefix here
  location = /countly/o {
    proxy_pass http://127.0.0.1:3001/o;
    proxy_set_header Host $http_host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Real-IP $remote_addr;
  }

  # default: location ~ /o($|/.*$) {
  # changed: location ~ /countly/o($|/.*$) {
  # add your custom prefix here
  location ~ /countly/o($|/.*$) {
    proxy_pass http://127.0.0.1:3001/o$1$is_args$args;
    proxy_set_header Host $http_host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Real-IP $remote_addr;
  }

  # default: location ~ /o($|/.*$) {
  # changed: location ~ /countly/o($|/.*$) {
  # add your custom prefix here
  location ~ ^/countly($|/.*$) {
    proxy_pass http://127.0.0.1:6001/countly$1;
    proxy_set_header Host $http_host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Real-IP $remote_addr;
  }
}

As you can see above, we've changed our proxy configuration for "/o", "/i" and "/" paths. Please be sure to you're changed your proxy_pass fields too for fields that has $is_args$args keywords.

SSL Configuration

If you have ssl section on your nginx file, you should do same configuration for ssl server section too.

Step 2: Change path variable from Countly config files

After nginx configuration step, we should change path variable from Countly config file too. You can find the file on /frontend/express/config.js path.

...
...
    web: {
        port: 6001,
        host: "localhost",
        use_intercom: true
    },
   // you can write any string to here.    
   path: "/countly",
   cdn: ""
};

You should change path variable in frontend/express/config.js file according above.

...
...
        /* GCM proxy server for push plugin
        push_proxy: {
            host: 'localhost',
            port: 8888
        } */
    },
    /**
     * Path to use for countly directory, empty path if installed at root of website
     * @type {string}
     */
    path: "/countly",
...
...

Do same change for api/config.js too.

Path customization

"/countly" is not mandatory, you can use any string for run Countly from subdirectory. You should use same path with nginx configuration file.

Step 3: Restart Nginx and Countly

After this changes, you should reload the nginx server. You can reload nginx server with command below.

nginx -s reload

And you should restart Countly too.

countly restart

Now you're ready to use Countly from yourdomain.com/countly.

Step 4: Make sure the all changes are working

If you follow correctly all steps are above you should able to access dashboard from yourdomain.com/countly url.

Also we should able to access read and write APIs too from /countly/i and /countly/o endpoints.

You should get this response when you try to go yourdomain.com/countly/o

{
	result: "Invalid path"
}

You should get this response whe you try to go yourdomain.com/countly/i

{
	result: "Missing parameter "app_key" or "device_id""
}

If everything are ok. You're ready to use Countly from subdirectory.

Looking for help?