Score:0

Docker Nginx serves all static content (none index.php) as 403 Forbidden

ba flag

I'am trying to convert my personal development setup from local php/ nginx setup to a docker setup. Almost everything is working great, the only issue I'am having right now is that all static content (*.css/*.js/*.jpg etc) is responding in a 403 forbidden. I have been scrolling and trying solution of the inter net the whole day, but non seem to be working. Most found issues had answers that the container user should be set to another user (uid 1000 in my case). Doing this results in Permission denied 13 error on nginx container boot.

Hope somebody here has the solution so I can finalize my new local setup.

Host: Linux OS (popOS)

Containers used:

  • NGINX - nginx:latest
  • PHP - php:8.2-fpm

For both containers I created a dockerfile.

When accessing the localhost page, everything except local static files work fine. So localhost:3030 loads the index.php files etc. Only when trying to access for example localhost:3030/dist/images/test.png I get a forbidden error.

Now I take it that this means nginx has no access to the files. I only don't see how to give this access. Because this is a local dev environment I am not copying all files in the container only using a volume bind to the project folder in a docker-compose.yml, and the same volume binding in the php container.

I hope I am missing something stupid, because it feels this should be working.

Project files example:

/public/
/public/index.php
/public/dist/
/public/dist/images/test.png

Docker files:

Below the setup of the docker files at this moment.

docker-compose.yml:

version: '3.7'
services:
    ngnx:
        build: ./nginx/
        container_name: nginx-container
        restart: unless-stopped
        links:
            - php
        volumes:
            - ./www/html/:/var/www/html/
            - ./www/logs/:/var/log/nginx/
    php:
        build: ./php/
        container_name: php-container
        restart: unless-stopped
        expose:
            - 9000
        volumes:
            - ./www/html/:/var/www/html/

./nginx/Dockerfile

FROM nginx:latest
COPY ./default.conf /etc/nginx/conf.d/default.conf

./php/Dockerfile

FROM php:8.2-fpm
RUN apt-get update -y && apt-get upgrade -y

./nginx/default.conf

Very basic nginx file because I first want a fully working environment before starting to tweak this.

server {
     listen 80 default_server;
     root /var/www/html/public;
     index index.php;

     charset utf-8;

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

     access_log /var/log/nginx/error.log;
     error_log /var/log/nginx/error.log error;
     sendfile off;

     client_max_body_size 100m;

     location ~ .php$ {
        fastcgi_split_path_info ^(.+.php)(/.+)$;
        fastcgi_pass php-container:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_intercept_errors off;
        fastcgi_buffer_size 16k;
        fastcgi_buffers 4 16k;
    }
}
vidarlo avatar
ar flag
Well, how does your configuration *actually* look?
WiZu avatar
ba flag
Your right, I did some updating on my post/ question. It now contains the files/ contents.
Reishin avatar
sa flag
@WiZu check access logs. It most likely file permissions issue for the static files . + its not a good idea to use same file for error and access logs
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.