简体   繁体   中英

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

Nginx default file:

    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.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. I have tried to change it with the command RUN chown -R www-data:www-data /var/www/html but it not works.

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. You may need to add php-fpm into your 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

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. it may be a good idead to have:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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