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
- npm
yarn install && yarn run build
npm install && npm 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
-
Add a
serverblock to your Nginx configuration. ReplaceYOUR_DOMAIN_NAMEwith 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;
}
} -
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.
-
In
app.conf, setbasePath:basePath = /casdoorCasdoor trims leading and trailing slashes and adds one leading slash, so
casdoor/works too. Like other options,basePathcan also be set as an environment variable. -
If you set
origin, include the path in it, for exampleorigin = https://YOUR_DOMAIN_NAME/casdoor. Casdoor usesoriginas it is. Whenoriginis empty, Casdoor adds the path to the URLs that it derives from the request host. -
Restart the backend.
-
Proxy the path to the backend in the
serverblock: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.
-
Reload Nginx.
-
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
serverblock and serve Casdoor over HTTPS. OAuth 2.0 and OpenID Connect (OIDC) clients expect an HTTPS issuer in production. - Set
origininapp.confto the public URL of Casdoor.