Which nginx server block configuration correctly redirects all HTTP requests to HTTPS?
server {
listen 80;
server_name example.com www.example.com;
# Redirect rule here
}Use a simple return directive for permanent redirect in nginx.
The return 301 https://$host$request_uri; directive sends a permanent redirect to the client, telling it to use HTTPS. The rewrite directive can also redirect but is more complex and less efficient here. proxy_pass is for forwarding requests, not redirecting. error_page is for error handling, not redirects.
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?
Permanent redirects use status code 301.
The curl -I command fetches headers only. When nginx redirects HTTP to HTTPS, it sends a 301 Moved Permanently status code to tell the client to use the HTTPS URL.
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?
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
}Check if the HTTP block actually tells nginx to redirect.
If the HTTP server block does not have a redirect directive, nginx will serve HTTP content normally without redirecting. SSL certificate issues or server_name missing in HTTPS block do not affect HTTP redirect. HTTPS block listening on port 80 is incorrect.
Arrange the steps in the correct order to enable HTTP to HTTPS redirect on an nginx server.
Think about what must be done before configuring SSL and redirects.
First, you must have a valid SSL certificate (step 1). Then configure the HTTP block to redirect (step 2) and the HTTPS block with SSL (step 3). Finally, reload nginx to apply changes (step 4).
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?
Redirect all variants to a single canonical HTTPS domain with www.
Option C redirects all HTTP requests and HTTPS requests for example.com to https://www.example.com. This ensures a single canonical URL with HTTPS and www. Other options either do not redirect non-www HTTPS or redirect to non-www HTTPS, which is not the best practice here.