Docker ngix/traefik 301 在本地主机中将 http 重定向到 https

Docker ngix/traefik 301 redirecting http to https in localhost

这是对 的跟进,提供了更多信息。我还没弄明白

我在 Docker 松弛组中询问,他们确信它来自 nginx 或 traefik 配置。

在 Firefox 中有一个 SSL_ERROR_UNRECOGNIZED_NAME_ALERT 错误,在 Chrome 中是类似的 ERR_SSL_UNRECOGNIZED_NAME_ALERT。我没有通过搜索找到太多关于其中任何一个的信息。

我的 nginx 配置:

user                                    nginx;
daemon                                  off;
worker_processes                        auto;
error_log                               /proc/self/fd/2 debug;

events {
  worker_connections                  1024;
  multi_accept                        on;
}

http {
  include                             /etc/nginx/mime.types;
  default_type                        application/octet-stream;
  fastcgi_buffers                     16 32k;
  fastcgi_buffer_size                 32k;
  fastcgi_intercept_errors            on;
  fastcgi_read_timeout                900;
  include                             fastcgi_params;
  access_log                          /proc/self/fd/1;
  port_in_redirect                    off;
  send_timeout                        600;
  sendfile                            on;
  client_body_timeout                 600;
  client_header_timeout               600;
  client_max_body_size                256M;
  client_body_buffer_size             16K;
  client_header_buffer_size           4K;
  large_client_header_buffers         8 16K;
  keepalive_timeout                   60;
  keepalive_requests                  100;
  reset_timedout_connection           off;
  tcp_nodelay                         on;
  tcp_nopush                          on;
  server_tokens                       off;
  upload_progress                     uploads 1m;

  gzip                                on;
  gzip_buffers                        16 8k;
  gzip_comp_level                     2;
  gzip_http_version                   1.1;
  gzip_min_length                     20;
  gzip_types                          text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript image/x-icon application/vnd.ms-fonto
  gzip_vary                           on;
  gzip_proxied                        any;
  gzip_disable                        msie6;

  add_header                          X-XSS-Protection '1; mode=block';
  add_header                          X-Frame-Options SAMEORIGIN;
  add_header                          X-Content-Type-Options nosniff;

  map $http_x_forwarded_proto $fastcgi_https {
      default $https;
      http '';
      https on;
  }

  map $uri $no_slash_uri {
      ~^/(?<no_slash>.*)$ $no_slash;
  }


  upstream backend {
      server php:9000;
  }


  include conf.d/*.conf;
}

我的nginx.conf.default:

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

我的docker-compose.yml和上一题没有变化

我一直在寻找类似 traefik 配置的东西,但找不到任何东西。

到目前为止我尝试过的事情:

我真的很茫然,感谢任何帮助。

经过 OP 的更多测试和其他用户的评论:重定向(HTTP 到 HTTPS)似乎是在 Nginx 处理请求之后发生的。

OP 还使用单个 index.html 文件进行了测试,并且未重定向到 HTTPS:确认重定向来自 PHP(或至少不是来自 Nginx)。

接下来的步骤是查看 Drupal 配置,and/or htaccess 配置。 OP 更改了一些 Drupal 配置(关于重定向),并成功获得了仅使用 HTTP 的 drupal 设置页面。

在这种情况下,最好始终尝试查明问题的来源:

  • 使您的 Nginx 配置最少:简单 index.html
  • 定期清除浏览器缓存:它们有时会缓存重定向
  • Check/remove htaccess 查看行为是否改变
  • 最后,如果 Nginx 没有任何问题,并且 htaccess 似乎不是问题所在:它主要是“之后”,所以问题可能来自“Nginx 向谁发送请求”。 =25=]
  • 来自“大型”frameworks/CMS,例如 Drupal、Woocommerce、Laravel... 重定向通常可以从配置文件或数据库设置中“轻松”处理。
  • 当您有自定义代码处理重定向时:需要调试