0
0
Nginxdevops~10 mins

HTTP to HTTPS redirect in Nginx - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - HTTP to HTTPS redirect
Client sends HTTP request
Nginx listens on port 80
Check if request is HTTP
Yes
Send 301 redirect to HTTPS URL
Client receives redirect and sends HTTPS request
Secure connection established
The server listens for HTTP requests and responds with a redirect to the HTTPS version, prompting the client to reconnect securely.
Execution Sample
Nginx
server {
    listen 80;
    server_name example.com;
    return 301 https://$host$request_uri;
}
This nginx config listens on HTTP port 80 and redirects all requests to the HTTPS version of the same URL.
Process Table
StepClient RequestNginx ActionResponse SentClient Action
1HTTP GET http://example.com/pageCheck if request is HTTP301 Redirect to https://example.com/pageReceives redirect
2HTTPS GET https://example.com/pageServe HTTPS content (not shown here)200 OK with contentDisplays secure page
3N/AN/AN/AProcess ends
ExitN/ANo more HTTP requestsN/ASecure connection established
💡 After redirect, client uses HTTPS; HTTP request handling ends.
Status Tracker
VariableStartAfter Step 1After Step 2Final
$hostexample.comexample.comexample.comexample.com
$request_uri/page/page/page/page
Key Moments - 3 Insights
Why does nginx send a 301 redirect instead of serving content on HTTP?
Because the config explicitly returns a 301 redirect for all HTTP requests (see execution_table step 1), forcing clients to use HTTPS.
What happens if the client ignores the redirect?
The client will not get the secure content and stays on HTTP, which is not secure. The redirect is a signal to switch to HTTPS (execution_table step 1).
Why do we use $host and $request_uri in the redirect?
They preserve the original domain and path so the client is redirected to the exact same page but with HTTPS (see variable_tracker).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what response does nginx send to the HTTP request at step 1?
A200 OK with content
B404 Not Found
C301 Redirect to HTTPS URL
D500 Internal Server Error
💡 Hint
Check the 'Response Sent' column in execution_table row for step 1.
At which step does the client send the HTTPS request?
AStep 2
BStep 3
CStep 1
DExit
💡 Hint
Look at the 'Client Request' column in execution_table for HTTPS requests.
If we remove the 'return 301' line from the config, what happens to HTTP requests?
AThey get redirected to HTTPS anyway
BNginx serves HTTP content without redirect
CNginx returns 404 error
DNginx crashes
💡 Hint
The redirect is explicitly caused by 'return 301' in the config shown in execution_sample.
Concept Snapshot
nginx HTTP to HTTPS redirect:
- Listen on port 80 for HTTP
- Use 'return 301 https://$host$request_uri;' to redirect
- Sends permanent redirect to HTTPS URL
- Client then requests HTTPS URL
- Ensures secure connection by forcing HTTPS
Full Transcript
This visual execution shows how nginx redirects HTTP requests to HTTPS. When a client sends an HTTP request to port 80, nginx checks the request and responds with a 301 redirect to the same URL but with HTTPS. The client then follows this redirect and sends a new request over HTTPS. This process ensures all traffic uses a secure connection. Variables $host and $request_uri keep the domain and path unchanged during redirect. Without the redirect, nginx would serve HTTP content directly, which is less secure.