Invalid csrf token, have tried multiple solutions without success

Technical Support
  • I've recently been trying to setup NodeBB on my server, but whenever i try to login or register the POST request returns a 403 forbidden response and NodeBB logs an "invalid csrf token" error.

    The following software packages are used:

    • NodeBB 1.10.1
    • MongoDB 3.2.11
    • Node.js 8.11.4
    • Debian 9.3

    NodeBB itself is a subfolder installation in The node server is multi-tenant and works with express.js as a router/reverse proxy. All external http requests are redirected to https, but requests to the nodeBB server are proxied internally over http. Essentially this is the flow for an incoming forum request:

    • main server app -> enforce https, direct request to directory
    • host/domain app -> Check url, if /forum, proxy to port 4567 over http (or wss if websocket)
    • let nodeBB do its thing.

    I've been googling and have found quite a few threads, but none of the suggested solutions have worked. things i have tried so far:

    • Check url in config.json: ''
    • Make sure cookieDomain is '' in MongoDB
    • Including header 'X-Forwarded-Proto: https'
    • Including header 'X-Forwarded-SSL: on'
    • Including header 'X-Url-Scheme: https'
      I've also found that in the GET request for the login form, no X-CSRF-Token header is received. The form itself however is populated with a token.

    Lastly, here are the relevant code snippets:

    main server app

    #!/usr/bin/env nodejs
    // filename: app.js
    const http = require('http');
    const https = require('https');
    const fs = require('fs');
    const express = require('express');
    const vhost = require('vhost');
    const app = express();
    const sslOptions = {
            cert: fs.readFileSync('./.sslcert/fullchain.pem'),
            key: fs.readFileSync('./.sslcert/privkey.pem')
    http.createServer(function(req, res) {
            res.writeHead(301, { "Location": "https://" + req.headers['host'] + req.url });
    .use(express.static(__dirname + '/static'))
    <redacted, other domains>
    .use(vhost('', require('./apps/EndlessEndeavors').app))
    app.get('/', function (req, res) {
            res.writeHead(200, {'Content-Type': 'text/plain'});

    domain app

    const express = require('express')
    const app = express();
    const http = require('http');
    const httpProxy = require('http-proxy');
    const proxy = httpProxy.createProxyServer();
    app.use('/static',express.static(__dirname + '/static'));
    app.use('', express.static(__dirname + '/dist/EndlessEndeavors'));
    app.all('/forum[/]+*', function(req, res){
            console.log('got socket request');
            proxy.web(req, res, {target: 'wss://', ws: true});
        } else {
            proxy.web(req, res, {target: ''});
    app.all('/forum$', function(req, res) {
        res.writeHead(301, { "Location": "https://" + req.headers['host'] + '/forum/' });
    ) = app; 

    nodeBB config.json

        "url": "",
        "secret": "<redacted>",
        "database": "mongo",
        "port": 4567,
        "mongo": {
            "host": "",
            "port": 27017,
            "username": "<redacted>",
            "password": "<redacted>",
            "database": "<redacted>",
            "uri": "<redacted>"

    Hopefully this is enough into to help find the cause of the invalid csrf token issue... i have no idea what else could be wrong at this point.

Suggested Topics