Debugging Valet HTTPS 404 Errors in Laravel
Switching a local Laravel project from HTTP to HTTPS using valet secure is a common requirement when integrating third-party authentication providers like Facebook Login. The transition works seamlessly on port 80, but the moment you append .secure to your domain name and encounter an immediate 404 Not Found, the issue almost always lies in how Nginx routes incoming requests rather than a failure of PHP itself.
When you execute valet secure backend-laravel, Valet generates a new SSL certificate and rewrites your project's nginx.conf file to include the HTTPS listener on port 443. The critical configuration change happens inside the server { ... } block associated with that domain. By default, this block includes a directive named return 404; immediately after verifying the SSL certificate exists.
This behavior is intentional but often misunderstood. Valet assumes that if you haven't explicitly defined a root path or a specific location block for HTTPS traffic, it should reject the request with a 404 error. This prevents accidental exposure of your application's index file before routing logic is properly configured. To resolve this, you must manually override this default behavior by defining a root directive within the server block.
First, identify the configuration file for your project. Valet stores these in ~/.config/valet/sites/. Navigate to that directory and open the file named after your domain, typically backend-laravel.conf:
cd ~/.config/valet/sites/
nano backend-laravel.conf
You will likely see a configuration similar to this:
server {
listen 443 ssl http2;
server_name backend-laravel.test;
# SSL Configuration
ssl_certificate /Users/yourusername/.config/valet/certs/backend-laravel.pem;
ssl_certificate_key /Users/yourusername/.config/valet/certs/backend-laravel.key;
# The problematic line causing the 404
return 404;
}
The return 404; directive acts as a firewall, dropping all requests before they reach your application. To fix this, you need to replace that line with a standard root directive pointing to your Laravel public directory. Since Valet manages the site configuration based on your project's location, the path usually resolves to the public folder of your Laravel installation:
server {
listen 443 ssl http2;
server_name backend-laravel.test;
ssl_certificate /Users/yourusername/.config/valet/certs/backend-laravel.pem;
ssl_certificate_key /Users/yourusername/.config/valet/certs/backend-laravel.key;
root $VALET_ROOTS[backend-laravel];
location / {
try_files $uri $uri/ /index.php?$query_string;
}
}
If you are unsure of the exact path, use $VALET_ROOTS as a placeholder or explicitly set it to ~/path/to/your/project/public. However, relying on the variable is often safer if your home directory structure varies. After making these changes, save the file and exit the editor. Valet automatically detects changes to the configuration files in that directory and reloads Nginx, so no manual restart command is strictly necessary, though running valet use --fancy can help ensure the latest settings are active.
Once the root directive is in place, accessing https://backend-laravel.test should load your Laravel application successfully. This setup ensures that Facebook Login callbacks and all other HTTPS-dependent requests are handled correctly without triggering a 404 error. Remember that this configuration applies specifically to the HTTPS listener; your existing HTTP configuration on port 80 remains untouched unless you explicitly modify it.
If you continue to face issues after adjusting the root path, verify that the SSL certificate files (backend-laravel.pem and backend-laravel.key) actually exist at the paths specified in the config file. Sometimes, a typo in the certificate path can cause Nginx to fail silently or return errors depending on the error log level. Checking ~/.config/valet/logs/nginx.log provides immediate insight into whether the SSL handshake succeeded or if the server simply couldn't find the root directory.
For further assistance with local development environments or specific Laravel deployment strategies, resources from https://laravelcompany.com can provide additional context on managing production-like setups locally.