ssl-guides 2026-12-02 ⏱ 4 min read

Configure SSL for Laravel APIs

Configure SSL for Laravel APIs

Laravel does not terminate TLS. Nginx or Apache does, usually with PHP-FPM behind it, or a load balancer does. Laravel's job is to notice that the original request was secure, generate https:// URLs and mark cookies as secure. Most "my API works but returns http links" problems come from skipping that second half.

Step 1: Terminate TLS at Nginx

Issue a certificate for your API hostname first; see getting a free certificate with Certbot. Then configure the server block. The paths below are shortened; Certbot keeps the real files under /etc/letsencrypt/live/.

server {
    listen 443 ssl;
    server_name api.example.com;
    root /var/www/app/public;
    index index.php;

    ssl_certificate     /etc/ssl/api/fullchain.pem;
    ssl_certificate_key /etc/ssl/api/privkey.pem;
    ssl_protocols       TLSv1.2 TLSv1.3;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }
    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME
            $realpath_root$fastcgi_script_name;
        fastcgi_param HTTPS on;
        fastcgi_pass unix:/run/php/php-fpm.sock;
    }
}

The fastcgi_param HTTPS on; line is what tells PHP the request was secure. Adjust the socket path to match your PHP version. A second server block on port 80 should return a 301 to HTTPS.

Configure SSL behind a Laravel proxy

If a load balancer or CDN terminates TLS and talks to Laravel over HTTP, Laravel must be told which proxies it can believe. In current Laravel versions this is configured in bootstrap/app.php; older versions use the TrustProxies middleware class. Check the documentation for the version you run.

->withMiddleware(function (Middleware $middleware) {
    $middleware->trustProxies(
        at: ['10.0.0.0/8'],
    );
})

Use the actual address range of your load balancer. Trusting every address (*) is only reasonable when the app is reachable exclusively through the proxy, because otherwise any client can spoof the forwarded headers.

Step 3: Force https URLs

Set APP_URL=https://api.example.com in .env. If generated links are still http://, force the scheme in AppServiceProvider::boot() for production only:

use Illuminate\Support\Facades\URL;

public function boot(): void
{
    if ($this->app->environment('production')) {
        URL::forceScheme('https');
    }
}

Forcing the scheme is a fallback. Correct proxy trust is the cleaner fix, because it also makes $request->secure() return the right answer.

Step 4: Cookies and token auth

SettingWhereValue
SESSION_SECURE_COOKIE.envtrue
SESSION_SAME_SITE.envlax (or none with secure for cross-site SPAs)
SANCTUM_STATEFUL_DOMAINS.envYour SPA hostnames, if used
APP_URL.envhttps://your hostname

For a pure token API (Sanctum tokens or Passport), cookies matter less, but the tokens still travel in the Authorization header and need HTTPS to stay private. Never put tokens in query strings.

Step 5: Redirect and HSTS

Do the HTTP redirect in Nginx so it also covers non-PHP paths. Add the HSTS header there too, once HTTPS has been stable; the process and risks are in how to enable HSTS. Start with a short max-age.

Configure SSL and test the Laravel app

curl -i https://api.example.com/api/health

curl -I http://api.example.com/api/health

openssl s_client -connect api.example.com:443 \
  -servername api.example.com -brief

Expect a 200 on the first call, a 301 on the second, and TLS 1.2 or 1.3 with a verified chain in the OpenSSL output. The OpenSSL testing guide explains each line, and the free SSL chain checker catches missing intermediates.

Outbound calls and local development

Laravel's HTTP client verifies certificates by default. Avoid withoutVerifying() outside of a local test. For local HTTPS, Laravel Herd, Valet or Sail with a locally trusted certificate give a secure context without touching production settings.

Renewals

When the certificate renews, Nginx must reload. Use a deploy hook as described in automating renewal with Certbot, and set an expiry alert so a failure is noticed early. A lapsed certificate breaks every client of the API at the same time.

Common mistakes

  • Setting APP_URL to http:// and wondering why signed URLs and redirects use it.
  • Trusting all proxies while the app is also reachable directly from the internet.
  • Caching configuration with php artisan config:cache and then editing .env, which no longer has any effect until the cache is rebuilt.
  • Enabling secure cookies before the HTTP redirect is in place.

Related reading: forcing HTTPS in Laravel and the wider PHP setup guide.

Frequently Asked Questions

Does Laravel need its own certificate?

No. Laravel runs behind a web server or a load balancer that holds the certificate. The framework only needs to know the request arrived over HTTPS, so it generates https URLs and marks cookies secure.

Why does my API still return http links?

Because the app does not see the proxy headers. Trust the proxy addresses, or set the trusted proxies to the load balancer, and Laravel will read X-Forwarded-Proto and build https links.

Should I force HTTPS in the app or at the proxy?

At the proxy when you can. A redirect at Nginx or the load balancer happens before PHP starts, which is cheaper and cannot be bypassed by a route that forgets the middleware.

🔒 Test Your Website SSL Certificate Now
Run Free SSL Check →

✅ Post-Installation Verification & SEO Checklist

After applying the steps above, run through this 5-point checklist to prevent browser warnings and protect your Google SEO rankings:

  • Verify full CA chain trust and TLS 1.3 support using our Live SSL Checker.
  • Scan for Mixed Content HTTP links and check HSTS/CSP headers on Security Headers Checker.
  • Enforce a permanent 301 HTTP-to-HTTPS redirect with our HTTPS Redirect Generator.
  • Confirm both root and www versions resolve cleanly to one canonical HTTPS URL.
  • Verify your HTTPS property and sitemap.xml inside Google Search Console and Bing Webmaster Tools.
Technically Reviewed by TryFreeSSL Security Team
Last updated: 2026-12-05
About Us →

Free SSL Certificates, Live SSL Checker & CSR Generator

Issue trusted Let's Encrypt SSL certificates in 60 seconds with zero server setup.

Related Articles

🤖 TryFreeSSL AI Assistant