0
0
LaravelHow-ToBeginner · 4 min read

How to Use Nginx with Laravel: Setup and Configuration Guide

To use nginx with Laravel, configure an Nginx server block pointing to Laravel's public directory as the root. Set up URL rewriting with try_files to route requests through index.php, enabling Laravel's routing system.
📐

Syntax

This is the basic Nginx server block syntax to serve a Laravel app:

  • server_name: Your domain or IP address.
  • root: Points to Laravel's public folder where the front controller index.php lives.
  • index: Defines the default file to serve.
  • location /: Handles URL rewriting to route requests to index.php.
  • try_files: Checks if the requested file exists; if not, forwards to index.php.
nginx
server {
    listen 80;
    server_name example.com;

    root /var/www/laravel_project/public;
    index index.php index.html index.htm;

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

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /\.ht {
        deny all;
    }
}
💻

Example

This example shows a complete Nginx configuration for a Laravel app running on PHP 8.1 with PHP-FPM. It serves the app on example.com and properly routes all requests through Laravel's index.php.

nginx
server {
    listen 80;
    server_name example.com;

    root /var/www/laravel_project/public;
    index index.php index.html index.htm;

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

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /\.ht {
        deny all;
    }
}
Output
Nginx serves the Laravel app at example.com, routing all requests through index.php, enabling Laravel's routing and controllers to handle URLs.
⚠️

Common Pitfalls

Common mistakes when using Nginx with Laravel include:

  • Setting root to the Laravel project root instead of the public folder, exposing sensitive files.
  • Not configuring try_files properly, causing 404 errors on routes.
  • Incorrect PHP-FPM socket or version in fastcgi_pass, leading to PHP errors.
  • Missing location ~ \.php$ block, so PHP files are not processed.

Example of wrong and right try_files usage:

nginx
# Wrong: Missing try_files or incorrect root
location / {
    try_files $uri $uri/ =404;
}

# Right: Properly routes to index.php
location / {
    try_files $uri $uri/ /index.php?$query_string;
}
📊

Quick Reference

Summary tips for using Nginx with Laravel:

  • Always set root to laravel_project/public.
  • Use try_files $uri $uri/ /index.php?$query_string; to enable Laravel routing.
  • Configure PHP-FPM correctly with the right socket or TCP address.
  • Protect sensitive files by denying access to hidden files like .htaccess.
  • Reload Nginx after changes with sudo systemctl reload nginx.

Key Takeaways

Set Nginx root to Laravel's public directory to serve the app securely.
Use try_files to route requests through index.php for Laravel's routing to work.
Configure PHP-FPM correctly to process PHP files without errors.
Deny access to hidden files to protect sensitive data.
Reload Nginx after configuration changes to apply them.