Nginx Nextcloud слишком много перенаправлений

Я пытаюсь установить Nextcloud на свой Raspberry Pi.

  1. Я создал A запись в Cloudflare с поддоменом, указывающим на мой IP-адрес Raspberry Pi, и настроил NGINX с помощью certbot , используя файл конфигурации по умолчанию в / etc / nginx / sites-available / nextcloud .
    1. Я создал символическую ссылку в / etc / nginx / sites-enable и проверил конфигурацию NGINX с помощью sudo nginx -t , и вся конфигурация верна .
    2. Я успешно перенаправляюсь на https , но получаю сообщение об ошибке слишком много перенаправлений при попытке открыть nextcloud.mydomain.com в браузере.

Это моя конфигурация nextcloud :

upstream php-handler {
    server 127.0.0.1:9000;
    #server unix:/var/run/php/php7.3-fpm.sock;
}

server {
    listen 80;
    server_name nextcloud.domain.com;

    if ($host = nextcloud.domain.com) {
        return 301 https://$server_name:443$request_uri;
    } # managed by Certbot

    listen 443 ssl http2;

    ssl_certificate /etc/letsencrypt/live/nextcloud.domain.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/nextcloud.domain.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
    add_header Strict-Transport-Security "max-age=31536000" always; # managed by Certbot
    ssl_trusted_certificate /etc/letsencrypt/live/nextcloud.domain.com/chain.pem; # managed by Certbot
    ssl_stapling on; # managed by Certbot
    ssl_stapling_verify on; # managed by Certbot

    # Add headers to serve security related headers
    # Before enabling Strict-Transport-Security headers please read into this
    # topic first.
    #add_header Strict-Transport-Security "max-age=15768000; includeSubDomains; preload;";
    #
    # WARNING: Only add the preload option once you read about
    # the consequences in https://hstspreload.org/. This option
    # will add the domain to a hardcoded list that is shipped
    # in all major browsers and getting removed from this list
    # could take several months.
    add_header X-Content-Type-Options nosniff;
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Robots-Tag none;
    add_header X-Download-Options noopen;
    add_header X-Permitted-Cross-Domain-Policies none;
    add_header Referrer-Policy no-referrer;

    # Remove X-Powered-By, which is an information leak
    fastcgi_hide_header X-Powered-By;

    # Path to the root of your installation
    root /var/www/html/nextcloud;

    location = /robots.txt {
        allow all;
        log_not_found off;
        access_log off;
    }

    # The following 2 rules are only needed for the user_webfinger app.
    # Uncomment it if you're planning to use this app.
    #rewrite ^/.well-known/host-meta /public.php?service=host-meta last;
    #rewrite ^/.well-known/host-meta.json /public.php?service=host-meta-json last;

    # The following rule is only needed for the Social app.
    # Uncomment it if you're planning to use this app.
    #rewrite ^/.well-known/webfinger /public.php?service=webfinger last;

    location = /.well-known/carddav {
      return 301 $scheme://$host:$server_port/remote.php/dav;
    }
    location = /.well-known/caldav {
      return 301 $scheme://$host:$server_port/remote.php/dav;
    }

    # set max upload size
    client_max_body_size 512M;
    fastcgi_buffers 64 4K;

    # Enable gzip but do not remove ETag headers
    gzip on;
    gzip_vary on;
    gzip_comp_level 4;
    gzip_min_length 256;
    gzip_proxied expired no-cache no-store private no_last_modified no_etag auth;
    gzip_types application/atom+xml application/javascript application/json application/ld+json application/manifest+json application/rss+xml application/vnd.geo+json application/vnd.ms-fontobject application/x-font-ttf application/x-web-app-manifest+json application/xhtml+xml application/xml font/opentype image/bmp image/svg+xml image/x-icon text/cache-manifest text/css text/plain text/vcard text/vnd.rim.location.xloc text/vtt text/x-component text/x-cross-domain-policy;

    # Uncomment if your server is build with the ngx_pagespeed module
    # This module is currently not supported.
    #pagespeed off;

    location / {
        rewrite ^ /index.php;
    }

    location ~ ^\/(?:build|tests|config|lib|3rdparty|templates|data)\/ {
        deny all;
    }
    location ~ ^\/(?:\.|autotest|occ|issue|indie|db_|console) {
        deny all;
    }

    location ~ ^\/(?:index|remote|public|cron|core\/ajax\/update|status|ocs\/v[12]|updater\/.+|oc[ms]-provider\/.+)\.php(?:$|\/) {
        fastcgi_split_path_info ^(.+?\.php)(\/.*|)$;
        set $path_info $fastcgi_path_info;
        try_files $fastcgi_script_name =404;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $path_info;
        fastcgi_param HTTPS on;
        # Avoid sending the security headers twice
        fastcgi_param modHeadersAvailable true;
        # Enable pretty urls
        fastcgi_param front_controller_active true;
        fastcgi_pass php-handler;
        fastcgi_intercept_errors on;
        fastcgi_request_buffering off;
    }

    location ~ ^\/(?:updater|oc[ms]-provider)(?:$|\/) {
        try_files $uri/ =404;
        index index.php;
    }

    # Adding the cache control header for js, css and map files
    # Make sure it is BELOW the PHP block
    location ~ \.(?:css|js|woff2?|svg|gif|map)$ {
        try_files $uri /index.php$request_uri;
        add_header Cache-Control "public, max-age=15778463";
        # Add headers to serve security related headers (It is intended to
        # have those duplicated to the ones above)
        # Before enabling Strict-Transport-Security headers please read into
        # this topic first.
        #add_header Strict-Transport-Security "max-age=15768000; includeSubDomains; preload;";
        #
        # WARNING: Only add the preload option once you read about
        # the consequences in https://hstspreload.org/. This option
        # will add the domain to a hardcoded list that is shipped
        # in all major browsers and getting removed from this list
        # could take several months.
        add_header X-Content-Type-Options nosniff;
        add_header X-XSS-Protection "1; mode=block";
        add_header X-Robots-Tag none;
        add_header X-Download-Options noopen;
        add_header X-Permitted-Cross-Domain-Policies none;
        add_header Referrer-Policy no-referrer;

        # Optional: Don't log access to assets
        access_log off;
    }

    location ~ \.(?:png|html|ttf|ico|jpg|jpeg|bcmap)$ {
        try_files $uri /index.php$request_uri;
        # Optional: Don't log access to other assets
        access_log off;
    }
}

