【问题标题】:nginx 403 Forbidden on laravel project with dockernginx 403 Forbidden on laravel project with docker
【发布时间】:2020-06-04 21:38:09
【问题描述】:

当我用php artisan serve 启动de Project 时一切正常,但是当我用docker-compose up -d 启动我的项目时出现错误:403 Forbidden nginx/1.10.3

Nginx 默认文件:

    listen [::]:80;
    listen 80;

    root /var/www/html/public;

    index index.html index.htm index.php;

    server_name {{getenv "NGINX_SERVER_NAME"}};

    server_tokens off;

    charset utf-8;

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

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

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/usr/local/var/run/php-fpm.sock;
    }

    error_page 404 /index.php;

    location ~ /\.ht {
        deny all;
    }

    add_header X-Served-By Bitpress.io;
    include h5bp/basic.conf;
}

这是我的 docker-compose 文件

docker-compose.yml

version: "3"
networks:
  app-tier:
    driver: bridge

services:
  app:
    image: test
    container_name: site
    build:
      context: .
      dockerfile: docker/Dockerfile
    networks:
      - app-tier
    env_file:
      - .docker.env
    ports:
      - 5050:80
    volumes:
      - .:/var/www/html
    environment:
      APP_ENV: local
      CONTAINER_ROLE: app

  scheduler:
    image: test
    container_name: scheduler
    depends_on:
      - app
    env_file:
      - .docker.env
    volumes:
      - .:/var/www/html
    environment:
      CONTAINER_ROLE: scheduler

  queue:
    image: test
    container_name: queue
    depends_on:
      - app
    env_file:
      - .docker.env
    volumes:
      - .:/var/www/html
    environment:
      CONTAINER_ROLE: queue

我已经看到,来自目录的权限是 root。 我尝试使用 commandRUN chown -R www-data:www-data /var/www/html 更改它,但它不起作用。

【问题讨论】:

  • 能否提供docker-compose.yml
  • 添加了撰写文件:)

标签: docker docker-compose nginx-config


【解决方案1】:

我只是更新你所拥有的,但不会 100% 解决你的问题,有些事情也没有完成,但没有所有信息我无法做更多。 您可能需要将 php-fpm 添加到您的 docker-compose.yml

nginx.conf

server {
    listen [::]:80;
    listen 80;

    # will be remove if you run everything inside container
    root /var/www/html/public;

    # will be remove if you run everything inside container
    index index.html index.htm index.php;

    server_name {{getenv "NGINX_SERVER_NAME"}};

    server_tokens off;

    charset utf-8;

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

    # will be remove
    # location / {
    #     try_files $uri $uri/ /index.php$is_args$args;
    # }

    # Add this, now nginx only redirect request to expose socket from docker
    location / {
        proxy_pass          http://localhost:5050
        proxy_ser_header    X-Served-By Bitpress.io;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/usr/local/var/run/php-fpm.sock;
    }

    # will be remove if you run everything inside container
    error_page 404 /index.php;

    location ~ /\.ht {
        deny all;
    }

    # will be remove if you run everything inside container
    add_header X-Served-By Bitpress.io;

    include h5bp/basic.conf;
}

docker-compose.yml

version: "3"

networks:
  app-tier:
    driver: bridge

services:
  app:
    image: test
    container_name: site
    build:
      context: .
      dockerfile: docker/Dockerfile
    networks:
      - app-tier
    env_file:
      - .docker.env
    ports:
      - 5050:80
    volumes:
      - .:/var/www/html
      # - /absolute/path/better:/var/www/html
    environment:
      APP_ENV: local
      CONTAINER_ROLE: app

  scheduler:
    image: test
    container_name: scheduler
    networks:       # <-- add thisadd this
      - app-tier    # <-- add thisadd this
    depends_on:
      - app
    env_file:
      - .docker.env
    volumes:
      - .:/var/www/html
      # - /absolute/path/better:/var/www/html
    environment:
      CONTAINER_ROLE: scheduler

  queue:
    image: test
    container_name: queue
    networks:       # <-- add thisadd this
      - app-tier    # <-- add thisadd this
    depends_on:
      - app
    env_file:
      - .docker.env
    volumes:
      - .:/var/www/html
      # - /absolute/path/better:/var/www/html
    environment:
      CONTAINER_ROLE: queue

您可能在env_file:CONTAINER_ROLE 之间有一个问题,谁拥有优先权:您的 3 个容器共享耻辱.docker.env 这可能是一个问题。最好有:

.docker.app.env
.docker.scheduler.env
.docker.queue.env

【讨论】:

  • 感谢您的帮助,我忘记了 Dockerfile 行 xD
猜你喜欢
  • 2016-01-31
  • 1970-01-01
  • 2013-11-02
  • 2012-08-21
  • 1970-01-01
  • 1970-01-01
  • 2020-06-27
  • 2021-09-06
  • 1970-01-01
相关资源
最近更新 更多