Skip to main content

Deploy Casdoor behind Nginx

This guide explains how to run Casdoor from source in production mode and proxy the traffic of your domain to it with Nginx.


Learning outcomes​

  • Build the Casdoor frontend into static files.
  • Run the Casdoor backend, which serves both the API and the frontend.
  • Proxy your domain to the backend with Nginx.

What you need​

  • Casdoor cloned and connected to a database. See Install the Casdoor server.
  • Nginx on the same server
  • A domain name that points to the server

Build the frontend​

In the web directory of the Casdoor repository, build the static files:

yarn install && yarn run build

Run the backend​

In the repository root, run the backend. It serves the API and the built frontend on port 8000.

go run main.go

Alternatively, build a binary and run it:

go build && ./casdoor

Configure Nginx​

  1. Add a server block to your Nginx configuration. Replace YOUR_DOMAIN_NAME with your domain.

    server {
    listen 80;
    server_name YOUR_DOMAIN_NAME;
    location / {
    proxy_set_header Host $http_host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_redirect off;
    proxy_pass http://127.0.0.1:8000;
    }
    }
  2. Reload Nginx:

    nginx -s reload

Serve Casdoor under a path​

To share a domain with other applications, serve Casdoor under a path such as https://YOUR_DOMAIN_NAME/casdoor instead of at the root.

  1. In app.conf, set basePath:

    basePath = /casdoor

    Casdoor trims leading and trailing slashes and adds one leading slash, so casdoor/ works too. Like other options, basePath can also be set as an environment variable.

  2. If you set origin, include the path in it, for example origin = https://YOUR_DOMAIN_NAME/casdoor. Casdoor uses origin as it is. When origin is empty, Casdoor adds the path to the URLs that it derives from the request host.

  3. Restart the backend.

  4. Proxy the path to the backend in the server block:

    location /casdoor/ {
    proxy_set_header Host $http_host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_redirect off;
    proxy_pass http://127.0.0.1:8000;
    }

    Casdoor removes the path prefix before it routes a request, so the proxy may either keep the prefix, as in this example, or strip it.

  5. Reload Nginx.

  6. Update the URLs that point to Casdoor: the Casdoor URL in your applications and SDKs, and the callback URL that you register with each identity provider, which becomes https://YOUR_DOMAIN_NAME/casdoor/callback.

With basePath set, Casdoor writes the path into the index.html that it serves: it prefixes the root-relative script and stylesheet URLs and adds a <meta name="casdoor-base-path"> tag. The frontend reads that tag for its routes, API calls, and links. The OIDC discovery document, including the issuer, uses the path too.

Verify the result​

Open http://YOUR_DOMAIN_NAME in a browser, or http://YOUR_DOMAIN_NAME/casdoor if you serve Casdoor under a path. The Casdoor sign-in page appears.

Next steps​

  • Add a TLS certificate to the server block and serve Casdoor over HTTPS. OAuth 2.0 and OpenID Connect (OIDC) clients expect an HTTPS issuer in production.
  • Set origin in app.conf to the public URL of Casdoor.

See also​