nginx Docker Laravel:重定向到https

0g0grzrc  于 2023-08-03  发布在  Nginx
关注(0)|答案(1)|浏览(106)

因此,我正在尝试dockerize我现有的laravel应用程序。这就是我正在做的。
dir结构如下:

phirater-l51
  .. <laravel applciation>
  Dockerfile
docker
  mysql
    init
      01-databases.sql
    my.cnf
  nginx
    default.conf
  docker-compose.yml

字符串
下面是我的docker-compose.yml文件:

version: "3.7"
services:
  app:
    container_name: app
    build:
      args:
        user: ${USER}
        uid: ${UID}
      context: ./../phirater-l51
      dockerfile: ./../phirater-l51/Dockerfile
    working_dir: /var/www/
    environment:
      - COMPOSER_MEMORY_LIMIT=-1
    depends_on:
      - db
    image: phirater-l51:latest
    volumes:
      - ./../phirater-l51:/var/www
    networks:
      - phios

  db:
    image: mysql:8.0
    container_name: db
    environment:
      MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
      MYSQL_DATABASE: ${MYSQL_DATABASE}
      MYSQL_USER: ${MYSQL_USER}
      MYSQL_PASSWORD: ${MYSQL_PASSWORD}
      SERVICE_TAGS: dev
      SERVICE_NAME: mysql
    volumes:
      - dbdata:/var/lib/mysql
      - ./mysql/my.cnf:/etc/mysql/my.cnf
      - ./mysql/init:/docker-entrypoint-initdb.d
    ports:
      - "3307:3306"
    networks:
      - phios

  nginx:
    container_name: nginx
    image: nginx:alpine
    ports:
      - "8005:80"
    depends_on:
      - app
      - db
    volumes:
      - ./../phirater-l51:/var/www
      - ./nginx/:/etc/nginx/conf.d
    networks:
      - phios

  cache:
    container_name: cache
    image: redis:alpine
    volumes:
      - cachedata:/data
    networks:
      - phios

networks:
  phios:
    driver: bridge

volumes:
  dbdata:
    driver: local
  cachedata:
    driver: local


下面是我的nginx/default.conf文件:

server {
    listen 80;
    index index.php index.html;
    error_log  /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;
    root /var/www/public;
    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass app:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
    location / {
        try_files $uri $uri/ /index.php?$query_string;
        gzip_static on;
    }
}


现在,当我运行容器时,它工作正常,但当我尝试从终端执行curl localhost:8005时,它返回以下内容:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8" />
        <meta http-equiv="refresh" content="0;url='https://localhost:8005'" />

        <title>Redirecting to https://localhost:8005</title>
    </head>
    <body>
        Redirecting to <a href="https://localhost:8005">https://localhost:8005</a>.
    </body>
</html>


当我执行curl -I时,我得到这些头文件:

HTTP/1.1 302 Found
Server: nginx/1.25.1
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/7.4.33
Cache-Control: no-cache, private
Date: Thu, 20 Jul 2023 06:44:34 GMT
Location: https://localhost:8005
Set-Cookie: laravel_session=<session>; path=/; secure; httponly


当我尝试在浏览器中执行此操作时,我会得到以下结果:
此站点无法提供安全连接localhost发送了无效的
响应. ERR_SSL_PROTOCOL_ERROR
我做错什么了?

cidc1ykv

cidc1ykv1#

我也在一个自定义的docker中使用Laravel。这里是我的http的NGINX配置的工作示例。工作正常,无需重定向。http://project.test/
这与Laravel的官方建议非常接近:https://laravel.com/docs/10.x/deployment#nginx

server {
    listen 80;
    server_name project.test;

    root /var/www/laravel/public;
    index index.php;

    error_log  /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-Content-Type-Options "nosniff";

    charset utf-8;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    error_page 404 /index.php;

    location ~ \.php$ {
        fastcgi_pass app:9000;

        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /\.(?!well-known).* {
        deny all;
    }
}

字符串
你有几个偏差,比如没有server_name。尝试添加它。
或者选择官方配置并检查问题是否消失。之后,您可以调整工作配置,直到您达到您的目标或问题。

相关问题