{"title":"ミドルウェア","version":"2.2","locale":"ja","docname":"ref/middleware","url":"/ja/2.2/ref/middleware/","canonical":"https://djangodocs.dev/ja/2.2/ref/middleware/","summary":"This document explains all middleware components that come with Django. For information on how to use them and how to write your own middleware, see the middleware…","html":"<span id=\"middleware\"></span><h1>ミドルウェア<a class=\"heading-anchor\" href=\"#module-django.middleware\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<p>This document explains all middleware components that come with Django. For\ninformation on how to use them and how to write your own middleware, see\nthe <a class=\"reference internal\" href=\"/ja/2.2/topics/http/middleware/\"><span class=\"doc\">middleware usage guide</span></a>.</p>\n<section id=\"available-middleware\">\n<h2>使用できる middleware<a class=\"heading-anchor\" href=\"#available-middleware\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<section id=\"module-django.middleware.cache\">\n<span id=\"cache-middleware\"></span><h3>Cache middleware<a class=\"heading-anchor\" href=\"#module-django.middleware.cache\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.middleware.cache.UpdateCacheMiddleware\">\n<em class=\"property\"><span class=\"k\"><span class=\"pre\">class</span></span><span class=\"w\"> </span></em><span class=\"sig-name descname\"><span class=\"pre\">UpdateCacheMiddleware</span></span><a class=\"heading-anchor\" href=\"#django.middleware.cache.UpdateCacheMiddleware\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.middleware.cache.FetchFromCacheMiddleware\">\n<em class=\"property\"><span class=\"k\"><span class=\"pre\">class</span></span><span class=\"w\"> </span></em><span class=\"sig-name descname\"><span class=\"pre\">FetchFromCacheMiddleware</span></span><a class=\"heading-anchor\" href=\"#django.middleware.cache.FetchFromCacheMiddleware\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>サイト全体でキャッシュを有効にします。有効にすると、Django を利用している各ページは <a class=\"reference internal\" href=\"/ja/2.2/ref/settings/#std-setting-CACHE_MIDDLEWARE_SECONDS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">CACHE_MIDDLEWARE_SECONDS</span></code></a> 設定で指定した時間だけキャッシュされます。詳しくは the <a class=\"reference internal\" href=\"/ja/2.2/topics/cache/\"><span class=\"doc\">cache documentation</span></a> を読んでください。</p>\n</section>\n<section id=\"module-django.middleware.common\">\n<span id=\"common-middleware\"></span><h3>Common middleware<a class=\"heading-anchor\" href=\"#module-django.middleware.common\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.middleware.common.CommonMiddleware\">\n<em class=\"property\"><span class=\"k\"><span class=\"pre\">class</span></span><span class=\"w\"> </span></em><span class=\"sig-name descname\"><span class=\"pre\">CommonMiddleware</span></span><a class=\"heading-anchor\" href=\"#django.middleware.common.CommonMiddleware\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>完璧主義者のためにいくつかの便利な機能を提供します。</p>\n<ul>\n<li><p><a class=\"reference internal\" href=\"/ja/2.2/ref/settings/#std-setting-DISALLOWED_USER_AGENTS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">DISALLOWED_USER_AGENTS</span></code></a> 設定に含まれるユーザーエージェントのアクセスを禁止します。この設定は、コンパイルした正規表現オブジェクトのリストである必要があります。</p></li>\n<li><p><a class=\"reference internal\" href=\"/ja/2.2/ref/settings/#std-setting-APPEND_SLASH\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">APPEND_SLASH</span></code></a> および <a class=\"reference internal\" href=\"/ja/2.2/ref/settings/#std-setting-PREPEND_WWW\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">PREPEND_WWW</span></code></a> 設定に基づいて、URL の書き換えを実行します。</p>\n<p><a class=\"reference internal\" href=\"/ja/2.2/ref/settings/#std-setting-APPEND_SLASH\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">APPEND_SLASH</span></code></a> が <code class=\"docutils literal notranslate\"><span class=\"pre\">True</span></code> で、与えられた URL がスラッシュで終わっていなくて、さらに URLconf に URL が見つからなければ、Django は / が最後に追加された新しい URL を作り、この新しい URL へリクエストをリダイレクトします。それ以外のときには、最初の URL を通常通りに処理します。</p>\n<p>たとえば、 <code class=\"docutils literal notranslate\"><span class=\"pre\">foo.com/bar</span></code> は、 <code class=\"docutils literal notranslate\"><span class=\"pre\">foo.com/bar</span></code> に対する有効な URL パタンがなく、<code class=\"docutils literal notranslate\"><span class=\"pre\">foo.com/bar/</span></code> に対しては有効なパタンが <em>存在する</em> 場合に、 <code class=\"docutils literal notranslate\"><span class=\"pre\">foo.com/bar/</span></code> にリダイレクトされます。</p>\n<p><a class=\"reference internal\" href=\"/ja/2.2/ref/settings/#std-setting-PREPEND_WWW\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">PREPEND_WWW</span></code></a> を <code class=\"docutils literal notranslate\"><span class=\"pre\">True</span></code> にすると、先頭に &quot;www.&quot; がない URL は &quot;www.&quot; で始まる同じ URL にリダイレクトされます。</p>\n<p>2つのオプションが意味するのは、URL を正規化するということです。これは、1つの URL には1つの、かつただ1つだけの場所を指すべきだという考えです。技術的には URL <code class=\"docutils literal notranslate\"><span class=\"pre\">foo.com/bar</span></code> は <code class=\"docutils literal notranslate\"><span class=\"pre\">foo.com/bar/</span></code> とは区別されます。実際、サーチエンジンのインデクスはこれらを区別して作成されます。よって、URL を正規化するというのがベストプラクティスなのです。</p>\n</li>\n<li><p>Sets the <code class=\"docutils literal notranslate\"><span class=\"pre\">Content-Length</span></code> header for non-streaming responses.</p></li>\n</ul>\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.middleware.common.CommonMiddleware.response_redirect_class\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">CommonMiddleware.</span></span><span class=\"sig-name descname\"><span class=\"pre\">response_redirect_class</span></span><a class=\"heading-anchor\" href=\"#django.middleware.common.CommonMiddleware.response_redirect_class\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>Defaults to <a class=\"reference internal\" href=\"/ja/2.2/ref/request-response/#django.http.HttpResponsePermanentRedirect\" title=\"django.http.HttpResponsePermanentRedirect\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">HttpResponsePermanentRedirect</span></code></a>. Subclass\n<code class=\"docutils literal notranslate\"><span class=\"pre\">CommonMiddleware</span></code> and override the attribute to customize the redirects\nissued by the middleware.</p>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.middleware.common.BrokenLinkEmailsMiddleware\">\n<em class=\"property\"><span class=\"k\"><span class=\"pre\">class</span></span><span class=\"w\"> </span></em><span class=\"sig-name descname\"><span class=\"pre\">BrokenLinkEmailsMiddleware</span></span><a class=\"heading-anchor\" href=\"#django.middleware.common.BrokenLinkEmailsMiddleware\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<ul class=\"simple\">\n<li><p>Sends broken link notification emails to <a class=\"reference internal\" href=\"/ja/2.2/ref/settings/#std-setting-MANAGERS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">MANAGERS</span></code></a> (see\n<a class=\"reference internal\" href=\"/ja/2.2/howto/error-reporting/\"><span class=\"doc\">エラーのレポート</span></a>).</p></li>\n</ul>\n</section>\n<section id=\"module-django.middleware.gzip\">\n<span id=\"gzip-middleware\"></span><h3>GZip middleware<a class=\"heading-anchor\" href=\"#module-django.middleware.gzip\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.middleware.gzip.GZipMiddleware\">\n<em class=\"property\"><span class=\"k\"><span class=\"pre\">class</span></span><span class=\"w\"> </span></em><span class=\"sig-name descname\"><span class=\"pre\">GZipMiddleware</span></span><a class=\"heading-anchor\" href=\"#django.middleware.gzip.GZipMiddleware\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<aside class=\"admonition admonition-warning\" role=\"note\">\n<p class=\"admonition-title\">警告</p>\n<p>Security researchers recently revealed that when compression techniques\n(including <code class=\"docutils literal notranslate\"><span class=\"pre\">GZipMiddleware</span></code>) are used on a website, the site may become\nexposed to a number of possible attacks. Before using <code class=\"docutils literal notranslate\"><span class=\"pre\">GZipMiddleware</span></code> on\nyour site, you should consider very carefully whether you are subject to\nthese attacks. If you're in <em>any</em> doubt about whether you're affected, you\nshould avoid using <code class=\"docutils literal notranslate\"><span class=\"pre\">GZipMiddleware</span></code>. For more details, see the <a class=\"reference external\" href=\"http://breachattack.com/resources/BREACH%20-%20SSL,%20gone%20in%2030%20seconds.pdf\">the BREACH\npaper (PDF)</a> and <a class=\"reference external\" href=\"http://breachattack.com\">breachattack.com</a>.</p>\n</aside>\n<p>The <code class=\"docutils literal notranslate\"><span class=\"pre\">django.middleware.gzip.GZipMiddleware</span></code> compresses content for browsers\nthat understand GZip compression (all modern browsers).</p>\n<p>This middleware should be placed before any other middleware that need to\nread or write the response body so that compression happens afterward.</p>\n<p>It will NOT compress content if any of the following are true:</p>\n<ul class=\"simple\">\n<li><p>The content body is less than 200 bytes long.</p></li>\n<li><p>The response has already set the <code class=\"docutils literal notranslate\"><span class=\"pre\">Content-Encoding</span></code> header.</p></li>\n<li><p>The request (the browser) hasn't sent an <code class=\"docutils literal notranslate\"><span class=\"pre\">Accept-Encoding</span></code> header\ncontaining <code class=\"docutils literal notranslate\"><span class=\"pre\">gzip</span></code>.</p></li>\n</ul>\n<p>If the response has an <code class=\"docutils literal notranslate\"><span class=\"pre\">ETag</span></code> header, the ETag is made weak to comply with\n<span class=\"target\" id=\"index-0\"></span><a class=\"rfc reference external\" href=\"https://datatracker.ietf.org/doc/html/rfc7232.html#section-2.1\"><strong>RFC 7232 Section 2.1</strong></a>.</p>\n<p>You can apply GZip compression to individual views using the\n<a class=\"reference internal\" href=\"/ja/2.2/topics/http/decorators/#django.views.decorators.gzip.gzip_page\" title=\"django.views.decorators.gzip.gzip_page\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">gzip_page()</span></code></a> decorator.</p>\n</section>\n<section id=\"module-django.middleware.http\">\n<span id=\"conditional-get-middleware\"></span><h3>Conditional GET middleware<a class=\"heading-anchor\" href=\"#module-django.middleware.http\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.middleware.http.ConditionalGetMiddleware\">\n<em class=\"property\"><span class=\"k\"><span class=\"pre\">class</span></span><span class=\"w\"> </span></em><span class=\"sig-name descname\"><span class=\"pre\">ConditionalGetMiddleware</span></span><a class=\"heading-anchor\" href=\"#django.middleware.http.ConditionalGetMiddleware\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>Handles conditional GET operations. If the response doesn't have an <code class=\"docutils literal notranslate\"><span class=\"pre\">ETag</span></code>\nheader, the middleware adds one if needed. If the response has an <code class=\"docutils literal notranslate\"><span class=\"pre\">ETag</span></code> or\n<code class=\"docutils literal notranslate\"><span class=\"pre\">Last-Modified</span></code> header, and the request has <code class=\"docutils literal notranslate\"><span class=\"pre\">If-None-Match</span></code> or\n<code class=\"docutils literal notranslate\"><span class=\"pre\">If-Modified-Since</span></code>, the response is replaced by an\n<a class=\"reference internal\" href=\"/ja/2.2/ref/request-response/#django.http.HttpResponseNotModified\" title=\"django.http.HttpResponseNotModified\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">HttpResponseNotModified</span></code></a>.</p>\n</section>\n<section id=\"module-django.middleware.locale\">\n<span id=\"locale-middleware\"></span><h3>Locale ミドルウェア<a class=\"heading-anchor\" href=\"#module-django.middleware.locale\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.middleware.locale.LocaleMiddleware\">\n<em class=\"property\"><span class=\"k\"><span class=\"pre\">class</span></span><span class=\"w\"> </span></em><span class=\"sig-name descname\"><span class=\"pre\">LocaleMiddleware</span></span><a class=\"heading-anchor\" href=\"#django.middleware.locale.LocaleMiddleware\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>リクエストからのデータに基づいた言語セクションを有効化します。この機能は、それぞれのユーザに対してコンテンツをカスタマイズします。doc:国際化のドキュメント &lt;/topics/i18n/translation&gt; を参照してください。</p>\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.middleware.locale.LocaleMiddleware.response_redirect_class\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">LocaleMiddleware.</span></span><span class=\"sig-name descname\"><span class=\"pre\">response_redirect_class</span></span><a class=\"heading-anchor\" href=\"#django.middleware.locale.LocaleMiddleware.response_redirect_class\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>デフォルトは <a class=\"reference internal\" href=\"/ja/2.2/ref/request-response/#django.http.HttpResponseRedirect\" title=\"django.http.HttpResponseRedirect\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">HttpResponseRedirect</span></code></a> です。<code class=\"docutils literal notranslate\"><span class=\"pre\">LocaleMiddleware</span></code> をサブクラス化して属性をオーバーライドし、ミドルウェアによって発行されたリダイレクトをカスタマイズします。</p>\n</section>\n<section id=\"module-django.contrib.messages.middleware\">\n<span id=\"message-middleware\"></span><h3>Message ミドルウェア<a class=\"heading-anchor\" href=\"#module-django.contrib.messages.middleware\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.messages.middleware.MessageMiddleware\">\n<em class=\"property\"><span class=\"k\"><span class=\"pre\">class</span></span><span class=\"w\"> </span></em><span class=\"sig-name descname\"><span class=\"pre\">MessageMiddleware</span></span><a class=\"heading-anchor\" href=\"#django.contrib.messages.middleware.MessageMiddleware\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>クッキーおよびセッションをベースとしたメッセージサポートを有効化します。<a class=\"reference internal\" href=\"/ja/2.2/ref/contrib/messages/\"><span class=\"doc\">メッセージのドキュメント</span></a> を参照してください。</p>\n</section>\n<section id=\"module-django.middleware.security\">\n<span id=\"id1\"></span><span id=\"security-middleware\"></span><h3>Security middleware<a class=\"heading-anchor\" href=\"#module-django.middleware.security\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<aside class=\"admonition admonition-warning\" role=\"note\">\n<p class=\"admonition-title\">警告</p>\n<p>If your deployment situation allows, it's usually a good idea to have your\nfront-end Web server perform the functionality provided by the\n<code class=\"docutils literal notranslate\"><span class=\"pre\">SecurityMiddleware</span></code>. That way, if there are requests that aren't served\nby Django (such as static media or user-uploaded files), they will have\nthe same protections as requests to your Django application.</p>\n</aside>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.middleware.security.SecurityMiddleware\">\n<em class=\"property\"><span class=\"k\"><span class=\"pre\">class</span></span><span class=\"w\"> </span></em><span class=\"sig-name descname\"><span class=\"pre\">SecurityMiddleware</span></span><a class=\"heading-anchor\" href=\"#django.middleware.security.SecurityMiddleware\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>The <code class=\"docutils literal notranslate\"><span class=\"pre\">django.middleware.security.SecurityMiddleware</span></code> provides several security\nenhancements to the request/response cycle. Each one can be independently\nenabled or disabled with a setting.</p>\n<ul class=\"simple\">\n<li><p><a class=\"reference internal\" href=\"/ja/2.2/ref/settings/#std-setting-SECURE_BROWSER_XSS_FILTER\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">SECURE_BROWSER_XSS_FILTER</span></code></a></p></li>\n<li><p><a class=\"reference internal\" href=\"/ja/2.2/ref/settings/#std-setting-SECURE_CONTENT_TYPE_NOSNIFF\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">SECURE_CONTENT_TYPE_NOSNIFF</span></code></a></p></li>\n<li><p><a class=\"reference internal\" href=\"/ja/2.2/ref/settings/#std-setting-SECURE_HSTS_INCLUDE_SUBDOMAINS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">SECURE_HSTS_INCLUDE_SUBDOMAINS</span></code></a></p></li>\n<li><p><a class=\"reference internal\" href=\"/ja/2.2/ref/settings/#std-setting-SECURE_HSTS_PRELOAD\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">SECURE_HSTS_PRELOAD</span></code></a></p></li>\n<li><p><a class=\"reference internal\" href=\"/ja/2.2/ref/settings/#std-setting-SECURE_HSTS_SECONDS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">SECURE_HSTS_SECONDS</span></code></a></p></li>\n<li><p><a class=\"reference internal\" href=\"/ja/2.2/ref/settings/#std-setting-SECURE_REDIRECT_EXEMPT\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">SECURE_REDIRECT_EXEMPT</span></code></a></p></li>\n<li><p><a class=\"reference internal\" href=\"/ja/2.2/ref/settings/#std-setting-SECURE_SSL_HOST\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">SECURE_SSL_HOST</span></code></a></p></li>\n<li><p><a class=\"reference internal\" href=\"/ja/2.2/ref/settings/#std-setting-SECURE_SSL_REDIRECT\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">SECURE_SSL_REDIRECT</span></code></a></p></li>\n</ul>\n<section id=\"http-strict-transport-security\">\n<span id=\"id2\"></span><h4>HTTP Strict Transport Security<a class=\"heading-anchor\" href=\"#http-strict-transport-security\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>For sites that should only be accessed over HTTPS, you can instruct modern\nbrowsers to refuse to connect to your domain name via an insecure connection\n(for a given period of time) by setting the <a class=\"reference external\" href=\"https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Strict-Transport-Security\">&quot;Strict-Transport-Security&quot;\nheader</a>. This reduces your exposure to some SSL-stripping man-in-the-middle\n(MITM) attacks.</p>\n<p><code class=\"docutils literal notranslate\"><span class=\"pre\">SecurityMiddleware</span></code> will set this header for you on all HTTPS responses if\nyou set the <a class=\"reference internal\" href=\"/ja/2.2/ref/settings/#std-setting-SECURE_HSTS_SECONDS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">SECURE_HSTS_SECONDS</span></code></a> setting to a non-zero integer value.</p>\n<p>When enabling HSTS, it's a good idea to first use a small value for testing,\nfor example, <a class=\"reference internal\" href=\"/ja/2.2/ref/settings/#std-setting-SECURE_HSTS_SECONDS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">SECURE_HSTS_SECONDS</span> <span class=\"pre\">=</span> <span class=\"pre\">3600</span></code></a> for one\nhour. Each time a Web browser sees the HSTS header from your site, it will\nrefuse to communicate non-securely (using HTTP) with your domain for the given\nperiod of time. Once you confirm that all assets are served securely on your\nsite (i.e. HSTS didn't break anything), it's a good idea to increase this value\nso that infrequent visitors will be protected (31536000 seconds, i.e. 1 year,\nis common).</p>\n<p>Additionally, if you set the <a class=\"reference internal\" href=\"/ja/2.2/ref/settings/#std-setting-SECURE_HSTS_INCLUDE_SUBDOMAINS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">SECURE_HSTS_INCLUDE_SUBDOMAINS</span></code></a> setting\nto <code class=\"docutils literal notranslate\"><span class=\"pre\">True</span></code>, <code class=\"docutils literal notranslate\"><span class=\"pre\">SecurityMiddleware</span></code> will add the <code class=\"docutils literal notranslate\"><span class=\"pre\">includeSubDomains</span></code> directive\nto the <code class=\"docutils literal notranslate\"><span class=\"pre\">Strict-Transport-Security</span></code> header. This is recommended (assuming all\nsubdomains are served exclusively using HTTPS), otherwise your site may still\nbe vulnerable via an insecure connection to a subdomain.</p>\n<p>If you wish to submit your site to the <a class=\"reference external\" href=\"https://hstspreload.org/\">browser preload list</a>, set the\n<a class=\"reference internal\" href=\"/ja/2.2/ref/settings/#std-setting-SECURE_HSTS_PRELOAD\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">SECURE_HSTS_PRELOAD</span></code></a> setting to <code class=\"docutils literal notranslate\"><span class=\"pre\">True</span></code>. That appends the\n<code class=\"docutils literal notranslate\"><span class=\"pre\">preload</span></code> directive to the <code class=\"docutils literal notranslate\"><span class=\"pre\">Strict-Transport-Security</span></code> header.</p>\n<aside class=\"admonition admonition-warning\" role=\"note\">\n<p class=\"admonition-title\">警告</p>\n<p>The HSTS policy applies to your entire domain, not just the URL of the\nresponse that you set the header on. Therefore, you should only use it if\nyour entire domain is served via HTTPS only.</p>\n<p>Browsers properly respecting the HSTS header will refuse to allow users to\nbypass warnings and connect to a site with an expired, self-signed, or\notherwise invalid SSL certificate. If you use HSTS, make sure your\ncertificates are in good shape and stay that way!</p>\n</aside>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">注釈</p>\n<p>If you are deployed behind a load-balancer or reverse-proxy server, and the\n<code class=\"docutils literal notranslate\"><span class=\"pre\">Strict-Transport-Security</span></code> header is not being added to your responses,\nit may be because Django doesn't realize that it's on a secure connection;\nyou may need to set the <a class=\"reference internal\" href=\"/ja/2.2/ref/settings/#std-setting-SECURE_PROXY_SSL_HEADER\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">SECURE_PROXY_SSL_HEADER</span></code></a> setting.</p>\n</aside>\n</section>\n<section id=\"x-content-type-options-nosniff\">\n<span id=\"x-content-type-options\"></span><h4><code class=\"docutils literal notranslate\"><span class=\"pre\">X-Content-Type-Options:</span> <span class=\"pre\">nosniff</span></code><a class=\"heading-anchor\" href=\"#x-content-type-options-nosniff\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>Some browsers will try to guess the content types of the assets that they\nfetch, overriding the <code class=\"docutils literal notranslate\"><span class=\"pre\">Content-Type</span></code> header. While this can help display\nsites with improperly configured servers, it can also pose a security\nrisk.</p>\n<p>If your site serves user-uploaded files, a malicious user could upload a\nspecially-crafted file that would be interpreted as HTML or JavaScript by\nthe browser when you expected it to be something harmless.</p>\n<p>To prevent the browser from guessing the content type and force it to\nalways use the type provided in the <code class=\"docutils literal notranslate\"><span class=\"pre\">Content-Type</span></code> header, you can pass\nthe <a class=\"reference external\" href=\"https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Content-Type-Options\">X-Content-Type-Options: nosniff</a> header.  <code class=\"docutils literal notranslate\"><span class=\"pre\">SecurityMiddleware</span></code> will\ndo this for all responses if the <a class=\"reference internal\" href=\"/ja/2.2/ref/settings/#std-setting-SECURE_CONTENT_TYPE_NOSNIFF\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">SECURE_CONTENT_TYPE_NOSNIFF</span></code></a> setting\nis <code class=\"docutils literal notranslate\"><span class=\"pre\">True</span></code>.</p>\n<p>Note that in most deployment situations where Django isn't involved in serving\nuser-uploaded files, this setting won't help you. For example, if your\n<a class=\"reference internal\" href=\"/ja/2.2/ref/settings/#std-setting-MEDIA_URL\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">MEDIA_URL</span></code></a> is served directly by your front-end Web server (nginx,\nApache, etc.) then you'd want to set this header there. On the other hand, if\nyou are using Django to do something like require authorization in order to\ndownload files and you cannot set the header using your Web server, this\nsetting will be useful.</p>\n</section>\n<section id=\"x-xss-protection-1-mode-block\">\n<span id=\"x-xss-protection\"></span><h4><code class=\"docutils literal notranslate\"><span class=\"pre\">X-XSS-Protection:</span> <span class=\"pre\">1;</span> <span class=\"pre\">mode=block</span></code><a class=\"heading-anchor\" href=\"#x-xss-protection-1-mode-block\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>Some browsers have the ability to block content that appears to be an <a class=\"reference external\" href=\"https://en.wikipedia.org/wiki/Cross-site_scripting\">XSS\nattack</a>. They work by looking for JavaScript content in the GET or POST\nparameters of a page. If the JavaScript is replayed in the server's response,\nthe page is blocked from rendering and an error page is shown instead.</p>\n<p>The <a class=\"reference external\" href=\"https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-XSS-Protection\">X-XSS-Protection header</a> is used to control the operation of the\nXSS filter.</p>\n<p>To enable the XSS filter in the browser, and force it to always block\nsuspected XSS attacks, you can pass the <code class=\"docutils literal notranslate\"><span class=\"pre\">X-XSS-Protection:</span> <span class=\"pre\">1;</span> <span class=\"pre\">mode=block</span></code>\nheader. <code class=\"docutils literal notranslate\"><span class=\"pre\">SecurityMiddleware</span></code> will do this for all responses if the\n<a class=\"reference internal\" href=\"/ja/2.2/ref/settings/#std-setting-SECURE_BROWSER_XSS_FILTER\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">SECURE_BROWSER_XSS_FILTER</span></code></a> setting is <code class=\"docutils literal notranslate\"><span class=\"pre\">True</span></code>.</p>\n<aside class=\"admonition admonition-warning\" role=\"note\">\n<p class=\"admonition-title\">警告</p>\n<p>The browser XSS filter is a useful defense measure, but must not be\nrelied upon exclusively. It cannot detect all XSS attacks and not all\nbrowsers support the header. Ensure you are still <a class=\"reference internal\" href=\"/ja/2.2/topics/security/#cross-site-scripting\"><span class=\"std std-ref\">validating and\nsanitizing</span></a> all input to prevent XSS attacks.</p>\n</aside>\n</section>\n<section id=\"ssl-redirect\">\n<span id=\"id6\"></span><h4>SSL リダイレクト<a class=\"heading-anchor\" href=\"#ssl-redirect\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>サイトが HTTP と HTTPS 接続の両方をサポートしている場合、多くのユーザーはデフォルトでセキュアでない接続を行ってしまいます。最善のセキュリティのためには、すべての HTTP 接続を HTTPS 接続にリダイレクトするべきです。</p>\n<p><a class=\"reference internal\" href=\"/ja/2.2/ref/settings/#std-setting-SECURE_SSL_REDIRECT\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">SECURE_SSL_REDIRECT</span></code></a> 設定を True に設定すれば、 <code class=\"docutils literal notranslate\"><span class=\"pre\">SecurityMiddleware</span></code> が すべての HTTP 接続を HTTPS に parmanent (HTTP 301) にリダイレクトしてくれます。</p>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">注釈</p>\n<p>パフォーマンス上の理由により、このようなリダイレクトは Django の外部、フロントエンドのロードバランサーや <a class=\"reference external\" href=\"https://nginx.org\">nginx</a> などのリバースプロキシサーバーで実行した方が良いです。 <a class=\"reference internal\" href=\"/ja/2.2/ref/settings/#std-setting-SECURE_SSL_REDIRECT\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">SECURE_SSL_REDIRECT</span></code></a> は、これらのオプションが使用できないデプロイ環境で使われることを想定しています。</p>\n</aside>\n<p>If the <a class=\"reference internal\" href=\"/ja/2.2/ref/settings/#std-setting-SECURE_SSL_HOST\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">SECURE_SSL_HOST</span></code></a> setting has a value, all redirects will be\nsent to that host instead of the originally-requested host.</p>\n<p>If there are a few pages on your site that should be available over HTTP, and\nnot redirected to HTTPS, you can list regular expressions to match those URLs\nin the <a class=\"reference internal\" href=\"/ja/2.2/ref/settings/#std-setting-SECURE_REDIRECT_EXEMPT\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">SECURE_REDIRECT_EXEMPT</span></code></a> setting.</p>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">注釈</p>\n<p>If you are deployed behind a load-balancer or reverse-proxy server and\nDjango can't seem to tell when a request actually is already secure, you\nmay need to set the <a class=\"reference internal\" href=\"/ja/2.2/ref/settings/#std-setting-SECURE_PROXY_SSL_HEADER\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">SECURE_PROXY_SSL_HEADER</span></code></a> setting.</p>\n</aside>\n</section>\n</section>\n<section id=\"module-django.contrib.sessions.middleware\">\n<span id=\"session-middleware\"></span><h3>Session middleware<a class=\"heading-anchor\" href=\"#module-django.contrib.sessions.middleware\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.sessions.middleware.SessionMiddleware\">\n<em class=\"property\"><span class=\"k\"><span class=\"pre\">class</span></span><span class=\"w\"> </span></em><span class=\"sig-name descname\"><span class=\"pre\">SessionMiddleware</span></span><a class=\"heading-anchor\" href=\"#django.contrib.sessions.middleware.SessionMiddleware\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>セッションのサポートを有効にします。詳しくは <a class=\"reference internal\" href=\"/ja/2.2/topics/http/sessions/\"><span class=\"doc\">セッションのドキュメント</span></a> を読んでください。</p>\n</section>\n<section id=\"module-django.contrib.sites.middleware\">\n<span id=\"site-middleware\"></span><h3>Site middleware<a class=\"heading-anchor\" href=\"#module-django.contrib.sites.middleware\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.sites.middleware.CurrentSiteMiddleware\">\n<em class=\"property\"><span class=\"k\"><span class=\"pre\">class</span></span><span class=\"w\"> </span></em><span class=\"sig-name descname\"><span class=\"pre\">CurrentSiteMiddleware</span></span><a class=\"heading-anchor\" href=\"#django.contrib.sites.middleware.CurrentSiteMiddleware\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>受信側のすべての <code class=\"docutils literal notranslate\"><span class=\"pre\">HttpRequest</span></code> オブジェクトに、現在のサイトを表す <code class=\"docutils literal notranslate\"><span class=\"pre\">site</span></code> 属性を追加します。詳しくは <a class=\"reference internal\" href=\"/ja/2.2/ref/contrib/sites/#site-middleware\"><span class=\"std std-ref\">sites documentation</span></a> を読んでください。</p>\n</section>\n<section id=\"module-django.contrib.auth.middleware\">\n<span id=\"authentication-middleware\"></span><h3>Authentication middleware<a class=\"heading-anchor\" href=\"#module-django.contrib.auth.middleware\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.auth.middleware.AuthenticationMiddleware\">\n<em class=\"property\"><span class=\"k\"><span class=\"pre\">class</span></span><span class=\"w\"> </span></em><span class=\"sig-name descname\"><span class=\"pre\">AuthenticationMiddleware</span></span><a class=\"heading-anchor\" href=\"#django.contrib.auth.middleware.AuthenticationMiddleware\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>受信側のすべての <code class=\"docutils literal notranslate\"><span class=\"pre\">HttpRequest</span></code> オブジェクトに、現在のログイン中のユーザを表す <code class=\"docutils literal notranslate\"><span class=\"pre\">user</span></code> 属性を追加します。詳しくは <a class=\"reference internal\" href=\"/ja/2.2/topics/auth/default/#auth-web-requests\"><span class=\"std std-ref\">Authentication in Web requests</span></a> を読んでください。</p>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.auth.middleware.RemoteUserMiddleware\">\n<em class=\"property\"><span class=\"k\"><span class=\"pre\">class</span></span><span class=\"w\"> </span></em><span class=\"sig-name descname\"><span class=\"pre\">RemoteUserMiddleware</span></span><a class=\"heading-anchor\" href=\"#django.contrib.auth.middleware.RemoteUserMiddleware\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>Web サーバが提供する認証機能を利用するミドルウェアです。詳しい使い方については <a class=\"reference internal\" href=\"/ja/2.2/howto/auth-remote-user/\"><span class=\"doc\">REMOTE_USER を使用した認証</span></a> を読んでください。</p>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.auth.middleware.PersistentRemoteUserMiddleware\">\n<em class=\"property\"><span class=\"k\"><span class=\"pre\">class</span></span><span class=\"w\"> </span></em><span class=\"sig-name descname\"><span class=\"pre\">PersistentRemoteUserMiddleware</span></span><a class=\"heading-anchor\" href=\"#django.contrib.auth.middleware.PersistentRemoteUserMiddleware\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>ログインページでのみ有効になる、Web サーバが提供する認証機能を利用するミドルウェアです。詳しくい使い方については <a class=\"reference internal\" href=\"/ja/2.2/howto/auth-remote-user/#persistent-remote-user-middleware-howto\"><span class=\"std std-ref\">REMOTE_USERログインページのみの使用</span></a> を読んでください。</p>\n</section>\n<section id=\"csrf-protection-middleware\">\n<h3>CSRF プロテクション middleware<a class=\"heading-anchor\" href=\"#csrf-protection-middleware\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.middleware.csrf.CsrfViewMiddleware\">\n<em class=\"property\"><span class=\"k\"><span class=\"pre\">class</span></span><span class=\"w\"> </span></em><span class=\"sig-name descname\"><span class=\"pre\">CsrfViewMiddleware</span></span><a class=\"heading-anchor\" href=\"#django.middleware.csrf.CsrfViewMiddleware\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>POST フォームに隠しフォームフィールドを追加し、リクエストの値が正しいかチェックすることで、Cross Site Request Forgery に対するプロテクションを追加します。詳しくは <a class=\"reference internal\" href=\"/ja/2.2/ref/csrf/\"><span class=\"doc\">Cross Site Request Forgery プロテクションのドキュメント</span></a> を読んでください。</p>\n</section>\n<section id=\"x-frame-options-middleware\">\n<h3><code class=\"docutils literal notranslate\"><span class=\"pre\">X-Frame-Options</span></code> middleware<a class=\"heading-anchor\" href=\"#x-frame-options-middleware\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.middleware.clickjacking.XFrameOptionsMiddleware\">\n<em class=\"property\"><span class=\"k\"><span class=\"pre\">class</span></span><span class=\"w\"> </span></em><span class=\"sig-name descname\"><span class=\"pre\">XFrameOptionsMiddleware</span></span><a class=\"heading-anchor\" href=\"#django.middleware.clickjacking.XFrameOptionsMiddleware\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>シンプルな <a class=\"reference internal\" href=\"/ja/2.2/ref/clickjacking/\"><span class=\"doc\">クリックジャッキングに対する X-Frame-Options ヘッダ経由のプロテクション</span></a> です。</p>\n</section>\n</section>\n<section id=\"middleware-ordering\">\n<span id=\"id7\"></span><h2>Middleware の順序<a class=\"heading-anchor\" href=\"#middleware-ordering\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Django の多様なミドルウェアクラスの順序に関する注意点を挙げておきます。</p>\n<ol class=\"arabic\">\n<li><p><a class=\"reference internal\" href=\"#django.middleware.security.SecurityMiddleware\" title=\"django.middleware.security.SecurityMiddleware\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">SecurityMiddleware</span></code></a></p>\n<p>SSL リダイレクトを有効にしているなら、他のたくさんの必要のないミドルウェアが実行されないように、リストの先頭付近に置くべきです。</p>\n</li>\n<li><p><a class=\"reference internal\" href=\"#django.middleware.cache.UpdateCacheMiddleware\" title=\"django.middleware.cache.UpdateCacheMiddleware\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">UpdateCacheMiddleware</span></code></a></p>\n<p><code class=\"docutils literal notranslate\"><span class=\"pre\">Vary</span></code> ヘッダに変更を加えるミドルウェア (<code class=\"docutils literal notranslate\"><span class=\"pre\">SessionMiddleware</span></code>, <code class=\"docutils literal notranslate\"><span class=\"pre\">GZipMiddleware</span></code>, <code class=\"docutils literal notranslate\"><span class=\"pre\">LocaleMiddleware</span></code>) の前に置きます。</p>\n</li>\n<li><p><a class=\"reference internal\" href=\"#django.middleware.gzip.GZipMiddleware\" title=\"django.middleware.gzip.GZipMiddleware\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">GZipMiddleware</span></code></a></p>\n<p>response body を変更・使用する可能性のあるミドルウェアの前に置きます。</p>\n<p><code class=\"docutils literal notranslate\"><span class=\"pre\">Vary</span></code> ヘッダを修正するため、 <code class=\"docutils literal notranslate\"><span class=\"pre\">UpdateCacheMiddleware</span></code> の後に置きます。</p>\n</li>\n<li><p><a class=\"reference internal\" href=\"#django.contrib.sessions.middleware.SessionMiddleware\" title=\"django.contrib.sessions.middleware.SessionMiddleware\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">SessionMiddleware</span></code></a></p>\n<p>Before any middleware that may raise an an exception to trigger an error\nview (such as <a class=\"reference internal\" href=\"/ja/2.2/ref/exceptions/#django.core.exceptions.PermissionDenied\" title=\"django.core.exceptions.PermissionDenied\"><code class=\"xref py py-exc docutils literal notranslate\"><span class=\"pre\">PermissionDenied</span></code></a>) if you're\nusing <a class=\"reference internal\" href=\"/ja/2.2/ref/settings/#std-setting-CSRF_USE_SESSIONS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">CSRF_USE_SESSIONS</span></code></a>.</p>\n<p><code class=\"docutils literal notranslate\"><span class=\"pre\">Vary</span></code> ヘッダを修正するため、 <code class=\"docutils literal notranslate\"><span class=\"pre\">UpdateCacheMiddleware</span></code> の後に置きます。</p>\n</li>\n<li><p><a class=\"reference internal\" href=\"#django.middleware.http.ConditionalGetMiddleware\" title=\"django.middleware.http.ConditionalGetMiddleware\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">ConditionalGetMiddleware</span></code></a></p>\n<p>Before any middleware that may change the response (it sets the <code class=\"docutils literal notranslate\"><span class=\"pre\">ETag</span></code>\nheader).</p>\n<p>gzip されたコンテンツに対して <code class=\"docutils literal notranslate\"><span class=\"pre\">ETag</span></code> ヘッダを計算しないように、 <code class=\"docutils literal notranslate\"><span class=\"pre\">GZipMiddleware</span></code> の後に置きます。</p>\n</li>\n<li><p><a class=\"reference internal\" href=\"#django.middleware.locale.LocaleMiddleware\" title=\"django.middleware.locale.LocaleMiddleware\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">LocaleMiddleware</span></code></a></p>\n<p><code class=\"docutils literal notranslate\"><span class=\"pre\">SessionMiddleware</span></code> (session データを使う) と <code class=\"docutils literal notranslate\"><span class=\"pre\">UpdateCacheMiddleware</span></code> (<code class=\"docutils literal notranslate\"><span class=\"pre\">Vary</span></code> ヘッダを修正する) の後のできるだけ高い位置に置きます。</p>\n</li>\n<li><p><a class=\"reference internal\" href=\"#django.middleware.common.CommonMiddleware\" title=\"django.middleware.common.CommonMiddleware\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">CommonMiddleware</span></code></a></p>\n<p>Before any middleware that may change the response (it sets the\n<code class=\"docutils literal notranslate\"><span class=\"pre\">Content-Length</span></code> header). A middleware that appears before\n<code class=\"docutils literal notranslate\"><span class=\"pre\">CommonMiddleware</span></code> and changes the response must reset <code class=\"docutils literal notranslate\"><span class=\"pre\">Content-Length</span></code>.</p>\n<p><a class=\"reference internal\" href=\"/ja/2.2/ref/settings/#std-setting-APPEND_SLASH\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">APPEND_SLASH</span></code></a> か <a class=\"reference internal\" href=\"/ja/2.2/ref/settings/#std-setting-PREPEND_WWW\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">PREPEND_WWW</span></code></a> が <code class=\"docutils literal notranslate\"><span class=\"pre\">True</span></code> に設定されているとリダイレクトされるので、先頭の近くに置きます。</p>\n<p>After <code class=\"docutils literal notranslate\"><span class=\"pre\">SessionMiddleware</span></code> if you're using <a class=\"reference internal\" href=\"/ja/2.2/ref/settings/#std-setting-CSRF_USE_SESSIONS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">CSRF_USE_SESSIONS</span></code></a>.</p>\n</li>\n<li><p><a class=\"reference internal\" href=\"#django.middleware.csrf.CsrfViewMiddleware\" title=\"django.middleware.csrf.CsrfViewMiddleware\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">CsrfViewMiddleware</span></code></a></p>\n<p>CSRF 攻撃が可能なすべての view ミドルウェアの前に置きます。</p>\n<p>Before <a class=\"reference internal\" href=\"#django.contrib.auth.middleware.RemoteUserMiddleware\" title=\"django.contrib.auth.middleware.RemoteUserMiddleware\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">RemoteUserMiddleware</span></code></a>, or any\nother authentication middleware that may perform a login, and hence rotate\nthe CSRF token, before calling down the middleware chain.</p>\n<p>After <code class=\"docutils literal notranslate\"><span class=\"pre\">SessionMiddleware</span></code> if you're using <a class=\"reference internal\" href=\"/ja/2.2/ref/settings/#std-setting-CSRF_USE_SESSIONS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">CSRF_USE_SESSIONS</span></code></a>.</p>\n</li>\n<li><p><a class=\"reference internal\" href=\"#django.contrib.auth.middleware.AuthenticationMiddleware\" title=\"django.contrib.auth.middleware.AuthenticationMiddleware\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">AuthenticationMiddleware</span></code></a></p>\n<p>session ストレージを使うので、 <code class=\"docutils literal notranslate\"><span class=\"pre\">SessionMiddleware</span></code> の後に置きます。</p>\n</li>\n<li><p><a class=\"reference internal\" href=\"#django.contrib.messages.middleware.MessageMiddleware\" title=\"django.contrib.messages.middleware.MessageMiddleware\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">MessageMiddleware</span></code></a></p>\n<p>After <code class=\"docutils literal notranslate\"><span class=\"pre\">SessionMiddleware</span></code>: can use session-based storage.</p>\n</li>\n<li><p><a class=\"reference internal\" href=\"#django.middleware.cache.FetchFromCacheMiddleware\" title=\"django.middleware.cache.FetchFromCacheMiddleware\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">FetchFromCacheMiddleware</span></code></a></p>\n<p>キャッシュのハッシュキーを生成するのに <code class=\"docutils literal notranslate\"><span class=\"pre\">Vary</span></code> ヘッダを使用するため、このヘッダを修正するすべてのミドルウェアのあとに置きます。</p>\n</li>\n<li><p><a class=\"reference internal\" href=\"/ja/2.2/ref/contrib/flatpages/#django.contrib.flatpages.middleware.FlatpageFallbackMiddleware\" title=\"django.contrib.flatpages.middleware.FlatpageFallbackMiddleware\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">FlatpageFallbackMiddleware</span></code></a></p>\n<p>最後に実行されるタイプのミドルウェアなので、できるだけ下に置く必要があります。</p>\n</li>\n<li><p><a class=\"reference internal\" href=\"/ja/2.2/ref/contrib/redirects/#django.contrib.redirects.middleware.RedirectFallbackMiddleware\" title=\"django.contrib.redirects.middleware.RedirectFallbackMiddleware\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">RedirectFallbackMiddleware</span></code></a></p>\n<p>最後に実行されるタイプのミドルウェアなので、できるだけ下に置く必要があります。</p>\n</li>\n</ol>\n</section>","rootId":"module-django.middleware","toc":[{"title":"使用できる middleware","anchor":"available-middleware","children":[{"title":"Cache middleware","anchor":"module-django.middleware.cache","children":[]},{"title":"Common middleware","anchor":"module-django.middleware.common","children":[]},{"title":"GZip middleware","anchor":"module-django.middleware.gzip","children":[]},{"title":"Conditional GET middleware","anchor":"module-django.middleware.http","children":[]},{"title":"Locale ミドルウェア","anchor":"module-django.middleware.locale","children":[]},{"title":"Message ミドルウェア","anchor":"module-django.contrib.messages.middleware","children":[]},{"title":"Security middleware","anchor":"module-django.middleware.security","children":[{"title":"HTTP Strict Transport Security","anchor":"http-strict-transport-security","children":[]},{"title":"X-Content-Type-Options: nosniff","anchor":"x-content-type-options-nosniff","children":[]},{"title":"X-XSS-Protection: 1; mode=block","anchor":"x-xss-protection-1-mode-block","children":[]},{"title":"SSL リダイレクト","anchor":"ssl-redirect","children":[]}]},{"title":"Session middleware","anchor":"module-django.contrib.sessions.middleware","children":[]},{"title":"Site middleware","anchor":"module-django.contrib.sites.middleware","children":[]},{"title":"Authentication middleware","anchor":"module-django.contrib.auth.middleware","children":[]},{"title":"CSRF プロテクション middleware","anchor":"csrf-protection-middleware","children":[]},{"title":"X-Frame-Options middleware","anchor":"x-frame-options-middleware","children":[]}]},{"title":"Middleware の順序","anchor":"middleware-ordering","children":[]}],"breadcrumbs":[{"docname":"ref/index","title":"API Reference","url":"/ja/2.2/ref/"}],"prev":{"docname":"ref/forms/validation","title":"フォームとフィールドの検証","url":"/ja/2.2/ref/forms/validation/"},"next":{"docname":"ref/migration-operations","title":"マイグレーション操作","url":"/ja/2.2/ref/migration-operations/"},"formats":{"html":"/ja/2.2/ref/middleware/","markdown":"/ja/2.2/ref/middleware.md","json":"/ja/2.2/ref/middleware.json"},"source":"https://github.com/django/django/blob/stable/2.2.x/docs/ref/middleware.txt","official":"https://docs.djangoproject.com/ja/2.2/ref/middleware/","inVersions":["6.1","6.0","5.2","5.1","5.0","4.2","4.1","4.0","3.2","3.1","3.0","2.2","2.1","2.0","1.11","1.10","1.9"],"inLocales":["en","zh-hans","fr","ja","id","pt-br","ko","es","el","pl"]}