【问题标题】:The plain HTTP request was sent to HTTPS port error Docker nginx普通 HTTP 请求被发送到 HTTPS 端口错误 Docker nginx
【发布时间】:2021-02-21 04:23:03
【问题描述】:

我正在尝试通过http://localhost:8343/ 访问我的服务器,但它总是显示错误:

400 错误请求

普通的 HTTP 请求被发送到 HTTPS 端口

nginx/1.14.2

这是我对 docker-compose.yml 的配置。

/*other configurations*/
services:

    pages:
        build:
            context: .
            dockerfile: ./pages/Dockerfile
        container_name: proj_idcf_pages_1
        ports:
            - "8180:80"
            - "8343:443"
        environment:
            - TZ=Asia/Tokyo
            - LA_PROJECT_DIR=laravel
        depends_on:
            - php7
        volumes:
            - ../service/pages/:/var/www/
            - ./ssl_key/:/etc/nginx/ssl_key
        networks:
            - proj_default
/*other configurations*/

页面/Dockerfile

FROM centos:centos7
FROM nginx:1.14.2

COPY pages/default.conf /etc/nginx/conf.d/default.conf

RUN sed -i -e "s/access_log .*;$/access_log  \/dev\/stdout;/g" /etc/nginx/nginx.conf
RUN sed -i -e "s/error_log .*;$/error_log  \/dev\/stderr debug;/g" /etc/nginx/nginx.conf

CMD ["nginx", "-g", "daemon off;"]

pages/default.conf

server {
    listen 80;
    rewrite ^(.*)$ http://localhost:8343$1 permanent;
}

server {
    listen 8343 ssl;
    listen 443 ssl;
    index index.php;
    server_name localhost:8180;
    error_log  /dev/stderr debug;
    access_log  /dev/stdout;
    root /var/www/laravel/public;

    ssl_certificate /etc/nginx/ssl_key/localhost.pem;
    ssl_certificate_key /etc/nginx/ssl_key/localhost.key;

    location / {
            try_files $uri $uri/ /index.php$is_args$args;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass php7:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;

        add_header my_string 'start';
        add_header $request_filename $request_filename;
        add_header my_document_root $document_root;
        add_header fastcgi_script_name $fastcgi_script_name;
        add_header my_fastcgi_path_info $fastcgi_path_info;
        add_header my_string_2 ‘end’;
    }
/*other settings*/

有人可以帮我解决这个问题吗?我已经坚持了好几天了:(

【问题讨论】:

  • 在您的 docker-compose.yaml 端口部分中,您将主机的 8343 端口绑定到容器的 443 端口。容器中的 443 端口配置在 default.conf 上以侦听 https 流量。所以你要么访问http://localhost:8180要么https://localhost:8343
  • @zochamx 是的,我凭直觉做到了:) 现在我正在处理另一个问题。很抱歉回复和阅读您的评论晚了,每次我启动服务器时我都失去了连接。也许您可以将其作为答案,以便我可以接受它作为答案:)

标签: laravel docker nginx


【解决方案1】:

在你的docker-compose.yaml ->

ports:
   - "8180:80"  
   - "8343:443" <------ Here you bind the host port 8343 to container's port 443

在 nginx 配置文件中 ->

server {
    listen 8343 ssl;
    listen 443 ssl; <------ you configured that port to listen https traffic

所以你要么访问http://localhost:8180要么访问https://localhost:8343

【讨论】:

    【解决方案2】:

    Nginx 配置对我不起作用。 我有不安全的注册表配置,受反向代理 nginx 保护,它添加了 TLS 加密和基本身份验证。

    对我来说,解决方案是将“relativeurls: true”添加到注册表配置的 http 部分 /etc/docker/registry/config.yml

    ...
    http:
      addr: :5000
      relativeurls: true
      headers:
        X-Content-Type-Options: [nosniff]
    ...
    

    【讨论】:

      猜你喜欢
      • 2015-09-08
      • 2019-07-28
      • 2020-02-23
      • 1970-01-01
      • 2019-03-05
      • 1970-01-01
      • 1970-01-01
      • 2019-05-01
      • 2021-11-09
      相关资源
      最近更新 更多