Memo

メモ > サーバ > 各論: エトセトラ > nginxでリクエストを操作する

■nginxでリクエストを操作する
■インデックスファイルを省略する 以下のようにすると、ファイル名を省略してアクセスしたときに index.php が表示される 無ければ index.html が表示される
server { location / { index index.php index.html; }
以下のように指定しても同じ
server { index index.php index.html;
Laravelなどを使っている際に try_files でクエリを処理している場合、 「location /」に「index index.php index.html;」を追加しても意図したように処理されない 以下のように個別に指定すると、そのディレクトリ内はindexの指定が有効になる(以下の場合は tool ディレクトリ内が対象) もっといい設定方法が無いかは要確認
server { location / { try_files $uri /index.php?$query_string; } location /tool/ { index index.php index.html; }
■SSLを強制する ロードバランサーが無い場合
server { listen 80; server_name refirio.net; return 301 https://refirio.net$request_uri; } server { listen 80; listen 443; server_name www.refirio.net; return 301 https://refirio.net$request_uri; } server { listen 443 ssl default_server; server_name refirio.net; 〜略〜 }
nginxでhttpsのwwwなしURLにリダイレクトさせる設定 - くろもワークス https://blog.kuromoworks.com/entry/2017/10/31/234648 [Nginx] httpをhttpsにリダイレクトする | ハックノート https://hacknote.jp/archives/29730/ ロードバランサーがある場合 (サーバ名を「server_name _;」としている場合、「return 301 https://www.refirio.net$request_uri;」のように指定する)
server { listen 80 default_server; listen [::]:80 default_server; server_name localhost; root /usr/share/nginx/html; if ($http_x_forwarded_proto = 'http') { return 301 https://$server_name$request_uri; }
■リダイレクトする /etc/nginx/nginx.conf で以下のように設定する
# /hoge/foo.html にアクセスしたら302リダイレクト location /hoge/foo.html { rewrite ^(.*)$ http://example.com/hoge/foo.html redirect; } # /foo/bar.html にアクセスしたら301リダイレクト location /foo/bar.html { rewrite ^(.*)$ http://example.com/foo/ permanent; }
Nginxのリダイレクト設定のメモ - Qiita https://qiita.com/kt_higa/items/f26ba1453164c82d6ad5 nginxのrewriteを使ったリダイレクト | Skyarch Broadcasting https://www.skyarch.net/blog/?p=7088 ■URLを書き換える(Apacheのmod_rewriteに相当) /etc/nginx/nginx.conf で以下のように設定する
# /test/ にアクセスしたら /test.html の内容を表示 location /test/ { rewrite /(.*)/ /$1.html last; }

Advertisement