Nginx Nextcloud 重定向过多

Nginx Nextcloud 重定向过多

我正在尝试在我的 Raspberry Pi 上安装 Nextcloud。

  1. 我在 Cloudflare 中创建了一个A record子域,指向我的 Raspberry Pi IP 地址,并NGINX使用certbot中的默认配置文件进行了配置/etc/nginx/sites-available/nextcloud
    1. 我已经创建了符号链接/etc/nginx/sites-enables并检查了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>

我已将我的域名替换为domain

/var/log/nginx/access.log我收到了很多带有重定向和 HTTP 代码的行:301 和 185 。

如果我删除重定向return 301 https://$server_name:443@request_uri;行,我仍然会被重定向到https,但我不知道为什么。

顺便说一下,我以前曾lighttpd在 Raspberry Pi 上安装过,但在安装之前我已经将其卸载了NGINX

答案1

server您对和都有一个定义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

编辑:正如您所说,您可以将两个listen指令放在一个server块中,但这会导致重定向循环如果您的服务器是使用 直接访问的nextcloud.domain.com。Cloudflare 可能会在您的客户端和nginx并使用您的 IP 而不是域名转发请求。因此循环仍然隐藏,但它仍然存在。

如果您想保留单个服务器块,您应该考虑将条件更改if为:

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

不过从清晰度和性能上来说,可能比两块还差server

相关内容