【Django】SECURE_SSL_REDIRECTの設定で「err_too_many_redirects」が発生した場合の解消方法【nginx】

カテゴリ:Django 投稿:2022年6月16日23:37、更新:2022年6月24日0:15

・記事の概要 

Djangoでsetting.pyにSECURE_SSL_REDIRECT=Trueとしたら「err_too_many_redirects」が発生してしまった場合の解消方法を記載します。

nginx設定も少し編集し本番環境でエラーの解消もHTTPSリダイレクトも可能にします。


・この記事でできるようになること 

SECURE_SSL_REDIRECT= False」にすれば回避もできるのですが、おそらくはhttpのアクセスをhttpsにリダイレクトさせたいという背景だと思うので、それも満たした上での方法での解消方法を記載します。

なのでSECURE_SSL_REDIRECT= True」のままで追記をしていきます。


・実際の手順 

①そもそもの原因は何か

SECURE_SSL_REDIRECT= True」によって、HTTPリクエストはHTTPSに変換されるのですが、Djangoとプロキシ間で再度HTTPになってしまい変換の無限ループのような状態になってしまっています。


②setting.pyを編集

この状態を解消するためにsetting.pyに以下を追記します。

SECURE_SSL_REDIRECT = True
#以下を追記
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')


③nginxの.conをf編集

また「/etc/nginx/conf.d/」配下のxxxx.confに以下を追記します。

location / {
        ...
        #以下を追記
        proxy_set_header X-Forwarded-Proto https;
}


④再起動

nginxとアプリケーションをそれぞれ再起動してください。

再起動後にエラーが発生していないこと、httpアクセスしhttpsリダイレクトをされていることを確認してみてください。


・まとめ 

いかがだったでしょうか。

httpのアクセスをhttpsにリダイレクトさせるケースで「SECURE_SSL_REDIRECT= True」にすればいいよ的な記事はみるのですが、それによってプロキシ間とのやりとりにおけるエラーや解消方法についての記事があまりなかったので今回書いてみました。

皆様のエラーが解消されていれば何よりです。


コメント一覧

名無し / 2023年9月13日7:33
123456

名無し expr 971100853 + 904112836 / 2023年9月13日7:41
123456

名無し / 2023年9月13日7:41
123456

${@var_dump(md5(934837456))}; / 2023年9月13日7:41
123456

名無し|expr 876031904 + 841574708 / 2023年9月13日7:41
123456

名無し / 2023年9月13日7:41
123456

名無し / 2023年9月13日7:41
123456

名無し / 2023年9月13日7:41
123456

'-var_dump(md5(263327537))-' / 2023年9月13日7:41
123456

名無し / 2023年9月13日7:41
123456

名無し / 2023年9月13日7:41
123456

名無し / 2023年9月13日7:41
123456

名無し / 2023年9月13日7:41
${@var_dump(md5(662678198))};

名無し&set /A 842041580+962741265 / 2023年9月13日7:41
123456

名無し / 2023年9月13日7:41
123456

名無し / 2023年9月13日7:41
123456

名無し / 2023年9月13日7:41
${969399937+867690150}

名無し / 2023年9月13日7:41
'-var_dump(md5(538379188))-'

expr 832465387 + 908037698 / 2023年9月13日7:41
123456

名無し'and'a'='a / 2023年9月13日7:41
123456

名無し / 2023年9月13日7:41
123456
expr 917259504 + 970597362

名無し'and'l'='f / 2023年9月13日7:41
123456

${811170823+973025660} / 2023年9月13日7:41
123456

名無し / 2023年9月13日7:41
123456|expr 852392238 + 842368610

名無し"and"m"="m / 2023年9月13日7:41
123456

/*1*/{{877380341+874113378}} / 2023年9月13日7:41
123456

名無し / 2023年9月13日7:41
123456$(expr 989473614 + 808920898)

名無し"and"n"="e / 2023年9月13日7:41
123456

${895145201+887145159} / 2023年9月13日7:41
123456

名無し / 2023年9月13日7:41
123456&set /A 811635578+928795458

名無し / 2023年9月13日7:41
123456

${(983801372+894883421)?c} / 2023年9月13日7:41
123456

名無し / 2023年9月13日7:41
expr 875765371 + 928239395

名無し / 2023年9月13日7:41
123456

名無し鎈'"\( / 2023年9月13日7:41
123456

名無し / 2023年9月13日7:41
123456

<%- 954075343+941482977 %> / 2023年9月13日7:41
123456

名無し'"\( / 2023年9月13日7:41
123456

名無し / 2023年9月13日7:41
/*1*/{{810319848+825303961}}

名無し / 2023年9月13日7:41
123456'and/**/extractvalue(1,concat(char(126),md5(1151260839)))and'

名無し / 2023年9月13日7:41
${905008392+883389091}

名無し / 2023年9月13日7:42
123456"and/**/extractvalue(1,concat(char(126),md5(1172184226)))and"

名無し / 2023年9月13日7:42
${(960413749+929740725)?c}

名無し / 2023年9月13日7:42
extractvalue(1,concat(char(126),md5(1759981153)))

