diff --git a/app/views/errors/blocked_host.html.haml b/app/views/errors/blocked_host.html.haml
new file mode 100644
index 00000000000..698d4edb937
--- /dev/null
+++ b/app/views/errors/blocked_host.html.haml
@@ -0,0 +1,20 @@
+- content_for :page_title do
+ Blocked host:
+ = host
+
+- content_for :content do
+ :plain
+
Blocked host: #{host}
+
+
+ It appears you have tried accessing your Mastodon server using a different name (#{host}
) than defined in the LOCAL_DOMAIN
+ and WEB_DOMAIN
environment variables.
+
+
+
+ If that's not the case, check your reverse-proxy configuration to ensure it properly sets the Host
header.
+
+
+
+ If you want to allow requests to #{host}
without making it the primary domain name for your Mastodon instance, add it to the ALTERNATE_DOMAINS
environment variable.
+
diff --git a/app/views/layouts/anonymous_error.html.haml b/app/views/layouts/anonymous_error.html.haml
new file mode 100644
index 00000000000..2561dcb2f82
--- /dev/null
+++ b/app/views/layouts/anonymous_error.html.haml
@@ -0,0 +1,34 @@
+!!!
+%html{ lang: I18n.locale }
+ %head
+ %meta{ content: 'text/html; charset=UTF-8', 'http-equiv' => 'Content-Type' }/
+ %meta{ charset: 'utf-8' }/
+ %title= yield(:page_title)
+ %meta{ content: 'width=device-width,initial-scale=1', name: 'viewport' }/
+
+ :css
+ body.error {
+ position: absolute;
+ color: #9baec8;
+ background: #282c37;
+ width: 100%;
+ height: 100%;
+ padding: 0;
+ display: flex;
+ justify-content: center;
+ align-items: center;
+ margin: 0;
+ font-family: sans-serif;
+ font-size: 14px;
+ }
+
+ body.error .dialog h1 {
+ text-align: center;
+ font-size: 20px;
+ line-height: 28px;
+ font-weight: 400;
+ }
+
+ %body.error
+ .dialog
+ .dialog__message= yield :content
diff --git a/config/initializers/1_hosts.rb b/config/initializers/1_hosts.rb
index ee0d3ffdf6d..6f58901bab8 100644
--- a/config/initializers/1_hosts.rb
+++ b/config/initializers/1_hosts.rb
@@ -31,22 +31,15 @@ Rails.application.configure do
response_app = ->(env) do
request = ActionDispatch::Request.new(env)
- body = "Blocked host: #{request.host}. To allow requests to #{request.host}, add it to config.hosts.\n"
+ body = ApplicationController.renderer.render 'errors/blocked_host', layout: 'anonymous_error', locals: { host: request.host }, formats: [:html]
status = 403
headers = {
- 'Content-Type' => "text/plain; charset=#{ActionDispatch::Response.default_charset}",
+ 'Content-Type' => "text/html; charset=#{ActionDispatch::Response.default_charset}",
'Content-Length' => body.bytesize.to_s,
}
- if request.request_method == 'GET'
- status = 307
- headers['Location'] = "#{request.scheme}://#{web_host}#{request.fullpath}"
- end
-
- Rails.logger.warn("[HostAuthorization] Invalid host: #{request.host}.")
-
- [status, headers, [body]]
+ [403, headers, [body]]
end
config.hosts << host if host.present?