Nginx Configuration Files for Laravel Valet

I switched to Laravel Valet for local WordPress development on my Macbook in December 2019. I love it. I presented at January’s WordPress Lancaster meetup about how and why I made the move.

This week, I’ve written some code that protects uploads to the WordPress Media Library. (Did you know that anyone can view uploads if they find the URLs? The Media Library uses only security by obscurity.) To code my solution, I’m using an .htaccess file to protect a subfolder in wp-content/uploads.

There is No .htaccess in Nginx

.htaccess files do not work in Laravel Valet because it uses the nginx web server instead of Apache. (And, Valet does not use a reverse proxy to allow Apache features to work while running the faster nginx.) nginx does not allow its configuration to be changed on-the-fly, and that’s one reason it’s faster than Apache.

To replicate the protected folder mechanism, I learned how to block access to a directory inside an nginx configuration file, but that introduced a new challenge: Where is the nginx configuration file? Most resources will identify usr/local/etc/nginx/nginx.conf as the configuration file, and while that’s correct, changing it and restarting Valet did not change the behavior of the folder.

Nginx Config File Location for Laravel Valet

Each site you create inside Valet gets its own configuration file at this location: ~/.config/valet/Nginx/sitename.test This is the file you should edit.

Restart Valet to Allow Configuration Changes to be Loaded

Nginx needs to be restarted in order for configuration changes to take effect, and I found it easy to use this command inside Terminal: valet restart