Y
Y
yellow_pus2022-03-13 03:36:06
Nginx
yellow_pus, 2022-03-13 03:36:06

How to correctly specify the path to the project in the nginx config?

I'm trying to shove a Laravel project into the docker, I created all the necessary containers, each of which docker-compose upissues "ready to accept connection". But when I try to connect directly through the browser, I get an error 404 page not found. As far as I understand, it cannot find the index.php file in public/ and as far as I understand, this is due to the nginx container. Here are mine:
Docker-compose.yml (which is in the project, inside the _docker folder)

nginx:
    build:
      context: docker/nginx
    container_name: ${COMPOSE_PROJECT_NAME}.nginx
    networks:
      - frontend
      - backend
    env_file:
      - application.env
    restart: unless-stopped
    depends_on:
      - php-fpm
    volumes:
      - ../:/app/
      - ./docker/nginx/sites/:/etc/nginx/sites-available/

Dockerfile (which is located in _docker/nginx)
FROM nginx:alpine

ADD nginx.conf /etc/nginx/

RUN apk update \
    && apk upgrade \
    && apk add --no-cache bash \
    && adduser -D -H -u 1000 -s /bin/bash -Gwww-data www-data

CMD ["nginx"]

EXPOSE 80

app.conf (Which is in _docker/nginx/sites)
upstream php-upstream {
    server php-fpm:9000;
}

server {
    charset utf-8;
    client_max_body_size 128M;

    listen 80; ## listen for ipv4
    #listen [::]:80 default_server ipv6only=on; ## listen for ipv6

    server_name _;
    root     /app/public;
    index    index.php;

    access_log  /dev/stdout;
    error_log   /dev/sterr;

    location ~* \.(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc|eot|ttf|otf|woff|woff2|svg)$ {
        expires 1M;
        access_log off;
        add_header Cache-Control "public";
        add_header Access-Control-Allow-Origin *;
    }

    # CSS and Javascript
    location ~* \.(?:css|js)$ {
        expires 1y;
        access_log off;
        add_header Cache-Control "public";
    }

    rewrite ^/index.php(?:/)(.*)$ /$1  permanent;
    # rewrite ^/index.php$ /  permanent;
    if ($request_uri ~* "^/index\.php$") {
        return 301 /;
    }

    location / {
        # Redirect everything that isn't a real file to index.php
        try_files $uri $uri/ /index.php$is_args$args;
    }

    # uncomment to avoid processing of calls to non-existing static files by Yii
    #location ~ \.(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar)$ {
    #    try_files $uri =404;
    #}
    #error_page 404 /404.html;

    # deny accessing php files for the /assets directory
    location ~ ^/assets/.*\.php$ {
        deny all;
    }

    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_pass php-upstream;
        try_files $uri =404;
    }

    location ~* /\. {
        deny all;
    }
}

Please help to figure out which path and where I have indicated wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Karabanov, 2022-03-13
@yellow_pus

- ./docker/nginx/sites/:/etc/nginx/sites-available/

sites-available is Debian style, configs are placed in sites-available, and symlinks to them are placed in sites-enabled. So even if you used the Debian-based Nginx image, nothing would work.
Let's see what's in the image with Alpine:
docker run --rm -it nginx:alpine ls -1 /etc/nginx

conf.d
fastcgi.conf
fastcgi_params
mime.types
modules
nginx.conf
scgi_params
uwsgi_params

Yeah, conf.d means there is vanilla Nginx
docker run --rm -it nginx:alpine ls -1 /etc/nginx/conf.d/

default.conf

Here in conf.d you need to put the config instead of default.conf

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question