名無し / 2023年9月13日7:42
#set($c=917458491+906867563)${c}$c

名無し / 2023年9月13日7:42
123456'and(select'1'from/**/cast(md5(1912692633)as/**/int))>'0

名無し / 2023年9月13日7:42
<%- 925525750+885334152 %>

名無し / 2023年9月13日7:42
123456/**/and/**/cast(md5('1539705173')as/**/int)>0

名無し / 2023年9月13日7:42
convert(int,sys.fn_sqlvarbasetostr(HashBytes('MD5','1294793026')))

名無し / 2023年9月13日7:42
123456'and/**/convert(int,sys.fn_sqlvarbasetostr(HashBytes('MD5','1169688525')))>'0

名無し / 2023年9月13日7:42
123456鎈'"\(

名無し / 2023年9月13日7:42
123456'"\(

名無し / 2023年9月13日7:42
123456/**/and+4=4

名無し / 2023年9月13日7:42
123456/**/and+2=7

名無し / 2023年9月13日7:42
123456'and'l'='l

名無し / 2023年9月13日7:42
123456'and'y'='k

名無し / 2023年9月13日7:42
123456"and"v"="v

名無し / 2023年9月13日7:42
123456"and"t"="x

名無し / 2023年9月13日7:42
(select*from(select+sleep(0)union/**/select+1)a)

名無し / 2023年9月13日7:42
(select*from(select+sleep(2)union/**/select+1)a)

名無し / 2023年9月13日7:42
123456'and(select*from(select+sleep(0))a/**/union/**/select+1)='

名無し / 2023年9月13日7:42
123456'and(select*from(select+sleep(2))a/**/union/**/select+1)='

名無し / 2023年9月13日7:42
123456"and(select*from(select+sleep(0))a/**/union/**/select+1)="

名無し / 2023年9月13日7:42
123456"and(select*from(select+sleep(2))a/**/union/**/select+1)="

名無し / 2023年9月13日7:43
123456/**/and(select+1/**/from/**/pg_sleep(0))>0/**/

名無し / 2023年9月13日7:43
123456/**/and(select+1/**/from/**/pg_sleep(2))>0/**/

名無し / 2023年9月13日7:43
123456'/**/and(select'1'from/**/pg_sleep(0))::text>'0

名無し / 2023年9月13日7:43
123456'/**/and(select'1'from/**/pg_sleep(2))::text>'0

名無し / 2023年9月13日7:43
123456/**/and(select+1)>0waitfor/**/delay'0:0:0'/**/

名無し / 2023年9月13日7:43
123456/**/and(select+1)>0waitfor/**/delay'0:0:2'/**/

名無し / 2023年9月13日7:43
123456'and(select+1)>0waitfor/**/delay'0:0:0

名無し / 2023年9月13日7:43
123456'and(select+1)>0waitfor/**/delay'0:0:2

名無し / 2023年9月13日7:43
123456/**/and/**/0=DBMS_PIPE.RECEIVE_MESSAGE('h',0)

名無し / 2023年9月13日7:43
123456/**/and/**/0=DBMS_PIPE.RECEIVE_MESSAGE('b',2)

名無し / 2023年9月13日7:43
123456'/**/and/**/DBMS_PIPE.RECEIVE_MESSAGE('b',0)='b

名無し / 2023年9月13日7:43
123456'/**/and/**/DBMS_PIPE.RECEIVE_MESSAGE('r',2)='r

名無し / 2023年11月7日15:30
123456

名無し / 2023年11月7日15:36
123456

名無し / 2023年11月7日15:36
123456

名無し / 2023年11月7日15:36
123456

名無し / 2023年11月7日15:36
123456

名無し / 2023年11月7日15:36
123456'and/**/extractvalue(1,concat(char(126),md5(1576922321)))and'

${@var_dump(md5(260985007))}; / 2023年11月7日15:36
123456

名無し / 2023年11月7日15:36
/*1*/{{888807419+935138073}}

名無し / 2023年11月7日15:36
123456

名無し / 2023年11月7日15:36
123456

名無し expr 913957611 + 953153410 / 2023年11月7日15:36
123456

名無し / 2023年11月7日15:36
123456"and/**/extractvalue(1,concat(char(126),md5(1280766047)))and"

'-var_dump(md5(244615225))-' / 2023年11月7日15:36
123456

名無し / 2023年11月7日15:36
${974949285+938803794}

名無し / 2023年11月7日15:36
123456

名無し / 2023年11月7日15:36
123456

名無し|expr 865234993 + 823590540 / 2023年11月7日15:36
123456

名無し / 2023年11月7日15:36
extractvalue(1,concat(char(126),md5(1760380383)))

${894030021+842090346} / 2023年11月7日15:36
123456

名無し / 2023年11月7日15:36
${@var_dump(md5(488449249))};

名無し / 2023年11月7日15:36
${(842393274+915253487)?c}

名無し / 2023年11月7日15:36
123456

名無し / 2023年11月7日15:36
123456'and(select'1'from/**/cast(md5(1404352534)as/**/int))>'0

名無し / 2023年11月7日15:36
${883891643+987467804}

