0
0
Nginxdevops~20 mins

HTTP to HTTPS redirect in Nginx - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
HTTPS Redirect Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Configuration
intermediate
2:00remaining
Basic HTTP to HTTPS Redirect Configuration

Which nginx server block configuration correctly redirects all HTTP requests to HTTPS?

Nginx
server {
    listen 80;
    server_name example.com www.example.com;

    # Redirect rule here
}
Areturn 301 https://$host$request_uri;
Bproxy_pass https://$host$request_uri;
Crewrite ^ https://$host$request_uri permanent;
Derror_page 301 https://$host$request_uri;
Attempts:
2 left
💡 Hint

Use a simple return directive for permanent redirect in nginx.

💻 Command Output
intermediate
1:30remaining
Output of curl Command for HTTP Redirect

You run the command curl -I http://example.com against an nginx server configured to redirect HTTP to HTTPS. What is the expected HTTP status code in the response headers?

AHTTP/1.1 301 Moved Permanently
BHTTP/1.1 500 Internal Server Error
CHTTP/1.1 404 Not Found
DHTTP/1.1 200 OK
Attempts:
2 left
💡 Hint

Permanent redirects use status code 301.

Troubleshoot
advanced
2:30remaining
Why is HTTP to HTTPS Redirect Not Working?

An nginx server block is configured to redirect HTTP to HTTPS, but when accessing http://example.com, the browser does not redirect and shows the HTTP content instead. Which is the most likely cause?

Nginx
server {
    listen 80;
    server_name example.com;
    # redirect missing or incorrect
}

server {
    listen 443 ssl;
    server_name example.com;
    ssl_certificate /path/to/cert.pem;
    ssl_certificate_key /path/to/key.pem;
    # HTTPS content
}
AThe server_name directive is missing in the HTTPS block.
BThe HTTP server block is missing the redirect directive.
CThe HTTPS server block should listen on port 80.
DThe SSL certificate is expired, so redirect fails.
Attempts:
2 left
💡 Hint

Check if the HTTP block actually tells nginx to redirect.

🔀 Workflow
advanced
3:00remaining
Order of Steps to Enable HTTP to HTTPS Redirect in nginx

Arrange the steps in the correct order to enable HTTP to HTTPS redirect on an nginx server.

A3,1,2,4
B1,3,2,4
C2,1,3,4
D1,2,3,4
Attempts:
2 left
💡 Hint

Think about what must be done before configuring SSL and redirects.

Best Practice
expert
4:00remaining
Best Practice for Handling www and non-www Redirects with HTTPS

You want to redirect all HTTP and HTTPS requests to the HTTPS version with www.example.com. Which nginx configuration snippet correctly implements this best practice?

A
server {
    listen 80;
    server_name example.com;
    return 301 https://example.com$request_uri;
}

server {
    listen 443 ssl;
    server_name www.example.com;
    ssl_certificate /path/to/cert.pem;
    ssl_certificate_key /path/to/key.pem;
    # main site content
}
B
server {
    listen 80;
    server_name www.example.com;
    return 301 https://www.example.com$request_uri;
}

server {
    listen 443 ssl;
    server_name example.com www.example.com;
    ssl_certificate /path/to/cert.pem;
    ssl_certificate_key /path/to/key.pem;
    # main site content
}
C
server {
    listen 80;
    server_name example.com www.example.com;
    return 301 https://www.example.com$request_uri;
}

server {
    listen 443 ssl;
    server_name example.com;
    return 301 https://www.example.com$request_uri;
    ssl_certificate /path/to/cert.pem;
    ssl_certificate_key /path/to/key.pem;
}

server {
    listen 443 ssl;
    server_name www.example.com;
    ssl_certificate /path/to/cert.pem;
    ssl_certificate_key /path/to/key.pem;
    # main site content
}
D
server {
    listen 80;
    server_name example.com www.example.com;
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl;
    server_name example.com www.example.com;
    ssl_certificate /path/to/cert.pem;
    ssl_certificate_key /path/to/key.pem;
    # main site content
}
Attempts:
2 left
💡 Hint

Redirect all variants to a single canonical HTTPS domain with www.