【问题标题】:Laravel running on Docker is not sending any set-cookie in HTTP response在 Docker 上运行的 Laravel 没有在 HTTP 响应中发送任何 set-cookie
【发布时间】:2020-08-05 09:23:08
【问题描述】:

Laravel 不会发送 Set-cookie 响应,因此 Laravel 会话 cookie 不会存储在浏览器中。会话设置为文件适配器,并使用正确的权限成功创建文件。

我已将 Laravel 会话配置设置为 Laravel 附带的默认配置。

我正在使用 Laravel 框架 5.5.48。

谁能提供帮助?

docker-compose.yml

version: '3'
services:
  app:
    build:
      context: ./
      dockerfile: app.dockerfile
    working_dir: /var/www
    volumes:
      - ./src:/var/www
    environment:
      - "DB_PORT=3306"
      - "DB_HOST=database"
    ports:
      - "9000:9000"
    networks:
      - t-network
  web:
    build:
      context: ./
      dockerfile: web.dockerfile
    working_dir: /var/www
    volumes:
      - ./src:/var/www
    ports:
      - "80:80"
      - "443:443"
    networks:
      - t-network

  database:
    image: mysql:5.7
    volumes:
      - dbdata:/var/lib/mysql
    environment:
      - "MYSQL_DATABASE=t"
      - "MYSQL_USER=homestead"
      - "MYSQL_PASSWORD=secret"
      - "MYSQL_ROOT_PASSWORD=secret"
    ports:
      - "33060:3306"
    networks:
      - t-network

volumes:
  dbdata:

networks:
  t-network:

vhost.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;
    }
}

app.dockerfile

FROM php:7.2-fpm

# Copy composer.lock and composer.json
COPY ./src/composer.json /var/www/

# Set working directory
WORKDIR /var/www

# Install dependencies
RUN apt-get update && apt-get install -y \
    build-essential \
    libpng-dev \
    libjpeg62-turbo-dev \
    libfreetype6-dev \
    locales \
    zip \
    jpegoptim optipng pngquant gifsicle \
    vim \
    unzip \
    git \
    curl \
    xvfb \
    libfontconfig \
    wkhtmltopdf

# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*

# Install extensions
RUN docker-php-ext-install pdo_mysql mbstring zip exif pcntl
RUN docker-php-ext-configure gd --with-gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ --with-png-dir=/usr/include/
RUN docker-php-ext-install gd

# Install composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

# Add user for laravel application
RUN groupadd -g 1000 www
RUN useradd -u 1000 -ms /bin/bash -g www www

# Copy existing application directory contents
COPY ./src /var/www

# Copy existing application directory permissions
COPY --chown=www:www . /var/www
RUN chown -R www:www /tmp
RUN chmod -R 777 /tmp

# Change current user to www
USER www

# Expose port 9000 and start php-fpm server
EXPOSE 9000
CMD ["php-fpm"]

web.docker 文件

FROM nginx:1.17

ADD vhost.conf /etc/nginx/conf.d/default.conf

【问题讨论】:

  • 您能提供您的 nginx 或 apache 配置吗?
  • 你的 docker 配置呢?没有它,我们无法提供任何解决方案。
  • @nmfzone 我已经添加了配置 - 谢谢!
  • 这个问题有解决办法吗?

标签: php laravel docker cookies


【解决方案1】:

我也有同样的问题。 当我在我的应用程序容器中执行 -it 并运行“php artisan serve”和 curl -v 我的登录 URL 时,我无法获得正确的 set-cookie 标头集的响应。 所以我知道这与 Nginx 无关。

我找到了一些地方可以查看标题是如何设置的。

1.vi vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/VerifyCsrfToken.php
addCookieToResponse

2.vi vendor/laravel/framework/src/Illuminate/Session/Middleware/StartSession.php
addCookieToResponse

3.vi vendor/symfony/http-foundation/Response.php
prepare
sendHeaders

我发现 sendHeaders 中的 headers_sent 调用是 true 并且它直接返回。 所以我把参数放在 headers_sent 中,发现错误是我的一个路由文件中的第 1 行有一个空行,这很荒谬,而相同的源代码在其他非常健康的 VM 上运行,不需要关注没有错误!而且只是在docker中,它不起作用。

在我删除空行之后。我可以恢复我的 APP_sessionSESSION-TOKEN !!! 我不太确定是否有像javascript这样的“严格”模式,因为我没有导出PHP。我确定真正的原因不是路由文件顶部的空行。

【讨论】:

  • 我还有一个问题。 /login 的 Http Post 得到 200 响应。正常的 VM 部署应用程序将返回 302 响应。尽管如此,修改 headers_sent,发现我的代码中有一个 var_dump,这使得在我真正的 302 响应之前对代码 200 的响应更快。
  • 好的,所以对我来说,在项目的一个文件中
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-09-15
  • 1970-01-01
  • 1970-01-01
  • 2015-04-18
  • 2014-02-26
  • 2021-09-03
  • 1970-01-01
相关资源
最近更新 更多