Nginx laravel docker 项目 return 错误 502 错误网关
Ngnix laravel docker project return error 502 bad gateway
我尝试 运行 laravel 在 docker 容器中使用 ngnix。
目录结构:
文件docker-compose.yml
:
version: '3'
services:
nginx:
image: nginx:stable-alpine
container_name: nginx
ports:
- "${HOST_PORT}:80"
volumes:
- ../:/var/www/html
- ./etc/nginx/default.conf:/etc/nginx/conf.d/default.conf
depends_on:
- php
php:
build:
context: .
dockerfile: ../Dockerfile
restart: always
container_name: php
volumes:
- "../:/var/www/html"
ports:
- "9000:9000"
mongodb:
image: mongo:latest
container_name: mongodb
ports:
- "27017:27017"
redis:
image: redis:latest
container_name: redis
restart: always
ports:
- "6379:6379"
文件Dockerfile
:
FROM php:7.4-apache
RUN apt-get update && apt-get install --yes --no-install-recommends \
libssl-dev
RUN docker-php-ext-install pdo pdo_mysql
RUN pecl install mongodb \
&& docker-php-ext-enable mongodb
命令结果docker ps
:
当我尝试在浏览器地址http://localhost:8004
中打开时出现错误:
502 Bad Gateway nginx/1.18.0
Ngnix 配置文件:
server {
listen 80;
index index.php index.html;
server_name localhost;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /var/www/html/public;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
环境变量:
HOST_PORT=8004
HOST_SSL_PORT=3004
# Nginx
NGINX_HOST=localhost
当您使用 nginx 作为您的网络服务器时,您不需要在 Dockerfile
.
中安装 PHP Apache (FROM php:7.4-apache
)
改为尝试 php:7.4-fpm
并确保 nginx 正确访问 php-fpm(default.conf
文件中的第 fastcgi_pass php:9000;
行。在你的情况下,一切似乎都配置正确) .
还有一点需要注意,没有必要向主机公开 php-fpm
端口 9000。由于两个容器(在您的 docker-compose.yml
中)将使用相同的网络,因此 nginx
容器可以直接访问 php
容器。您可以删除行
ports:
- "9000:9000"
我尝试 运行 laravel 在 docker 容器中使用 ngnix。
目录结构:
文件docker-compose.yml
:
version: '3'
services:
nginx:
image: nginx:stable-alpine
container_name: nginx
ports:
- "${HOST_PORT}:80"
volumes:
- ../:/var/www/html
- ./etc/nginx/default.conf:/etc/nginx/conf.d/default.conf
depends_on:
- php
php:
build:
context: .
dockerfile: ../Dockerfile
restart: always
container_name: php
volumes:
- "../:/var/www/html"
ports:
- "9000:9000"
mongodb:
image: mongo:latest
container_name: mongodb
ports:
- "27017:27017"
redis:
image: redis:latest
container_name: redis
restart: always
ports:
- "6379:6379"
文件Dockerfile
:
FROM php:7.4-apache
RUN apt-get update && apt-get install --yes --no-install-recommends \
libssl-dev
RUN docker-php-ext-install pdo pdo_mysql
RUN pecl install mongodb \
&& docker-php-ext-enable mongodb
命令结果docker ps
:
当我尝试在浏览器地址http://localhost:8004
中打开时出现错误:
502 Bad Gateway nginx/1.18.0
Ngnix 配置文件:
server {
listen 80;
index index.php index.html;
server_name localhost;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /var/www/html/public;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
环境变量:
HOST_PORT=8004
HOST_SSL_PORT=3004
# Nginx
NGINX_HOST=localhost
当您使用 nginx 作为您的网络服务器时,您不需要在 Dockerfile
.
FROM php:7.4-apache
)
改为尝试 php:7.4-fpm
并确保 nginx 正确访问 php-fpm(default.conf
文件中的第 fastcgi_pass php:9000;
行。在你的情况下,一切似乎都配置正确) .
还有一点需要注意,没有必要向主机公开 php-fpm
端口 9000。由于两个容器(在您的 docker-compose.yml
中)将使用相同的网络,因此 nginx
容器可以直接访问 php
容器。您可以删除行
ports:
- "9000:9000"