Это результат curl:

curl -i http://nextcloud.domain.com
HTTP/1.1 301 Moved Permanently
Date: Mon, 03 Feb 2020 20:54:01 GMT
Transfer-Encoding: chunked
Connection: keep-alive
Cache-Control: max-age=3600
Expires: Mon, 03 Feb 2020 21:54:01 GMT
Location: https://nextcloud.domain.com/
X-Content-Type-Options: nosniff
Server: cloudflare
CF-RAY: 55f74c364a04d8d1-AMS

И когда я пытаюсь получить доступ к версии https :

curl -i https://nextcloud.domain.com 

HTTP/2 301 
date: Mon, 03 Feb 2020 20:55:45 GMT
content-type: text/html
set-cookie: __cfduid=d160f56e9986d2c7037ff81e233fcad0e1580763345; expires=Wed, 04-Mar-20 20:55:45 GMT; path=/; domain=.domain.com; HttpOnly; SameSite=Lax; Secure
location: https://nextcloud.domain.com:443/
strict-transport-security: max-age=15552000; includeSubDomains; preload
x-content-type-options: nosniff
x-xss-protection: 1; mode=block
x-robots-tag: none
x-download-options: noopen
x-permitted-cross-domain-policies: none
referrer-policy: no-referrer
cf-cache-status: DYNAMIC
expect-ct: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"
server: cloudflare
cf-ray: 55f74ebf5ed62bfe-AMS

<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx/1.14.2</center>
</body>
</html>

я заменил свою доменное имя с доменом .

В /var/log/nginx/access.log я получаю много строк с кодами перенаправления и HTTP: 301 и 185.

Если я удалю перенаправление return 301 https: // $ server_name: 443 @ request_uri; строка Я все еще перенаправляюсь на https , и я понятия не имею, почему.

Для справки, раньше я устанавливал lighttpd на Raspberry Pi, но я удалил его перед установкой NGINX .

0
задан 3 February 2020 в 23:14
1 ответ

У вас есть одно определение сервера для обоих HTTP и HTTPS и, как вы заметили, строки:

if ($host = nextcloud.domain.com) {
    return 301 https://$server_name:443$request_uri;
} # managed by Certbot

наносят большинство повреждений. Так как это 301 перенаправление, браузер запомнит его, пока не перезапустится.

Разделите конфигурацию двух портов прослушивания так, чтобы первые строки были:

server {
    listen 80;
    server_name nextcloud.domain.com;

    return 301 https://$server_name$request_uri; # managed by Certbot
}

server {
    listen 443 ssl http2;
    server_name nextcloud.domain.com;
    # rest of the config

Edit: Как вы заметили, вы можете поместить обе директивы listen в один блок сервера , однако это вызовет цикл перенаправления , если к вашему серверу осуществляется прямой доступ с помощью nextcloud.domain.com. Cloudflare, вероятно, поставит другой прокси-сервер между вашими клиентами и nginx и перенаправит запросы, используя ваш IP вместо доменного имени. Таким образом, цикл остается скрытым, но он все еще там.

Если вы хотите сохранить единственный блок сервера, вам следует подумать об изменении условия if на:

if ($scheme = http) {
    return 301 https://$server_name$request_uri;
} # managed by Certbot

Однако, с точки зрения ясности и производительности, это, вероятно, хуже, чем два блока сервера.

.
1
ответ дан 26 February 2020 в 00:36

Теги

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