[{"data":1,"prerenderedAt":427},["ShallowReactive",2],{"post-nginx-path-redirect":3},{"id":4,"title":5,"body":6,"date":416,"description":417,"extension":418,"meta":419,"navigation":131,"path":423,"seo":424,"stem":425,"__hash__":426},"blog\u002Fblog\u002Fnginx-path-redirect.md","Nginxで特定パスをリダイレクトする設定",{"type":7,"value":8,"toc":404},"minimark",[9,13,17,20,32,61,65,82,85,88,315,318,321,336,350,353,362,365,388,391,400],[10,11,12],"h2",{"id":12},"概要",[14,15,16],"p",{},"Nginxで特定のパスへのアクセスを別のURLにリダイレクトさせる方法をまとめます。",[10,18,19],{"id":19},"基本的なリダイレクト設定",[14,21,22,23,27,28,31],{},"特定のパスへのアクセスを301リダイレクトさせるには、",[24,25,26],"code",{},"location","ブロックで",[24,29,30],{},"return","ディレクティブを使用します。",[33,34,39],"pre",{"className":35,"code":36,"language":37,"meta":38,"style":38},"language-nginx shiki shiki-themes github-light github-dark","location \u002Fold-path\u002Findex.php {\n    return 301 https:\u002F\u002Fexample.com\u002Fnew-path;\n}\n","nginx","",[24,40,41,49,55],{"__ignoreMap":38},[42,43,46],"span",{"class":44,"line":45},"line",1,[42,47,48],{},"location \u002Fold-path\u002Findex.php {\n",[42,50,52],{"class":44,"line":51},2,[42,53,54],{},"    return 301 https:\u002F\u002Fexample.com\u002Fnew-path;\n",[42,56,58],{"class":44,"line":57},3,[42,59,60],{},"}\n",[62,63,64],"h3",{"id":64},"リダイレクトの種類",[66,67,68,76],"ul",{},[69,70,71,75],"li",{},[72,73,74],"strong",{},"301"," 恒久的なリダイレクト（SEO評価が引き継がれる）",[69,77,78,81],{},[72,79,80],{},"302"," 一時的なリダイレクト",[10,83,84],{"id":84},"実践的な設定例",[14,86,87],{},"Laravel\u002FPHPアプリケーション向けのNginx設定例です。",[33,89,91],{"className":35,"code":90,"language":37,"meta":38,"style":38},"server {\n    listen 80;\n    server_name example.com;\n    root \u002Fvar\u002Fwww\u002Fhtml\u002Fpublic;\n    index index.php;\n    charset utf-8;\n\n    # セキュリティヘッダー\n    add_header X-Frame-Options \"SAMEORIGIN\";\n    add_header X-Content-Type-Options \"nosniff\";\n\n    # メインのルーティング\n    location \u002F {\n        try_files $uri $uri\u002F \u002Findex.php?$query_string;\n    }\n\n    # 特定パスのリダイレクト\n    location \u002Fold-path\u002Findex.php {\n        return 301 https:\u002F\u002Fexample.com\u002Fnew-path;\n    }\n\n    # PHP-FPMの設定\n    location ~ \\.php$ {\n        fastcgi_pass app:9000;\n        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;\n        include fastcgi_params;\n    }\n\n    # 静的ファイルのログを無効化\n    location = \u002Ffavicon.ico { access_log off; log_not_found off; }\n    location = \u002Frobots.txt  { access_log off; log_not_found off; }\n\n    # 隠しファイルへのアクセス拒否\n    location ~ \u002F\\.(?!well-known).* {\n        deny all;\n    }\n\n    error_page 404 \u002Findex.php;\n}\n",[24,92,93,98,103,108,114,120,126,133,139,145,151,156,162,168,174,180,185,191,197,203,208,213,219,225,231,237,243,248,253,259,265,271,276,282,288,294,299,304,310],{"__ignoreMap":38},[42,94,95],{"class":44,"line":45},[42,96,97],{},"server {\n",[42,99,100],{"class":44,"line":51},[42,101,102],{},"    listen 80;\n",[42,104,105],{"class":44,"line":57},[42,106,107],{},"    server_name example.com;\n",[42,109,111],{"class":44,"line":110},4,[42,112,113],{},"    root \u002Fvar\u002Fwww\u002Fhtml\u002Fpublic;\n",[42,115,117],{"class":44,"line":116},5,[42,118,119],{},"    index index.php;\n",[42,121,123],{"class":44,"line":122},6,[42,124,125],{},"    charset utf-8;\n",[42,127,129],{"class":44,"line":128},7,[42,130,132],{"emptyLinePlaceholder":131},true,"\n",[42,134,136],{"class":44,"line":135},8,[42,137,138],{},"    # セキュリティヘッダー\n",[42,140,142],{"class":44,"line":141},9,[42,143,144],{},"    add_header X-Frame-Options \"SAMEORIGIN\";\n",[42,146,148],{"class":44,"line":147},10,[42,149,150],{},"    add_header X-Content-Type-Options \"nosniff\";\n",[42,152,154],{"class":44,"line":153},11,[42,155,132],{"emptyLinePlaceholder":131},[42,157,159],{"class":44,"line":158},12,[42,160,161],{},"    # メインのルーティング\n",[42,163,165],{"class":44,"line":164},13,[42,166,167],{},"    location \u002F {\n",[42,169,171],{"class":44,"line":170},14,[42,172,173],{},"        try_files $uri $uri\u002F \u002Findex.php?$query_string;\n",[42,175,177],{"class":44,"line":176},15,[42,178,179],{},"    }\n",[42,181,183],{"class":44,"line":182},16,[42,184,132],{"emptyLinePlaceholder":131},[42,186,188],{"class":44,"line":187},17,[42,189,190],{},"    # 特定パスのリダイレクト\n",[42,192,194],{"class":44,"line":193},18,[42,195,196],{},"    location \u002Fold-path\u002Findex.php {\n",[42,198,200],{"class":44,"line":199},19,[42,201,202],{},"        return 301 https:\u002F\u002Fexample.com\u002Fnew-path;\n",[42,204,206],{"class":44,"line":205},20,[42,207,179],{},[42,209,211],{"class":44,"line":210},21,[42,212,132],{"emptyLinePlaceholder":131},[42,214,216],{"class":44,"line":215},22,[42,217,218],{},"    # PHP-FPMの設定\n",[42,220,222],{"class":44,"line":221},23,[42,223,224],{},"    location ~ \\.php$ {\n",[42,226,228],{"class":44,"line":227},24,[42,229,230],{},"        fastcgi_pass app:9000;\n",[42,232,234],{"class":44,"line":233},25,[42,235,236],{},"        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;\n",[42,238,240],{"class":44,"line":239},26,[42,241,242],{},"        include fastcgi_params;\n",[42,244,246],{"class":44,"line":245},27,[42,247,179],{},[42,249,251],{"class":44,"line":250},28,[42,252,132],{"emptyLinePlaceholder":131},[42,254,256],{"class":44,"line":255},29,[42,257,258],{},"    # 静的ファイルのログを無効化\n",[42,260,262],{"class":44,"line":261},30,[42,263,264],{},"    location = \u002Ffavicon.ico { access_log off; log_not_found off; }\n",[42,266,268],{"class":44,"line":267},31,[42,269,270],{},"    location = \u002Frobots.txt  { access_log off; log_not_found off; }\n",[42,272,274],{"class":44,"line":273},32,[42,275,132],{"emptyLinePlaceholder":131},[42,277,279],{"class":44,"line":278},33,[42,280,281],{},"    # 隠しファイルへのアクセス拒否\n",[42,283,285],{"class":44,"line":284},34,[42,286,287],{},"    location ~ \u002F\\.(?!well-known).* {\n",[42,289,291],{"class":44,"line":290},35,[42,292,293],{},"        deny all;\n",[42,295,297],{"class":44,"line":296},36,[42,298,179],{},[42,300,302],{"class":44,"line":301},37,[42,303,132],{"emptyLinePlaceholder":131},[42,305,307],{"class":44,"line":306},38,[42,308,309],{},"    error_page 404 \u002Findex.php;\n",[42,311,313],{"class":44,"line":312},39,[42,314,60],{},[10,316,317],{"id":317},"主要な設定項目の説明",[62,319,320],{"id":320},"セキュリティヘッダー",[33,322,324],{"className":35,"code":323,"language":37,"meta":38,"style":38},"add_header X-Frame-Options \"SAMEORIGIN\";\nadd_header X-Content-Type-Options \"nosniff\";\n",[24,325,326,331],{"__ignoreMap":38},[42,327,328],{"class":44,"line":45},[42,329,330],{},"add_header X-Frame-Options \"SAMEORIGIN\";\n",[42,332,333],{"class":44,"line":51},[42,334,335],{},"add_header X-Content-Type-Options \"nosniff\";\n",[66,337,338,344],{},[69,339,340,343],{},[72,341,342],{},"X-Frame-Options"," クリックジャッキング攻撃を防止",[69,345,346,349],{},[72,347,348],{},"X-Content-Type-Options"," MIMEタイプスニッフィングを防止",[62,351,352],{"id":352},"try_filesディレクティブ",[33,354,356],{"className":35,"code":355,"language":37,"meta":38,"style":38},"try_files $uri $uri\u002F \u002Findex.php?$query_string;\n",[24,357,358],{"__ignoreMap":38},[42,359,360],{"class":44,"line":45},[42,361,355],{},[14,363,364],{},"ファイルの存在を順に確認し、存在しない場合はPHPルーターに処理を委譲します。",[366,367,368,375,381],"ol",{},[69,369,370,371,374],{},"静的ファイルとして ",[24,372,373],{},"$uri"," を探す",[69,376,377,378,374],{},"ディレクトリとして ",[24,379,380],{},"$uri\u002F",[69,382,383,384,387],{},"どちらも存在しなければ ",[24,385,386],{},"\u002Findex.php"," にフォールバック",[10,389,390],{"id":390},"まとめ",[14,392,393,394,396,397,399],{},"Nginxでのリダイレクト設定は",[24,395,26],{},"と",[24,398,30],{},"を組み合わせることでシンプルに実装できます。セキュリティヘッダーも合わせて設定することで、より安全なWebサーバーを構築できます。",[401,402,403],"style",{},"html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}",{"title":38,"searchDepth":51,"depth":51,"links":405},[406,407,410,411,415],{"id":12,"depth":51,"text":12},{"id":19,"depth":51,"text":19,"children":408},[409],{"id":64,"depth":57,"text":64},{"id":84,"depth":51,"text":84},{"id":317,"depth":51,"text":317,"children":412},[413,414],{"id":320,"depth":57,"text":320},{"id":352,"depth":57,"text":352},{"id":390,"depth":51,"text":390},"2022-01-15","Nginxで特定のパスにアクセスされた際に別のURLへリダイレクトさせる方法と、セキュリティヘッダーの設定について解説します。","md",{"tags":420},[37,421,422],"webserver","redirect","\u002Fblog\u002Fnginx-path-redirect",{"title":5,"description":417},"blog\u002Fnginx-path-redirect","Vfn7JmUZcLljzRFRJfydrZuvQP8vzAG7b0SEQvrqw5M",1773664054144]