普通的 HTTP 请求已发送到 HTTPS 端口(Synology NAS 中的 NextCloud docker + nginx-proxy)

普通的 HTTP 请求已发送到 HTTPS 端口(Synology NAS 中的 NextCloud docker + nginx-proxy)

我有一台 Synology NAS DS1813+,我想在其中安装 Nextcloud,我设法使用 Docker 完成了此操作。请注意,它仅在本地网络中可用。

接下来,我希望使用 SSL 来保护它,但我无法做到这一点,因为 443 已被 Synology 占用。因此,我尝试使用 jwilder/nginx-proxy 反向代理。但是,我收到错误:400 Bad Request 普通 HTTP 请求已发送至 HTTPS 端口。我使用 (https://cavrnas:446/. 无需 https 即可访问 NextCloud,方法是使用http://cavrnas:8080,可以使用http://cavrnas:5000或者https://cavrnas:5001)。

请注意,该证书是使用 paulczar/omgwtfssl 生成的自签名证书。由于 443 已被 Synology 占用,因此我尝试改为监听端口 446 以进行 SSL。以下是我的 docker-compose.yml 文件(基于https://github.com/nextcloud/docker/tree/master/.examples/docker-compose/with-nginx-proxy/mariadb/apache):

version: '3'

services:
  db:
    image: mariadb
    command: --transaction-isolation=READ-COMMITTED --binlog-format=ROW
    restart: always
    container_name : nextcloud_db
    volumes:
      - /volume1/NextCloud_Data/MariaDB:/var/lib/mysql
    environment:
      - MYSQL_ROOT_PASSWORD=123
    env_file:
      - db.env

  app:
    build: .
    restart: always
    container_name : nextcloud_app
    volumes:
      - /volume1/NextCloud_Data/main:/var/www/html
      - /volume1/NextCloud_Data/apps:/var/www/html/custom_apps
      - /volume1/NextCloud_Data/config:/var/www/html/config
      - /volume1/NextCloud_Data/data:/var/www/html/data
      - /volume1/NextCloud_Data/theme:/var/www/html/themes/
      - /volume1/NextCloud_Data/db:/var/lib/mysql
      - /volume1/NextCloud_Data/apache2_log:/var/log/apache2
    environment:
      - VIRTUAL_HOST=cavrnas.local
      - MYSQL_HOST=db
      - CERT_NAME=cavrnas.local
    env_file:
      - db.env
    depends_on:
      - db
      - certs
    networks:
      - proxy-tier
      - default

  proxy:
    build: ./proxy
    restart: always
    container_name : nextcloud_proxy
    depends_on : 
      - certs
    ports:
      - 81:80
      - 446:443
    volumes:
      - /var/run/docker.sock:/tmp/docker.sock:ro
      - /volume1/NextCloud_Data/SSL:/etc/nginx/certs
      - /volume1/NextCloud_Data/nginx:/etc/nginx/conf.d
      - /volume1/NextCloud_Data/nginx_log:/var/log/nginx
    environment:
     - SSL_POLICY=Mozilla-Modern
     - HTTPS_METHOD=nohttp
     - VIRTUAL_PORT=446
    networks:
      - proxy-tier

  certs:
    container_name : nextcloud_cert
    environment:
      - CA_SUBJECT=ntu
      - CA_CERT=/certs/cavrnas.local.crt
      - CA_KEY=/certs/cavrnas.local.key
      - CA_EXPIRE=3650
      - SSL_SIZE=2048
      - SSL_SUBJECT=cavrnas.local
    image: paulczar/omgwtfssl
    volumes:
      - /volume1/NextCloud_Data/SSL:/certs
    networks:
      - proxy-tier


volumes:
  db:
  nextcloud:
  certs:
  vhost.d:
  html:

networks:
  proxy-tier:

以下是default.conf的内容。

# If we receive X-Forwarded-Proto, pass it through; otherwise, pass along the
# scheme used to connect to this server
map $http_x_forwarded_proto $proxy_x_forwarded_proto {
  default $http_x_forwarded_proto;
  ''      $scheme;
}
# If we receive X-Forwarded-Port, pass it through; otherwise, pass along the
# server port the client connected to
map $http_x_forwarded_port $proxy_x_forwarded_port {
  default $http_x_forwarded_port;
  ''      $server_port;
}
# If we receive Upgrade, set Connection to "upgrade"; otherwise, delete any
# Connection header that may have been passed to this server
map $http_upgrade $proxy_connection {
  default upgrade;
  '' close;
}
# Apply fix for very long server names
server_names_hash_bucket_size 128;
# Default dhparam
ssl_dhparam /etc/nginx/dhparam/dhparam.pem;
# Set appropriate X-Forwarded-Ssl header
map $scheme $proxy_x_forwarded_ssl {
  default off;
  https on;
}
gzip_types text/plain text/css application/javascript application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
log_format vhost '$host $remote_addr - $remote_user [$time_local] '
                 '"$request" $status $body_bytes_sent '
                 '"$http_referer" "$http_user_agent"';
access_log off;
                ssl_protocols TLSv1.3;
                ssl_ciphers HIGH:!aNULL:!MD5;
                ssl_prefer_server_ciphers off;
resolver 127.0.0.11;
# HTTP 1.1 support
proxy_http_version 1.1;
proxy_buffering off;
proxy_set_header Host $http_host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $proxy_connection;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $proxy_x_forwarded_proto;
proxy_set_header X-Forwarded-Ssl $proxy_x_forwarded_ssl;
proxy_set_header X-Forwarded-Port $proxy_x_forwarded_port;
# Mitigate httpoxy attack (see README for details)
proxy_set_header Proxy "";
server {
        server_name _; # This is just an invalid value which will never trigger on a real hostname.
        listen 80;
        access_log /var/log/nginx/access.log vhost;
        return 503;
}
# cavrnas.local
upstream cavrnas.local {
                                # Cannot connect to network of this container
                                server 127.0.0.1 down;
                                ## Can be connected with "docker-compose_proxy-tier" network
                        # nextcloud_app
                        server 172.18.0.2:80;
}
server {
        server_name cavrnas.local;
        listen 80 ;
        access_log /var/log/nginx/access.log vhost;
        return 301 https://$host$request_uri;
}
server {
        server_name cavrnas.local;
        listen 443 ssl http2 ;

        access_log /var/log/nginx/access.log vhost;
        ssl_session_timeout 5m;
        ssl_session_cache shared:SSL:50m;
        ssl_session_tickets off;
        ssl_certificate /etc/nginx/certs/cavrnas.local.crt;
        ssl_certificate_key /etc/nginx/certs/cavrnas.local.key;
        add_header Strict-Transport-Security "max-age=31536000" always;
        location / {
                proxy_pass http://cavrnas.local;
        }
}

请注意,也可以使用 cavrnas.local 来访问它。(可能是因为 Synology 中的服务器名称设置是 cavrnas。如果我 ping IP 地址,它会给我 cavrnas.local)。

我看到有些人建议将其放入 .conf 文件中以解决此类错误。

error_page 497 https://$host:$server_port$request_uri;

然而,当我这样做时,它会被重定向到 NAS Web UIhttps://cavrnas:5001

有人能解决这个问题吗?

谢谢!

相关内容