متقدم
البحث فيأدلة «كيف»
طريقة البحث
  • اكتب المهمة بكلماتك (find port، open firewall، disk full) أو الصق رسالة الخطأ بين علامتي تنصيص.
  • اختر «الإجابة السريعة» لترى الأوامر فقط، أو مجالًا لتبقى في مجال واحد.
  • "كلمتان" تجد العبارة بالضبط.
  • الكلمات العادية تجد الأسماء والنصوص والأمثلة.
  • ↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق · / للبحث
جرّب:

كيف 87Web · Nginx

How to redirect HTTP to HTTPS in Nginx

Every visitor who types shop.example.com or follows an old http:// link should land on HTTPS, on one canonical name (with or without www), with one redirect — not a chain of three. This how-to writes the redirect server block, keeps the Let's Encrypt challenge path working, handles www, avoids loops behind a load balancer, and adds HSTS at the end.

  • 5 الدروس
  • 6 الأوامر

هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.

$ مستخدم عادي# بصلاحية root (أو مع sudo)

87.1

The quick answer

/etc/nginx/conf.d/shop-redirect.conf
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;
    }
}
nginx -t && systemctl reload nginx
curl -sSI http://www.shop.example.com/cart?x=1 | grep -iE '^HTTP|^location'
HTTP/1.1 301 Moved Permanently
Location: https://shop.example.com/cart?x=1

87.2

Step by step

  1. Port 80 only redirects (quick answer). $request_uri keeps the path and the query string. Writing the canonical name (instead of $host) also sends www to the main name in the same step.
  2. The HTTPS server block serves the site — and sends https://www to the main name too:
/etc/nginx/conf.d/shop.conf (part)
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).

  1. 301, 302 or 308?
CodeUse
301Permanent — normal for HTTP → HTTPS (browsers and Google remember it).
308Permanent and keeps the method — for APIs that receive POSTs on HTTP.
302Temporary — only while testing.
  1. Behind a load balancer or CDN that ends TLS, Nginx only sees HTTP — redirect based on the header, or you get a loop:
/etc/nginx/conf.d/shop.conf (behind a LB)
    if ($http_x_forwarded_proto = "http") {
        return 301 https://shop.example.com$request_uri;
    }

(Or let the load balancer do the redirect — most can.)

  1. Add HSTS once everything works on HTTPS (start with a short max-age):
/etc/nginx/conf.d/shop.conf (in the 443 server)
    add_header Strict-Transport-Security "max-age=300" always;

87.3

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.

87.4

Common problems

ProblemCauseFix
ERR_TOO_MANY_REDIRECTSTLS 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/queryreturn 301 https://shop.example.com; without $request_uri.Add $request_uri.
Two or three redirects in a chainhttp→https→www→non-www in separate steps.Redirect straight to the final URL.
www shows a certificate warningThe certificate has no www name.Get a certificate with both names.
Certbot renewal fails after the changeThe 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 GET301/302 on a POST.Use 308 for APIs, and give clients the HTTPS URL.

الأوامر في هذا الدرس

87.5

Go further

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.

كل الأوامر في هذا الفصل