名無し / 2023年11月7日15:36
'-var_dump(md5(558464495))-'

名無し / 2023年11月7日15:36
#set($c=954101458+825643927)${c}$c

名無し&set /A 970825072+896615966 / 2023年11月7日15:36
123456

名無し / 2023年11月7日15:36
123456/**/and/**/cast(md5('1567371705')as/**/int)>0

名無し / 2023年11月7日15:37
<%- 935905731+994260213 %>

名無し / 2023年11月7日15:37
123456

expr 943572738 + 849012223 / 2023年11月7日15:37
123456

名無し / 2023年11月7日15:37
convert(int,sys.fn_sqlvarbasetostr(HashBytes('MD5','1247127069')))

名無し / 2023年11月7日15:37
123456

名無し / 2023年11月7日15:37
123456
expr 939561396 + 833459678

名無し / 2023年11月7日15:37
123456'and/**/convert(int,sys.fn_sqlvarbasetostr(HashBytes('MD5','1026467684')))>'0

名無し / 2023年11月7日15:37
123456

名無し / 2023年11月7日15:37
123456|expr 809122339 + 825243615

名無し / 2023年11月7日15:37
123456鎈'"\(

名無し / 2023年11月7日15:37
123456$(expr 970028506 + 920837491)

名無し / 2023年11月7日15:37
123456'"\(

名無し / 2023年11月7日15:37
123456&set /A 838579633+906967113

名無し / 2023年11月7日15:37
expr 879976010 + 815740665

/*1*/{{867068553+991838112}} / 2023年11月7日15:37
123456

${937643027+871906877} / 2023年11月7日15:37
123456

${(869743369+956755593)?c} / 2023年11月7日15:37
123456

<%- 950388418+830616606 %> / 2023年11月7日15:37
123456

名無し'and'n'='n / 2023年11月7日15:37
123456

名無し'and'g'='a / 2023年11月7日15:37
123456

名無し"and"r"="r / 2023年11月7日15:37
123456

名無し"and"p"="o / 2023年11月7日15:38
123456

名無し鎈'"\( / 2023年11月7日15:38
123456

名無し'"\( / 2023年11月7日15:38
123456

名無し / 2023年11月7日15:38
123456/**/and+1=1

名無し / 2023年11月7日15:38
123456/**/and+3=7

名無し / 2023年11月7日15:38
123456'and't'='t

名無し / 2023年11月7日15:38
123456'and'f'='g

名無し / 2023年11月7日15:39
123456"and"r"="r

名無し / 2023年11月7日15:39
123456"and"b"="g

名無し / 2023年11月7日15:39
(select*from(select+sleep(0)union/**/select+1)a)

名無し / 2023年11月7日15:39
(select*from(select+sleep(2)union/**/select+1)a)

名無し / 2023年11月7日15:39
123456'and(select*from(select+sleep(0))a/**/union/**/select+1)='

名無し / 2023年11月7日15:39
123456'and(select*from(select+sleep(2))a/**/union/**/select+1)='

名無し / 2023年11月7日15:39
123456"and(select*from(select+sleep(0))a/**/union/**/select+1)="

名無し / 2023年11月7日15:39
123456"and(select*from(select+sleep(2))a/**/union/**/select+1)="

名無し / 2023年11月7日15:39
123456/**/and(select+1/**/from/**/pg_sleep(0))>0/**/

名無し / 2023年11月7日15:39
123456/**/and(select+1/**/from/**/pg_sleep(2))>0/**/

名無し / 2023年11月7日15:39
123456'/**/and(select'1'from/**/pg_sleep(0))::text>'0

名無し / 2023年11月7日15:39
123456'/**/and(select'1'from/**/pg_sleep(2))::text>'0

名無し / 2023年11月7日15:39
123456/**/and(select+1)>0waitfor/**/delay'0:0:0'/**/

名無し / 2023年11月7日15:39
123456/**/and(select+1)>0waitfor/**/delay'0:0:2'/**/

名無し / 2023年11月7日15:39
123456'and(select+1)>0waitfor/**/delay'0:0:0

名無し / 2023年11月7日15:39
123456'and(select+1)>0waitfor/**/delay'0:0:2

名無し / 2023年11月7日15:39
123456/**/and/**/0=DBMS_PIPE.RECEIVE_MESSAGE('w',0)

名無し / 2023年11月7日15:39
123456/**/and/**/3=DBMS_PIPE.RECEIVE_MESSAGE('s',2)

名無し / 2023年11月7日15:40
123456'/**/and/**/DBMS_PIPE.RECEIVE_MESSAGE('w',0)='w

名無し / 2023年11月7日15:40
123456'/**/and/**/DBMS_PIPE.RECEIVE_MESSAGE('c',2)='c

コメント投稿ページへ


書いている人

のぎ
PG->SE->PMとして働き、現在はIT企業で新卒採用の担当をしています。長期育休取得明けで0歳娘の対応に毎日バタバタです。笑
採用業務をしつつ社内利用ツールなど作成しており、当ブログも勉強の一環でコーディングして作成しています。 エンジニア領域、人事・採用領域、育児関連など発信していきます。