# nginx in front of Mattermost, on port 8000. The official nginx image # fills in ${MM_PREAUTH_SECRET} from the environment when it starts. # Room for the long secret in the maps below. map_hash_bucket_size 128; upstream mattermost { server mattermost:8065; keepalive 32; } # Websockets carry Mattermost's live updates. map $http_upgrade $connection_upgrade { default upgrade; "" ""; } # Who may reach Mattermost: browsers signed in to exe.dev (the proxy sets # X-ExeDev-Email), and the desktop and mobile apps, which send the secret. # While the VM is private, every request has X-ExeDev-Email. map $http_x_exedev_email $mm_exe_user { "" 0; default 1; } map $http_x_mattermost_preauth_secret $mm_app { "${MM_PREAUTH_SECRET}" 1; default 0; } # Signed in to exe.dev but not to Mattermost: opening the site, its login # page, or an invite link signs in with exe.dev instead (see exe-login.py). map "$mm_exe_user:$cookie_MMAUTHTOKEN:$uri" $mm_exe_login { default ""; "1::/" /oauth/gitlab/login; "1::/login" /oauth/gitlab/login?redirect_to=$arg_redirect_to; "~^1::/signup_user_complete/?$" /oauth/gitlab/signup?id=$arg_id&t=$arg_t; } # Team Edition hides the GitLab button on its sign-in page; nginx edits the # client settings to show it, as "exe.dev". That needs them uncompressed. map $uri $mm_accept_encoding { /api/v4/config/client ""; default $http_accept_encoding; } server { listen 8000; client_max_body_size 100M; # Redirects stay on the address the browser used. absolute_redirect off; set $mm_allowed "$mm_exe_user$mm_app"; proxy_http_version 1.1; proxy_set_header Host $http_host; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection $connection_upgrade; proxy_set_header Accept-Encoding $mm_accept_encoding; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $http_x_forwarded_for; proxy_set_header X-Forwarded-Proto https; proxy_read_timeout 600s; # Incoming webhooks: the secret is in the URL. location /hooks/ { proxy_pass http://mattermost; } # Mobile push acknowledgements carry no secret. location = /api/v4/notifications/ack { proxy_pass http://mattermost; } # exe.dev sign-in. Only this page of exe-login is reachable from outside. location = /exe-login/authorize { proxy_pass http://exe-login:8080; } # Show the exe.dev button on Mattermost's sign-in page. location = /api/v4/config/client { if ($mm_allowed = "00") { add_header X-Reject-Reason pre-auth always; return 403; } sub_filter_types application/json; sub_filter '"EnableSignUpWithEmail":' '"EnableSignUpWithGitLab":"true","GitLabButtonText":"exe.dev","GitLabButtonColor":"#000000","EnableSignUpWithEmail":'; proxy_pass http://mattermost; } # The API and its websocket: apps without the secret are asked for it. location /api/ { if ($mm_allowed = "00") { add_header X-Reject-Reason pre-auth always; return 403; } proxy_pass http://mattermost; } # Pages: browsers without exe.dev login are sent to sign in. location / { if ($mm_allowed = "00") { return 302 /__exe.dev/login?redirect=$request_uri; } if ($mm_exe_login) { return 302 $mm_exe_login; } proxy_pass http://mattermost; } }