Nginx

Nginx Nextcloud 重定向太多

  • February 4, 2020

我正在嘗試在我的 Raspberry Pi 上安裝 Nextcloud。

  1. A record在 Cloudflare 中創建了一個指向我的 Raspberry Pi IP 地址的子域,NGINXcertbot使用/etc/nginx/sites-available/nextcloud.

  2. 我已經創建了符號連結/etc/nginx/sites-enables並檢查了NGINX配置,sudo nginx -t並且配置都是有效的。

  3. 我已成功重定向到,但是當我嘗試在瀏覽器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 -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.

您對兩者都有一個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塊中,但是如果您的伺服器直接使用. Cloudflare 可能會在您的客戶端和nginx之間放置另一個代理伺服器,並使用您的 IP 而不是域名轉發請求。所以循環仍然隱藏,但仍然存在。nextcloud.domain.com

如果要保留單個伺服器塊,則應考慮將if條件更改為:

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

但是,就清晰度和性能而言,它可能比兩個server塊差。

引用自:https://serverfault.com/questions/1001625