2019-03-22 14:01:05 +00:00
|
|
|
# Nginx
|
|
|
|
|
|
|
|
[nginx] is the only official reverse proxy supported by **Authelia** for now.
|
|
|
|
|
|
|
|
## Configuration
|
|
|
|
|
|
|
|
Here is a commented example of configuration
|
|
|
|
|
|
|
|
server {
|
|
|
|
listen 443 ssl;
|
|
|
|
server_name myapp.example.com;
|
|
|
|
|
|
|
|
resolver 127.0.0.11 ipv6=off;
|
|
|
|
set $upstream_verify https://authelia.example.com/api/verify;
|
|
|
|
set $upstream_endpoint http://nginx-backend;
|
|
|
|
|
2019-04-24 21:52:08 +00:00
|
|
|
ssl_certificate /etc/ssl/server.cert;
|
2019-03-22 14:01:05 +00:00
|
|
|
ssl_certificate_key /etc/ssl/server.key;
|
|
|
|
|
|
|
|
# Use HSTS, please beware of what you're doing if you set it.
|
|
|
|
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
|
|
|
|
add_header X-Frame-Options "SAMEORIGIN";
|
|
|
|
|
|
|
|
location / {
|
|
|
|
# Send a subsequent request to Authelia to verify if the user is authenticated
|
|
|
|
# and has the right permissions to access the resource.
|
|
|
|
auth_request /auth_verify;
|
|
|
|
|
|
|
|
# Set the X-Forwarded-User and X-Forwarded-Groups with the headers
|
|
|
|
# returned by Authelia for the backends which can consume them.
|
|
|
|
# This is not safe, as the backend must make sure that they come from the
|
|
|
|
# proxy. In the future, it's gonna be safe to just use OAuth.
|
|
|
|
auth_request_set $user $upstream_http_remote_user;
|
|
|
|
proxy_set_header X-Forwarded-User $user;
|
|
|
|
|
|
|
|
auth_request_set $groups $upstream_http_remote_groups;
|
2019-04-16 00:45:37 +00:00
|
|
|
proxy_set_header X-Forwarded-Groups $groups;
|
2019-03-22 14:01:05 +00:00
|
|
|
|
2019-04-16 00:45:37 +00:00
|
|
|
# Set the `target_url` variable based on the request. It will be used to build the portal
|
|
|
|
# URL with the correct redirection parameter.
|
|
|
|
auth_request_set $target_url $scheme://$http_host$request_uri;
|
|
|
|
|
2019-03-22 14:01:05 +00:00
|
|
|
# If Authelia returns 401, then nginx redirects the user to the login portal.
|
|
|
|
# If it returns 200, then the request pass through to the backend.
|
|
|
|
# For other type of errors, nginx will handle them as usual.
|
2020-01-06 05:39:51 +00:00
|
|
|
error_page 401 =302 https://login.example.com:8080/?rd=$target_url;
|
2019-03-22 14:01:05 +00:00
|
|
|
|
|
|
|
proxy_pass $upstream_endpoint;
|
|
|
|
}
|
2019-03-26 20:00:47 +00:00
|
|
|
|
|
|
|
# Virtual endpoint created by nginx to forward auth requests.
|
|
|
|
location /auth_verify {
|
|
|
|
internal;
|
|
|
|
|
|
|
|
# [OPTIONAL] The IP of the client shown in Authelia logs.
|
|
|
|
proxy_set_header X-Real-IP $remote_addr;
|
|
|
|
|
2019-04-16 00:45:37 +00:00
|
|
|
# [REQUIRED] Needed by Authelia to check authorizations of the resource.
|
|
|
|
# Provide either X-Original-URL and X-Forwarded-Proto or
|
|
|
|
# X-Forwarded-Proto, X-Forwarded-Host and X-Forwarded-Uri or both.
|
|
|
|
# Those headers will be used by Authelia to deduce the target url of the user.
|
|
|
|
#
|
|
|
|
# X-Forwarded-Proto is mandatory since Authelia uses the "trust proxy" option.
|
|
|
|
# See https://expressjs.com/en/guide/behind-proxies.html
|
|
|
|
proxy_set_header X-Original-URL $scheme://$http_host$request_uri;
|
|
|
|
|
2019-03-26 20:00:47 +00:00
|
|
|
proxy_set_header X-Forwarded-Proto $scheme;
|
2019-04-16 00:45:37 +00:00
|
|
|
proxy_set_header X-Forwarded-Host $http_host;
|
|
|
|
proxy_set_header X-Forwarded-Uri $request_uri;
|
|
|
|
|
2019-03-26 20:00:47 +00:00
|
|
|
# [OPTIONAL] The list of IPs of client and proxies in the chain.
|
|
|
|
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
|
|
|
|
|
|
|
proxy_pass_request_body off;
|
|
|
|
proxy_set_header Content-Length "";
|
|
|
|
|
|
|
|
proxy_pass $upstream_verify;
|
|
|
|
}
|
2019-03-22 14:01:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-01-06 05:39:51 +00:00
|
|
|
[nginx]: https://www.nginx.com/
|