gpt4 book ai didi

php - 使用 docker、nginx、php-fpm 提供静态文件

转载 作者:IT老高 更新时间:2023-10-28 21:39:29 27 4
gpt4 key购买 nike

我正在使用 docker 中的容器
我有一个来自 PHP-FPM,另一个来自 Nginx。
但我在使用 Nginx 提供静态文件(css、js)时遇到问题
返回状态码:404 Not Found

Nginx 配置

server {
# Set the port to listen on and the server name
listen 80;
listen [::]:80;

# Set the document root of the project
root /var/www/html;

# Set the directory index files
index index.php;

#Set server name
server_name myproject;

# Specify the default character set
charset utf-8;

# Specify the logging configuration
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;

# Specify what happens when PHP files are requested
location ~* \.php$ {
#try_files $uri =404;
#try_files /index.php = 404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass myproject:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}

location / {
index index.php;
try_files $uri $uri/ /index.php;
include /etc/nginx/mime.types;
}

location ~* \.(jpg|jpeg|png|css|js|ico|xml)$ {
access_log off;
log_not_found off;
expires 360d;
add_header Cache-Control "public";
}

# Specify what happens what .ht files are requested
location ~ /\.ht {
deny all;
}
}

PHP Dockerfile

FROM php:7-fpm
RUN docker-php-ext-install pdo_mysql
COPY . /var/www/html/
EXPOSE 9000

Nginx Dockerfile

FROM nginx:1.12.2
COPY ./default.conf /etc/nginx/conf.d/default.conf
EXPOSE 80

最佳答案

我认为是问题,导致服务nginx找不到你的web项目。如果你使用 docker-compose,你可以使用 volume,但如果没有,你可以将 nginx Dockerfile 中的文件夹项目添加到 /var/www/html

nginx dockerfile

ROM nginx:1.12.2
COPY . /var/www/html/
COPY ./default.conf /etc/nginx/conf.d/default.conf
EXPOSE 80

对于 docker 像这样编写:

services:
nginx:
images: nginx:latest
...
...
volumes:
- ./:/var/www/html
php:
images: php
...
...
volumes:
- ./:/var/www/html

关于php - 使用 docker、nginx、php-fpm 提供静态文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47873793/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com