简体   繁体   English

nginx 403 Forbidden on laravel project with docker

[英]nginx 403 Forbidden on laravel project with docker

When I start de Project with php artisan serve everything works fine, but when I start my project with docker-compose up -d there is an error: 403 Forbidden nginx/1.10.3当我使用php artisan serve启动 de Project 时一切正常,但是当我使用docker-compose up -d启动我的项目时出现错误: 403 Forbidden nginx/1.10.3

Nginx default file: 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;
}

and here is my docker-compose File这是我的docker-compose文件

docker-compose.yml 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

I've seen, that the Permissions from the Directories is root.我已经看到,来自目录的权限是 root。 I have tried to change it with the command RUN chown -R www-data:www-data /var/www/html but it not works.我试图用命令RUN chown -R www-data:www-data /var/www/html改变它,但它不起作用。

I just update what you have, but won't fix 100% your issues, some stuff have ot be done too, but without all information I cannot do more.我只是更新你所拥有的,但不会 100% 解决你的问题,有些东西也没有完成,但没有所有信息我不能做更多。 You may need to add php-fpm into your docker-compose.yml您可能需要将 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 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

You may have an issues between env_file: and CONTAINER_ROLE who have the priority: your 3 containers share the shame .docker.env it may be an issues.您可能在env_file:和具有优先权的CONTAINER_ROLE之间env_file:问题:您的 3 个容器共享耻辱.docker.env这可能是一个问题。 it may be a good idead to have:最好有:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM