Nginx has no .htaccess. It is not disabled and cannot be turned on: reading a file in every directory on every request is exactly the cost Nginx exists to avoid. Rules live in the server config and are read once at start-up.
A site moved from Apache brings its .htaccess with it, and the file is simply ignored. Nothing errors. The rules just stop happening - which is how a redirect, a deny, or a rewrite silently disappears in a migration.
Redirect to https
server {
listen 80;
server_name yourdomain.com;
return 301 https://;
}
www to non-www
server {
listen 443 ssl;
server_name www.yourdomain.com;
return 301 https://yourdomain.com;
}
WordPress pretty permalinks
location / {
try_files / /index.php?;
}
Deny a directory
location ^~ /private/ {
deny all;
}
Password protection
location /staging/ {
auth_basic "Staging";
auth_basic_user_file /etc/nginx/.htpasswd;
}
Cache headers on static files
location ~* \.(jpg|jpeg|png|webp|gif|ico|css|js|woff2)$ {
expires 30d;
add_header Cache-Control "public, immutable";
}
Test before reloading, every time:
nginx -t. It names the file and line of anything it does not like, and a reload with a bad config leaves the old one running rather than breaking the site.