【问题标题】:Laravel docker compose nginx 404 Not FoundLaravel docker compose nginx 404 Not Found
【发布时间】:2021-09-02 09:11:16
【问题描述】:

我遇到了奇怪的行为并且不明白需要解决什么问题,我有 nginx 的 laravel 并且由于某种原因遇到了 404 Not Found

所以,我的码头工人撰写

version: '3.7'

services:
  nginx-laratest:
    build:
      context: .
      dockerfile: ./nginx/Dockerfile
    restart: on-failure
    volumes:
      - "../:/var/www"
    ports:
      - "80:80"
      - "443:443"
    depends_on:
      - php-laratest
    networks:
      - nginx-laratest-networks
      - php-laratest-networks

  php-laratest:
    build:
      context: .
      dockerfile: ./php/Dockerfile
    restart: on-failure
    volumes:
      - "../:/var/www"
      - "./php/php.ini:/usr/local/etc/php/conf.d/custom.ini"
      - "./data/php:/var/laratest"
    networks:
      - php-laratest-networks

networks:
  php-laratest-networks:
  nginx-laratest-networks:

和 docker/nginx/Dockerfile

# Use Alpine Linux
FROM alpine:latest

RUN apk add --update --no-cache nginx

# Timezone
ENV TIMEZONE Europe/Kiev

COPY nginx/zt.conf /etc/nginx/conf.d/
COPY nginx/zt.conf /etc/nginx/sites-enable/

RUN adduser -D -g '' -G www-data www-data
RUN mkdir -p /run/nginx


#CMD ["nginx"]
CMD [ "nginx", "-g", "daemon off;" ]


EXPOSE 80
EXPOSE 443

一个 docker/nginx/zt.conf

server {
    server_name laratest.local.com;
    listen 80;

    root /var/www/public;
    index index.php index.html index.htm;

    location /storage/ {
        alias /var/www/storage/app/public/;
        autoindex off;
    }

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

    location ~ \.php$ {
        try_files $uri /index.php =404;
        client_max_body_size 50m;

        fastcgi_pass php-laratest:9000;
        fastcgi_buffers 16 16k;
        fastcgi_buffer_size 32k;
        fastcgi_read_timeout 3600;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME /var/www/public/index.php;
        fastcgi_param PHP_VALUE "error_log=/var/log/nginx/php-laratest_errors.log";
    }

    access_log /var/log/nginx/php-laratest_service.access.log;
    error_log  /var/log/nginx/php-laratest_service.error.log  crit;


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

我将laratest.local.com 添加到我的主机127.0.0.1 laratest.local.com 并尝试打开 - 面对

404 Not Found
nginx

在 nginx 容器中我只有两个文件 error.log 是空的,access.log 由每个请求填充

/var/www/public # tail -f /var/log/nginx/
access.log  error.log

docker compose ps 是这样的

ivan@ivan-laptop:~/hosts/test_laravel/docker$ docker-compose ps
         Name                        Command               State                                   Ports                                 
-----------------------------------------------------------------------------------------------------------------------------------------
docker_nginx-laratest_1   nginx -g daemon off;             Up      0.0.0.0:443->443/tcp,:::443->443/tcp, 0.0.0.0:80->80/tcp,:::80->80/tcp
docker_php-laratest_1     bash /usr/local/bin/docker ...   Up      9000/tcp   

在 nginx 容器中,我检查了应用程序的路径,看起来是正确的

ivan@ivan-laptop:~/hosts/test_laravel/docker$ docker-compose exec nginx-laratest ash
/ # cd /var/www/public/
/var/www/public # ls
favicon.ico  index.php    robots.txt   web.config
/var/www/public # ls -la
total 24
drwxrwxr-x    2 www-data 1000          4096 Sep  2 08:44 .
drwxrwxr-x   16 www-data 1000          4096 Sep  2 08:10 ..
-rw-rw-r--    1 www-data 1000           603 Sep  2 07:19 .htaccess
-rw-rw-r--    1 www-data 1000             0 Sep  2 07:19 favicon.ico
-rw-rw-r--    1 www-data 1000          1984 Sep  2 08:44 index.php
-rw-rw-r--    1 www-data 1000            24 Sep  2 07:19 robots.txt
-rw-rw-r--    1 www-data 1000          1183 Sep  2 07:19 web.config
/var/www/public # 

我检查了 nginx 版本

/var/www/public # nginx -v
nginx version: nginx/1.20.1

我在 nginx continer 中检查了 conf 文件 docker-compose exec nginx-laratest ash

/var/log/nginx # cd /etc/nginx/
conf.d/        http.d/        modules/       sites-enable/
/var/log/nginx # cat /etc/nginx/conf.d/zt.conf 
server {
    server_name laratest.local.com;
    listen 80;

    root /var/www/public;
    index index.php index.html index.htm;


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

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass php-laratest:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }

    access_log /var/log/nginx/php-laratest_service.access.log;
    error_log  /var/log/nginx/php-laratest_service.error.log  crit;


    location ~ /\.ht {
        deny all;
    }
}
/var/log/nginx # cat /etc/nginx/s
scgi_params    sites-enable/
/var/log/nginx # cat /etc/nginx/s
scgi_params    sites-enable/
/var/log/nginx # cat /etc/nginx/sites-enable/zt.conf 
server {
    server_name laratest.local.com;
    listen 80;

    root /var/www/public;
    index index.php index.html index.htm;


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

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass php-laratest:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }

    access_log /var/log/nginx/php-laratest_service.access.log;
    error_log  /var/log/nginx/php-laratest_service.error.log  crit;


    location ~ /\.ht {
        deny all;
    }
}
/var/log/nginx # 

【问题讨论】:

    标签: laravel docker nginx devops alpine


    【解决方案1】:

    我对@9​​87654321@ 的了解很少,但我认为您的问题是您忘记将docker/nginx/zt.conf 文件复制到您的nginx-laratest 容器中。您可以通过进入 docker 容器内部并执行 ls -la /etc/nginx/sites-available 来验证这一点。如果zt.conf 不在列表中,则说明您忘记复制了。

    要解决这个问题,您必须将文件从本地机器映射到 docker 容器中:

    volumes:
          - ./:/var/www
          - ./docker/nginx/zt.conf:/etc/nginx/sites-available/zt.conf
    

    【讨论】:

    • 文件被复制到 docker/nginx/Dockerfile COPY nginx/zt.conf /etc/nginx/conf.d/ COPY nginx/zt.conf /etc/nginx/sites-enable/ 我更新了问题
    【解决方案2】:

    我在将 nginx 映像更改为时修复了它 FROM nginx:stable-alpine

    【讨论】:

      猜你喜欢
      • 2021-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-12
      • 2020-10-21
      • 1970-01-01
      • 2022-08-02
      • 2021-06-03
      相关资源
      最近更新 更多