Laravel docker-compose 404 未找到 Nginx
Laravel docker-compose 404 not found Nginx
我在尝试使用 docker-compose 运行 我的 Laravel 应用程序时遇到了一个奇怪的行为。
启动容器后,如果我尝试访问我的网站 URL,我会收到以下错误:
404 Not Found from Nginx.
这里是 docker-compose.yml
:
version: '3'
services:
#Laravel App
app:
build:
context: . # path to the docker file
dockerfile: Dockerfile # name of the dockerfile
# image: apperitivo_app # name to assign to the image
container_name: app # name to assign to the container
restart: unless-stopped # always restart container except when stopped (manually or otherwise)
tty: true # once started keeps container running (attaching a terminal to it, like using -t docker directive)
environment: # set environment variables
SERVICE_NAME: app
SERVICE_TAGS: dev
working_dir: /var/www/html # working directory of the running container
volumes:
- ./src/:/var/www/html # map application directory in the /var/www/html directory of the container
networks:
- mynet # networks at which container is connected
#Nginx Service
nginx:
image: nginx:alpine
container_name: nginx
restart: unless-stopped
tty: true
ports: # exposed ports
- "80:80" # http
- "443:443" # https
volumes:
- ./src/:/var/www/html
- ./nginx/conf.d/:/etc/nginx/conf.d/ # nginx configuration
- ./data/certbot/conf:/etc/letsencrypt # letsencrypt certificates
- ./data/certbot/www:/var/www/certbot # certbot service
networks:
- mynet
command: "/bin/sh -c 'while :; do sleep 6h & wait $${!}; nginx -s reload; done & nginx -g \"daemon off;\"'"
# Certbot service to manage ssl certificates
certbot:
image: certbot/certbot
volumes:
- ./data/certbot/conf:/etc/letsencrypt
- ./data/certbot/www:/var/www/certbot
entrypoint: "/bin/sh -c 'trap exit TERM; while :; do certbot renew; sleep 12h & wait $${!}; done;'"
networks:
- mynet
#MySQL Service
db:
image: mysql/mysql-server #mysql:5.7
container_name: db
restart: unless-stopped
tty: true
ports:
- "3306:3306"
environment:
MYSQL_DATABASE: laraveldb
MYSQL_USER: laraveluser
MYSQL_PASSWORD: laravel_db_password
MYSQL_ROOT_PASSWORD: mysql_root_password
volumes:
- mysqldata:/var/lib/mysql/
networks:
- mynet
#Docker Networks
networks:
mynet:
driver: bridge
#Volumes
volumes:
mysqldata:
driver: local
这是 nginx 配置文件:
server {
listen 80;
index index.php index.html;
error_log /var/log/nginx/error.log debug;
access_log /var/log/nginx/access.log;
root /var/www/public;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass app:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
location / {
try_files $uri $uri/ /index.php?$query_string;
gzip_static on;
}
}
最后这是 Laravel 的 .env 文件:
APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:Vs71ZhnWfc9tvoSBTCn26CYCuP/lm2vI7AO7IOf0v5E=
APP_DEBUG=true
APP_URL=http://www.apperitivo.it
LOG_CHANNEL=stack
LOG_DEPRECATIONS_CHANNEL=null
LOG_LEVEL=debug
DB_CONNECTION=mysql
DB_HOST=db
DB_PORT=3306
DB_DATABASE=laraveldb
DB_USERNAME=laraveluser
DB_PASSWORD=laravel_db_password
BROADCAST_DRIVER=log
CACHE_DRIVER=file
FILESYSTEM_DISK=local
QUEUE_CONNECTION=sync
SESSION_DRIVER=file
SESSION_LIFETIME=120
MEMCACHED_HOST=127.0.0.1
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379
MAIL_MAILER=smtp
MAIL_HOST=mailhog
MAIL_PORT=1025
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS="hello@example.com"
MAIL_FROM_NAME="${APP_NAME}"
AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_DEFAULT_REGION=us-east-1
AWS_BUCKET=
AWS_USE_PATH_STYLE_ENDPOINT=false
PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_APP_CLUSTER=mt1
MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"
有什么建议吗?
在您的 nginx 中,您将 root
设置为 root /var/www/public;
。
在您的 docker-compose.yml
中,您将源代码安装到 /var/www/html
.
确保它们相同。
我在尝试使用 docker-compose 运行 我的 Laravel 应用程序时遇到了一个奇怪的行为。 启动容器后,如果我尝试访问我的网站 URL,我会收到以下错误:
404 Not Found from Nginx.
这里是 docker-compose.yml
:
version: '3'
services:
#Laravel App
app:
build:
context: . # path to the docker file
dockerfile: Dockerfile # name of the dockerfile
# image: apperitivo_app # name to assign to the image
container_name: app # name to assign to the container
restart: unless-stopped # always restart container except when stopped (manually or otherwise)
tty: true # once started keeps container running (attaching a terminal to it, like using -t docker directive)
environment: # set environment variables
SERVICE_NAME: app
SERVICE_TAGS: dev
working_dir: /var/www/html # working directory of the running container
volumes:
- ./src/:/var/www/html # map application directory in the /var/www/html directory of the container
networks:
- mynet # networks at which container is connected
#Nginx Service
nginx:
image: nginx:alpine
container_name: nginx
restart: unless-stopped
tty: true
ports: # exposed ports
- "80:80" # http
- "443:443" # https
volumes:
- ./src/:/var/www/html
- ./nginx/conf.d/:/etc/nginx/conf.d/ # nginx configuration
- ./data/certbot/conf:/etc/letsencrypt # letsencrypt certificates
- ./data/certbot/www:/var/www/certbot # certbot service
networks:
- mynet
command: "/bin/sh -c 'while :; do sleep 6h & wait $${!}; nginx -s reload; done & nginx -g \"daemon off;\"'"
# Certbot service to manage ssl certificates
certbot:
image: certbot/certbot
volumes:
- ./data/certbot/conf:/etc/letsencrypt
- ./data/certbot/www:/var/www/certbot
entrypoint: "/bin/sh -c 'trap exit TERM; while :; do certbot renew; sleep 12h & wait $${!}; done;'"
networks:
- mynet
#MySQL Service
db:
image: mysql/mysql-server #mysql:5.7
container_name: db
restart: unless-stopped
tty: true
ports:
- "3306:3306"
environment:
MYSQL_DATABASE: laraveldb
MYSQL_USER: laraveluser
MYSQL_PASSWORD: laravel_db_password
MYSQL_ROOT_PASSWORD: mysql_root_password
volumes:
- mysqldata:/var/lib/mysql/
networks:
- mynet
#Docker Networks
networks:
mynet:
driver: bridge
#Volumes
volumes:
mysqldata:
driver: local
这是 nginx 配置文件:
server {
listen 80;
index index.php index.html;
error_log /var/log/nginx/error.log debug;
access_log /var/log/nginx/access.log;
root /var/www/public;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass app:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
location / {
try_files $uri $uri/ /index.php?$query_string;
gzip_static on;
}
}
最后这是 Laravel 的 .env 文件:
APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:Vs71ZhnWfc9tvoSBTCn26CYCuP/lm2vI7AO7IOf0v5E=
APP_DEBUG=true
APP_URL=http://www.apperitivo.it
LOG_CHANNEL=stack
LOG_DEPRECATIONS_CHANNEL=null
LOG_LEVEL=debug
DB_CONNECTION=mysql
DB_HOST=db
DB_PORT=3306
DB_DATABASE=laraveldb
DB_USERNAME=laraveluser
DB_PASSWORD=laravel_db_password
BROADCAST_DRIVER=log
CACHE_DRIVER=file
FILESYSTEM_DISK=local
QUEUE_CONNECTION=sync
SESSION_DRIVER=file
SESSION_LIFETIME=120
MEMCACHED_HOST=127.0.0.1
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379
MAIL_MAILER=smtp
MAIL_HOST=mailhog
MAIL_PORT=1025
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS="hello@example.com"
MAIL_FROM_NAME="${APP_NAME}"
AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_DEFAULT_REGION=us-east-1
AWS_BUCKET=
AWS_USE_PATH_STYLE_ENDPOINT=false
PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_APP_CLUSTER=mt1
MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"
有什么建议吗?
在您的 nginx 中,您将 root
设置为 root /var/www/public;
。
在您的 docker-compose.yml
中,您将源代码安装到 /var/www/html
.
确保它们相同。