“ GET /index.php” Dockerize Laravel应用时出现404 [英] "GET /index.php" 404 when dockerize Laravel app
问题描述
我尝试了一个非常小的设置,以对Laravel应用进行Docker化。即使Nginx能够将请求转发到Laravel容器,我仍然得到 GET /index.php 404
C:yocto\snapweb> docker-compose日志--follow laravel
附加到snapweb_laravel_1
laravel_1 | [23-Jul-2018 07:10 :04]注意:fpm正在运行,pid 1
laravel_1 | [23-Jul-2018 07:10:04]注意:准备处理连接
laravel_1 | 172.18.0.3-23 / Jul / 2018 :07:14:01 +0000 GET /index.php 404
当我进入laravel时
内部Laravel容器
/ var / www / html#pwd
/ var / www / html
/ var / www / html#ls -al
总计224
drwxr- xr-x 1个根目录4096 Jul 23 07:09。
drwxr-xr-x 1个根目录4096 Jul 21 08:11 ..
-rwxr-xr-x 1个根目录668 Jul 23 07 :08 Dockerfile
drwxr-xr-x 6根根4096 5月8日19:42 app
-rwxr-xr-x 1 root root 1686 May 8 19:42 artisan
drwxr-xr-x 3 root root 4096 May 8 19:42 bootstrap
-rw-r-- r-- 1个根目录1477 5月8日19:42 composer.json
-rw-r--r-- 1个根目录144199 Jul 23 07:03 composer.lock
drwxr-xr-x 2 root root 4096 May 8 19:42 config
drwxr-xr-x 5 root root 4096 May 8 19:42 database
-rw-r--r-- 1 root root 1022 May 8 19:42 package.json
-rw-r--r-- 1个根root 1134年5月8日19:42 phpunit.xml
drwxr-xr-x 4个根4096 5月8日19:42 public
-rw-r--r-- 1个根目录3675年5月8日19:42 readme.md
drwxr-xr-x 5个根目录4096 5月8日19:42资源
drwxr-xr-x 2根root 4096年5月8日19:42路由
-rw-r--r-- 1根root 563 5月8日19:42 server.php
drwxr-xr-x 5根root 4096 5月8日19 :42存储
drwxr-xr-x 4根root 4096 5月8日19:42测试
drwxr-xr-x 37根root 4096 Jul 23 07:03供应商
-rw-r- -r-- 1个根root 549年5月8日19:42 webpack.mix.js
/ var / www / html#ls -al public
total 32
drwxr-xr-x 4 root根4096年5月8日19:42。
drwxr-xr-x 1个根目录4096 Jul 23 07:09 ..
-rw-r--r-- 1个根目录593 May 8 19:42 .htaccess
drwxr- xr-x 2根root 4096年5月8日19:42 css
-rw-r--r-- 1根0年5月8日19:42 favicon.ico
-rw-r--r- -1根root 1823年5月8日19:42 index.php
drwxr-xr-x 2根root 4096 5月8日19:42 js
-rw-r--r-- 1根root 5月24日8 19:42 robots.txt
-rw-r--r-- 1个根root 914 May 8 19:42 web.config
/ var / www / html#
我可以知道我的设置有什么问题吗?这是我的简约Dockerfiles
laravel / Dockerfile
从php:7.2.8-fpm-alpine3.7
RUN apk更新&& b
apk添加git&& \
apk添加解压缩
RUN curl -sS https://getcomposer.org/installer | php---install-dir = / usr / local / bin --filename = composer
#将laravel放入一个临时文件夹。
RUN composer create-project --prefer-dist laravel / laravel laravel-install-tmp
#将本地主机文件移动到Docker容器中。
COPY。 / var / www / html
#复制所有laravel文件,而不覆盖主机文件。
RUN假| cp -ai / var / www / html / laravel-install-tmp / * / var / www / html / 2> / dev / null
#删除临时文件夹。
运行rm -rf / var / www / html / laravel-install-tmp /
WORKDIR / var / www / html
CMD [ php-fpm , -F]
nginx / default.conf
服务器{
监听443;
ssl;
ssl_certificate /app/cert.pem;
ssl_certificate_key /app/key.pem;
位置/ {
try_files $ uri /index.php?$args;
}
location〜\.php $ {
fastcgi_split_path_info ^(。+ \.php)(/.+)$;
fastcgi_pass laravel:9000;
fastcgi_index index.php;
包括fastcgi_params;
fastcgi_param SCRIPT_FILENAME $ document_root $ fastcgi_script_name;
fastcgi_param PATH_INFO $ fastcgi_path_info;
}
}
nginx / Dockerfile
从Nginx:1.13.9-alpine
COPY。 / app
运行rm /etc/nginx/conf.d/default.conf
COPY default.conf /etc/nginx/conf.d/
docker-compose.yml
版本:'2'
服务:
#注意,SSL证书(cert.pem和key.pem)是从cloudflare生成的。
nginx:
构建:
上下文:./nginx
dockerfile:Dockerfile
重新启动:始终
端口:
- 2053:443
laravel:
构建:
上下文:./laravel
dockerfile:Dockerfile
重新启动:始终
PHP-FPM
我认为您缺少 php-fpm.conf 配置文件,我的文件看起来像是标准文件,但进行了一些更改:
-
error_log = / proc / self / fd / 2
允许您使用docker日志
-
daemonize = no
-
include = / etc / php / 7.1 / fpm / php-fpm.d / www.conf
www.conf
我包含在 php-fpm.conf
标准之一的底部更改:
-
听= 9000
Dockerfile
:
很明显,我复制了php-fpm.conf和www.conf(我使用 php:7.1.14-fpm-jessie
)
...安装&清理步骤
复制php-fpm.conf /etc/php/7.1/fpm/php-fpm.conf
复制www.conf /etc/php/7.1/fpm/php-fpm.d/www .conf
用于php-fpm的Dockerfile的结尾看起来像:
CMD [ php-fpm, --fpm-config, /etc/php/7.1/fpm/php-fpm.conf]
NGINX
$ b我通过Dockerfile复制的$ b
默认
:
服务器{
监听80 default_server;
root / var / www / app / public;
index index.html index.htm index.php;
server_name _;
charset utf-8;
位置= /favicon.ico {log_not_found off; access_log关闭; }
location = /robots.txt {log_not_found已关闭; access_log关闭; }
位置/ {
try_files $ uri $ uri / /index.php$is_args$args;
}
location〜\.php $ {
fastcgi_split_path_info ^(。+ \.php)(/.+)$;
fastcgi_pass pn-php-fpm:9000;
fastcgi_index index.php;
包括fastcgi_params;
fastcgi_param SCRIPT_FILENAME $ document_root $ fastcgi_script_name;
}
error_page 404 /index.php;
位置〜/\.ht {
拒绝全部;
}
}
NGINX的 Dockerfile
:
FROM nginx
RUN echo daemon off; >> /etc/nginx/nginx.conf
复制默认/etc/nginx/conf.d/default.conf
#将请求和错误日志转发到docker日志收集器
运行ln -sf / dev / stdout /var/log/nginx/access.log \
&& ln -sf / dev / stderr /var/log/nginx/error.log
CMD [ nginx]
Docker组成部分
在编写时需要共享卷;我的意思是,NGINX + php-fpm必须能够读取您的应用程序代码
卷:
-无论您/您的/代码/在/在/主机上:/ var / www / app
< blockquote>
注意:这个答案是WIP,我将定期编辑/评论以帮助OP。
I tried to have a very minimal setup, to dockerize Laravel app. Even the nginx able to forward the request to Laravel container, I'm still getting GET /index.php" 404
C:\yocto\snapweb>docker-compose logs --follow laravel
Attaching to snapweb_laravel_1
laravel_1 | [23-Jul-2018 07:10:04] NOTICE: fpm is running, pid 1
laravel_1 | [23-Jul-2018 07:10:04] NOTICE: ready to handle connections
laravel_1 | 172.18.0.3 - 23/Jul/2018:07:14:01 +0000 "GET /index.php" 404
When I went inside laravel container, this is how it looks like. Seem fine to me.
Inside Laravel container
/var/www/html # pwd
/var/www/html
/var/www/html # ls -al
total 224
drwxr-xr-x 1 root root 4096 Jul 23 07:09 .
drwxr-xr-x 1 root root 4096 Jul 21 08:11 ..
-rwxr-xr-x 1 root root 668 Jul 23 07:08 Dockerfile
drwxr-xr-x 6 root root 4096 May 8 19:42 app
-rwxr-xr-x 1 root root 1686 May 8 19:42 artisan
drwxr-xr-x 3 root root 4096 May 8 19:42 bootstrap
-rw-r--r-- 1 root root 1477 May 8 19:42 composer.json
-rw-r--r-- 1 root root 144199 Jul 23 07:03 composer.lock
drwxr-xr-x 2 root root 4096 May 8 19:42 config
drwxr-xr-x 5 root root 4096 May 8 19:42 database
-rw-r--r-- 1 root root 1022 May 8 19:42 package.json
-rw-r--r-- 1 root root 1134 May 8 19:42 phpunit.xml
drwxr-xr-x 4 root root 4096 May 8 19:42 public
-rw-r--r-- 1 root root 3675 May 8 19:42 readme.md
drwxr-xr-x 5 root root 4096 May 8 19:42 resources
drwxr-xr-x 2 root root 4096 May 8 19:42 routes
-rw-r--r-- 1 root root 563 May 8 19:42 server.php
drwxr-xr-x 5 root root 4096 May 8 19:42 storage
drwxr-xr-x 4 root root 4096 May 8 19:42 tests
drwxr-xr-x 37 root root 4096 Jul 23 07:03 vendor
-rw-r--r-- 1 root root 549 May 8 19:42 webpack.mix.js
/var/www/html # ls -al public
total 32
drwxr-xr-x 4 root root 4096 May 8 19:42 .
drwxr-xr-x 1 root root 4096 Jul 23 07:09 ..
-rw-r--r-- 1 root root 593 May 8 19:42 .htaccess
drwxr-xr-x 2 root root 4096 May 8 19:42 css
-rw-r--r-- 1 root root 0 May 8 19:42 favicon.ico
-rw-r--r-- 1 root root 1823 May 8 19:42 index.php
drwxr-xr-x 2 root root 4096 May 8 19:42 js
-rw-r--r-- 1 root root 24 May 8 19:42 robots.txt
-rw-r--r-- 1 root root 914 May 8 19:42 web.config
/var/www/html #
May I know what's wrong with my setup? Here's my minimalist Dockerfiles
laravel/Dockerfile
FROM php:7.2.8-fpm-alpine3.7
RUN apk update && \
apk add git && \
apk add unzip
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
# Get laravel into a temporary folder.
RUN composer create-project --prefer-dist laravel/laravel laravel-install-tmp
# Move local host files to docker container.
COPY . /var/www/html
# Copy all laravel files, without overwrite files from host.
RUN false | cp -ai /var/www/html/laravel-install-tmp/* /var/www/html/ 2>/dev/null
# Remove temporary folder.
RUN rm -rf /var/www/html/laravel-install-tmp/
WORKDIR /var/www/html
CMD ["php-fpm", "-F"]
nginx/default.conf
server {
listen 443;
ssl on;
ssl_certificate /app/cert.pem;
ssl_certificate_key /app/key.pem;
location / {
try_files $uri /index.php?$args;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass laravel:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
nginx/Dockerfile
FROM nginx:1.13.9-alpine
COPY . /app
RUN rm /etc/nginx/conf.d/default.conf
COPY default.conf /etc/nginx/conf.d/
docker-compose.yml
version: '2'
services:
# Note, SSL cert (cert.pem and key.pem) is generated from cloudflare.
nginx:
build:
context: ./nginx
dockerfile: Dockerfile
restart: always
ports:
- "2053:443"
laravel:
build:
context: ./laravel
dockerfile: Dockerfile
restart: always
PHP-FPM
I think you are missing the php-fpm.conf configuration file, mine looks just like a standard one with some changes:
error_log = /proc/self/fd/2
allows you to usedocker logs
daemonize = no
include=/etc/php/7.1/fpm/php-fpm.d/www.conf
www.conf
which I include in the bottom of php-fpm.conf
standard one with changes:
listen = 9000
Dockerfile
:
Obviously I copy the php-fpm.conf and www.conf (I use php:7.1.14-fpm-jessie
)
... install & cleanup steps
COPY php-fpm.conf /etc/php/7.1/fpm/php-fpm.conf
COPY www.conf /etc/php/7.1/fpm/php-fpm.d/www.conf
the end of Dockerfile for php-fpm looks like:
CMD ["php-fpm", "--fpm-config", "/etc/php/7.1/fpm/php-fpm.conf"]
NGINX
default
which I copy in via Dockerfile:
server {
listen 80 default_server;
root /var/www/app/public;
index index.html index.htm index.php;
server_name _;
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$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass pn-php-fpm:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
error_page 404 /index.php;
location ~ /\.ht {
deny all;
}
}
NGINX's Dockerfile
:
FROM nginx
RUN echo "daemon off;" >> /etc/nginx/nginx.conf
COPY default /etc/nginx/conf.d/default.conf
# forward request and error logs to docker log collector
RUN ln -sf /dev/stdout /var/log/nginx/access.log \
&& ln -sf /dev/stderr /var/log/nginx/error.log
CMD ["nginx"]
Docker compose part
You need to share volumes when composing; what I mean, NGINX + php-fpm must be able to read your application's code
volumes:
- wherever/your/code/is/on/host:/var/www/app
Note: this answer is WIP and I will edit / comment regularly in order to help OP.
这篇关于“ GET /index.php” Dockerize Laravel应用时出现404的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!