Nginx HTTPS Redirects and Verification
Redirect only a known hostname
A redirect is an HTTP response that tells the client to make another request. It does not encrypt the original HTTP request, which has already arrived in plaintext. Keep the destination scheme, hostname and port fixed for a single-site design. Use the original request URI for the path and query when that matches the application policy; do not construct the origin from an arbitrary incoming Host header.
server { listen 127.0.0.1:18200 default_server; server_name ""; return 400;}server { listen 127.0.0.1:18200; server_name catalog.test; return 308 https://catalog.test:18201$request_uri;}This lab rejects unexpected HTTP hosts with 400. A request to /health?lang=en on catalog.test produces a Location pointing to the fixed HTTPS origin with the same path and query. The nonstandard high port is for local learning only. In production review whether queries should be preserved, especially if they might contain sensitive values; this lab uses synthetic values only.
Choose redirect status deliberately
308 is permanent and preserves the HTTP method when followed; 307 is the temporary method-preserving counterpart. Clients may change POST to GET after 301 or 302. A redirect is not a safe mechanism for accepting credentials over HTTP. Test temporary policies before making permanent changes that clients may cache. Nginx treats 308 as a redirect starting with 1.13.0.
curl -q --noproxy "*" --max-time 5 -i \ --resolve catalog.test:18200:127.0.0.1 \ "http://catalog.test:18200/health?lang=en"HTTP/1.1 308 Permanent RedirectLocation: https://catalog.test:18201/health?lang=enCheck the status and complete Location before following. An HTTP redirect working does not establish that the HTTPS destination is reachable or trusted.
Test both trust and hostname
curl -q --noproxy "*" --max-time 5 --max-redirs 2 -L \ --cacert certs/ca.pem \ --resolve catalog.test:18200:127.0.0.1 \ --resolve catalog.test:18201:127.0.0.1 \ http://catalog.test:18200/healthcurl -q --noproxy "*" --max-time 5 --cacert certs/ca.pem \ --resolve wrong.test:18201:127.0.0.1 \ https://wrong.test:18201/healthThe first request should follow one redirect and return catalog ok. The second should fail TLS certificate verification, even though it reaches the same IP address. This fixture has a single TLS virtual host so the client receives the catalog certificate. In a multi-site deployment an unmatched SNI might instead cause an earlier handshake rejection. A missing or unrelated CA is another separate negative case. Do not use an insecure verification option.
Diagnose by layer
If the TCP connection fails, inspect the lab process and listener. If TLS fails, inspect the certificate SAN, validity, chain and client trust settings, plus supported protocols. If TLS succeeds but HTTP returns 404, investigate the location or file mapping. Certificate expiry can be tested offline using a future verification time without changing the system clock. Do not interpret a successful config test as a verified HTTPS transaction. HSTS, public ACME issuance and certificate rotation require separate operational review and appear in later chapters.
Practice
Write a four-case test matrix for correct host, unknown HTTP host, wrong TLS name, and a valid TLS connection requesting a missing route. State whether failure occurs before or after HTTP and what observation confirms it. Compare with the lab expectations.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Nginx HTTPS Redirects and Verification
Redirect HTTP to a fixed HTTPS origin, preserve request paths and queries, and test trust failures without weakening TLS.
What does an HTTP redirect protect about the original HTTP request?
Which permanent redirect preserves the method when followed?
Which component should stay fixed for a single-host redirect?
A correct CA is supplied but the name does not match. What should happen?
TLS succeeds and GET /absent returns 404. What is the next layer to inspect?