1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Laravel環境における Nginx設定ファイルの基本設定内容

Last updated at Posted at 2025-12-25

概要

Laravel を Nginx で動かす際に設定する default.conf(または server 設定ファイル)について、設定例・各項目の解説・http/https の違い・SSL 設定・再起動方法・確認方法・注意点 など、基本となる内容をまとめます。

※メインのnginx.confファイル内で include /etc/nginx/conf.d/*.conf のようにincludeディレクティブによって読み込まれているファイルについて記載しています。

想定環境

  • OS:Linux(AlmaLinux / CentOS / Rocky Linux など)
  • Webサーバ:Nginx
  • アプリケーション:Laravel
  • PHP:PHP-FPM

内容

Nginx 設定ファイルの場所

一般的な配置例です。

/etc/nginx/conf.d/default.conf

基本的な default.conf 設定例

server {
    listen 80;
    server_name example.com;

    root /var/www/laravel/public;
    index index.php index.html;

    location / {
      # 特定のIPを許可
        allow 10.10.10.0;
        # それ以外はすべて拒否
        deny all;
        
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_pass unix:/var/run/php-fpm/www.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }

    location ~ /\.(?!well-known).* {
        deny all;
    }

    error_page 404 /custom_404.html;
    location = /custom_404.html {
        root /opt/nginx/html;
        internal;
    }
}

各設定項目の解説

listen
listen 80;
  • 待ち受けるポート番号
  • 80 は HTTP 通信
server_name
server_name example.com;
  • アクセスされるドメイン名
  • 複数指定も可能
server_name example.com www.example.com;
root
root /var/www/laravel/public;
  • Laravelでは必ず public ディレクトリを指定
  • public 以外を指定するとセキュリティリスクあり
IPの許可/拒否
allow 10.10.10.0;
deny all;
  • 特定のIPアドレスからのアクセスの許可と禁止を行う
  • allow 許可するIPアドレス
  • deny 拒否するIPアドレス
try_files
try_files $uri $uri/ /index.php?$query_string;
  • Laravel のルーティングを正しく動かすための重要設定
  • 存在しないパスはすべて index.php に流す
PHP-FPM 設定
fastcgi_pass unix:/var/run/php-fpm/www.sock;
  • PHP-FPM との通信設定
  • 環境によっては TCP 接続の場合もあり
fastcgi_pass 127.0.0.1:9000;
隠しファイルへのアクセス禁止
location ~ /\.(?!well-known).* {
    deny all;
}
  • .env などの重要ファイルを外部から見えなくする
エラーページのカスタマイズ
error_page 404 /custom_404.html;
location = /custom_404.html {
  root /opt/nginx/html;
    internal;
}
  • 404エラー発生時「custom_404.html」が帰ってくる

HTTP と HTTPS でアクセスされた時の違い

項目 HTTP HTTPS
ポート 80 443
通信内容 平文 暗号化
セキュリティ 低い 高い
ブラウザ表示 警告が出る場合あり 鍵マーク表示
HTTPS(SSL)設定例

HTTPS 用 server 設定

server {
    listen 443 ssl;
    server_name example.com;

    root /var/www/laravel/public;
    index index.php index.html;

  # 証明書と秘密鍵のパスを設定
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_pass unix:/var/run/php-fpm/www.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
}
HTTP → HTTPS リダイレクト

HTTP でアクセスされた場合、自動で HTTPS に転送

server {
    listen 80;
    server_name example.com;

    return 301 https://$host$request_uri;
}

Nginx の再起動・反映方法

sudo nginx -t

syntax is ok と test is successful が出ればOK
エラーがある場合、再起動前に必ず修正

再読み込み(推奨)
sudo systemctl reload nginx

動作確認方法

ブラウザ確認

http://example.com
https://example.com
ステータス確認
sudo systemctl status nginx

まとめ

  • Laravel × Nginx では try_files 設定が最重要
  • 本番環境では HTTPS を必ず使用
  • 設定変更後は nginx -t → reload を徹底する

お知らせ(採用情報)

最後にお知らせとなりますが、AppTime では一緒に働くメンバーを募集しております。
詳しくは採用情報ページをご確認ください。

AppTime 採用情報

みなさまからのご応募をお待ちしております。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?