Nginx Как перенаправить HTTP без -www на HTTPS www с одним перенаправлением?

Я перенаправляю входящий HTTP-трафик с порта 80 на HTTPS на порт 443. Я перенаправляю без www на www:

http://example.com -> https://example.com -> https://www.example.com

Похоже, что Google Pagespeed Insights считает это "множественной переадресацией". Также кажется, что между каждым перенаправлением возникает задержка в 300-400 мс.

Есть ли способ выполнить перенаправление за один шаг для всех сценариев?

  1. HTTP без www -> HTTPS www
  2. HTTP www -> HTTPS www
  3. HTTPS без www -> HTTPS www
  4. HTTPS www -> HTTPS www

Мой файл конфигурации ниже:

server {
        listen 80;
        server_name example.com;
        rewrite ^/(.*) https://example.com/$1 permanent;
}

server {
        root /var/www/html;

        index index.php index.js index.html index.htm index.nginx-debian.html;

        server_name example.com;
        return 301 https://www.example.com$request_uri;
        ssl_certificate /ssl/example.com.chained.crt;
        ssl_certificate_key /ssl/example.com.key;

        location / {
        proxy_pass http://localhost:8000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
        }

        location /wordpress {
        proxy_pass http://localhost:8090;
        proxy_http_version 1.1;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
        }
}

server {
        listen 443 ssl;
        listen [::]:443 ssl;

        root /var/www/html;

        index index.php index.js index.html index.htm index.nginx-debian.html;

        server_name www.example.com;
        ssl_certificate /ssl/example.com.chained.crt;
        ssl_certificate_key /ssl/example.com.key;

        location / {
        proxy_pass http://localhost:8000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
        }

        location /wordpress {
        proxy_pass http://localhost:8090;
        proxy_http_version 1.1;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
        }
}
0
задан 3 July 2019 в 05:54
1 ответ

В этой части конфигурации вы можете:

  • вы можете поймать несколько имен example.com и www.example.com
  • вы может выполнять перенаправление напрямую на www.example.com

Пример всей конфигурации перенаправления:

server {
        listen 80;
        server_name example.com  www.example.com;
        rewrite ^/(.*) https://www.example.com/$1 permanent;
}

server {
    listen 443 ssl;
    listen [::]:443 ssl;

    server_name example.com;
    ssl_certificate /ssl/example.com.chained.crt;
    ssl_certificate_key /ssl/example.com.key;
    rewrite ^/(.*) https://www.example.com/$1 permanent;
}


server {
    listen 443 ssl;
    listen [::]:443 ssl;

    server_name www.example.com ;

    # HERE you push all you configuration for HTTPS://WWW.EXAMPLE.COM

    #.../.../

}
1
ответ дан 4 December 2019 в 15:40

Теги

Похожие вопросы