Moving a Laravel app from Apache to nginx sounds like a one-line change in your server config. In practice it is a small migration with a handful of sharp edges. Apache reads .htaccess files in each directory. nginx does not. PHP runs through mod_php on Apache, but through PHP-FPM on nginx. Rewrite rules, uploads, headers, and file permissions all behave differently enough to break a working app in ways that look confusing at first. This post covers the breaks I ran into and the fixes that stuck, so your cutover stays boring.
1. Your .htaccess files stop working
This is the first thing that breaks, and it breaks quietly. nginx never reads .htaccess, so every rule in public/.htaccess simply vanishes: the front-controller rewrite, the directory listing protection, the trailing-slash handling. Your routes return 404s while static files still load, which makes it look like the app is half broken. There is no setting to turn .htaccess support on. You translate each rule into the nginx server block instead, and you test the result before you switch traffic over.
2. The rewrite rule becomes try_files
Laravel's Apache rewrite sends every request that is not a real file or directory to index.php. The nginx equivalent is one try_files line inside the root location block:
location / {
try_files $uri $uri/ /index.php?$query_string;
}
That line does the same job: serve the file if it exists, otherwise hand the request to Laravel with the query string intact. The full server block also needs the document root pointed at public, never at the project root. Pointing the root at the project root is how .env files and source code end up reachable from the web, so check this line twice:
server {
listen 80;
server_name example.com;
root /var/www/example.com/public;
index index.php;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
}
3. PHP runs through PHP-FPM, not mod_php
On Apache with mod_php, PHP runs inside the web server process. On nginx, PHP runs as a separate service, PHP-FPM, and nginx talks to it over a Unix socket or a TCP port. That means a second service to install, start, and keep running, and a second place for things to fail. A typical PHP location block looks like this:
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
The socket path must match the PHP version you actually installed, or every PHP request fails. Also check that the PHP-FPM pool runs as a user that can read your project files and write to storage/. If you hit 502 errors after the move, the cause is almost always here: PHP-FPM stopped, the socket path wrong, or the pool user lacking permissions. I wrote up the full checklist for that error in Nginx 502 Bad Gateway with PHP-FPM: The Usual Suspects.
4. Uploads hit nginx's own size limit
On Apache, the upload limit that bites you is usually PHP's upload_max_filesize. On nginx there is an extra limit in front of it: client_max_body_size, which defaults to 1 MB. A file upload that worked fine on Apache suddenly fails with a 413 error, and the Laravel logs show nothing because the request never reaches PHP. Set it in the server block to match what your app needs:
server {
client_max_body_size 20M;
}
Then make sure PHP's own limits still agree: upload_max_filesize and post_max_size in the PHP-FPM php.ini, and restart PHP-FPM after changing them. Both limits have to pass, not just one.
5. Storage, permissions, and the public symlink
Laravel needs write access to storage/ and bootstrap/cache/, and the web server user on nginx is often www-data while your deploy user is someone else. After the move, re-check ownership rather than assuming the Apache setup still holds:
sudo chown -R www-data:www-data storage bootstrap/cache
sudo chmod -R 775 storage bootstrap/cache
php artisan storage:link
Re-run php artisan storage:link if the public/storage symlink was created under a different user or got lost in the move. A broken symlink shows up as missing user-uploaded images with no error in the app logs, which is easy to misread as a code bug.
6. Headers and security bits you used to get for free
Some Apache setups add security headers globally, or the .htaccess in public/ sets them. nginx starts with none of that, so add the basics in the server block and block direct access to hidden files in the same place:
add_header X-Frame-Options "SAMEORIGIN";
add_header X-Content-Type-Options "nosniff";
location ~ /\.(?!well-known).* {
deny all;
}
That last block keeps dotfiles like .env and .git out of reach even if the document root is ever misconfigured. nginx does not serve directory listings by default, so that particular Apache worry goes away, but do not rely on it as your only protection.
7. Cutover checklist
Run through these in order and the move stays uneventful. First, validate the config and reload nginx:
sudo nginx -t
sudo systemctl reload nginx
Then clear the cached config, since paths and environment values can change with the server setup:
php artisan config:clear
php artisan config:cache
php artisan route:cache
Then click through the app yourself: log in, submit a form, upload a file, and hit an API route. Queue workers and scheduled jobs run outside the web server, so they keep working through the move, but confirm the worker is still running afterwards anyway. Finally, watch the error log for the first few minutes:
sudo tail -f /var/log/nginx/error.log
If something breaks, the log usually names the exact file or socket it could not reach, which beats guessing.
Moving a Laravel app from Apache to nginx is mostly translation work: rewrite rules become try_files, mod_php becomes PHP-FPM, and the limits and headers you took for granted get set explicitly. Do the translation in a staging copy first, run the checklist, and the production cutover is a config reload rather than a debugging session.