هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.
$ مستخدم عادي# بصلاحية root (أو مع sudo)
The quick answer
server {
listen 80;
listen [::]:80;
server_name shop.example.com www.shop.example.com;
location /.well-known/acme-challenge/ {
root /var/www/letsencrypt;
}
location / {
return 301 https://shop.example.com$request_uri;
}
}Step by step
- Port 80 only redirects (quick answer).
$request_urikeeps the path and the query string. Writing the canonical name (instead of$host) also sendswwwto the main name in the same step. - The HTTPS server block serves the site — and sends
https://wwwto the main name too:
server {
listen 443 ssl;
listen [::]:443 ssl;
server_name www.shop.example.com;
ssl_certificate /etc/letsencrypt/live/shop.example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/shop.example.com/privkey.pem;
return 301 https://shop.example.com$request_uri;
}
server {
listen 443 ssl;
listen [::]:443 ssl;
server_name shop.example.com;
ssl_certificate /etc/letsencrypt/live/shop.example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/shop.example.com/privkey.pem;
root /var/www/shop;
}The certificate must contain both names (-d shop.example.com -d www.shop.example.com).
- 301, 302 or 308?
| Code | Use |
|---|---|
301 | Permanent — normal for HTTP → HTTPS (browsers and Google remember it). |
308 | Permanent and keeps the method — for APIs that receive POSTs on HTTP. |
302 | Temporary — only while testing. |
- Behind a load balancer or CDN that ends TLS, Nginx only sees HTTP — redirect based on the header, or you get a loop:
if ($http_x_forwarded_proto = "http") {
return 301 https://shop.example.com$request_uri;
}(Or let the load balancer do the redirect — most can.)
- Add HSTS once everything works on HTTPS (start with a short
max-age):
add_header Strict-Transport-Security "max-age=300" always;Check that it worked
for u in http://shop.example.com http://www.shop.example.com https://www.shop.example.com; do curl -sSIL -o /dev/null -w "$u -> %{url_effective} (%{num_redirects} redirect)\n" "$u"; done
http://shop.example.com -> https://shop.example.com/ (1 redirect)Every start URL ends at https://shop.example.com/ with one redirect, and certbot renew --dry-run still works.
Common problems
| Problem | Cause | Fix |
|---|---|---|
ERR_TOO_MANY_REDIRECTS | TLS ends at a LB/CDN, Nginx sees HTTP and redirects again. | Use X-Forwarded-Proto (step 4), or redirect at the LB. |
| Redirect drops the path/query | return 301 https://shop.example.com; without $request_uri. | Add $request_uri. |
| Two or three redirects in a chain | http→https→www→non-www in separate steps. | Redirect straight to the final URL. |
www shows a certificate warning | The certificate has no www name. | Get a certificate with both names. |
| Certbot renewal fails after the change | The challenge path is redirected to a place without the file. | Keep the /.well-known/acme-challenge/ location (or use the nginx plugin). |
| POST requests turn into GET | 301/302 on a POST. | Use 308 for APIs, and give clients the HTTPS URL. |
Go further
- Command pages: nginx, curl, certbot.
- Next how-tos: Check the TLS settings of a website, Get a free TLS certificate, Set up Nginx as a reverse proxy, Add security headers in Nginx.
- Full guides: Nginx, Certbot. At work: Nginx.
1Why redirect to https://shop.example.com$request_uri instead of https://$host$request_uri?
It also moves www (and other names) to the one canonical name in the same single redirect.
2Why do you get a redirect loop behind a load balancer that ends TLS?
Nginx only sees HTTP from the LB and redirects every request; check X-Forwarded-Proto or redirect at the LB.