![](/img/trans.png)
[英]How to resolve a PHP-FPM Primary script unknown with a PHP-FPM and an Nginx Docker container?
[英]Docker - NGINX Container forward to PHP-FPM Container
我在單獨的容器中運行 NGINX、PHP-FPM 和 DB。
PHP-FPM 內部正在從我的本地機器安裝Laravel項目。
在訪問 127.0.0.1:8000 時,我已成功將 PHP 請求轉發到 PHP-FPM 容器(端口 9000)。 不幸的是,帶有資產擴展名(例如.css、.js)的請求遇到了403 forbidden。
以下是我的 NGINX 配置腳本。
server {
listen 80;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";
index index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_pass fpm:9000;
fastcgi_param SCRIPT_FILENAME /app/public$fastcgi_script_name;
fastcgi_index index.php;
include fastcgi_params;
}
location ~* \.(css|js|gif|ico|jpeg|jpg|png)$ {
fastcgi_pass fpm:9000;
fastcgi_param SCRIPT_FILENAME /app/public$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_index index.php;
include fastcgi_params;
}
}
不確定是否有人遇到過類似的問題並對此有解決方案?
您將所有內容都轉發到 PHP FPM,同時,默認情況下,在 PHP-FPM 進程配置文件中,它只允許提供.php
文件。
您可以在php-fpm
容器內檢查/usr/local/etc/php-fpm.d/www.conf
,並搜索security.limit_extensions
,您會看到。
所以這里你有 2 個解決方案
解決方案 1:將您的項目源映射到您運行 Nginx 的容器中,如下所示:
# docker-compose.yml
webserver:
image: nginx:1.17-alpine
restart: unless-stopped
ports:
- "8000:80"
volumes:
- ./:/var/www/html
通過這樣做,Nginx 可以輕松找到您的靜態文件並為它們提供服務。 請注意, /var/www/html
是您在 Nginx 配置文件中定義的根項目路徑。 例如,Laravel 項目的 Nginx 配置文件通常如下所示:
server {
listen 80;
index index.php index.html;
root /var/www/html/public;
...
解決方案 2:將.css, .js
添加到 PHP-FPM 進程配置文件中,使用此解決方案,您將覆蓋 PHP-FPM 配置文件並將靜態文件添加到 PHP-FPM 允許的文件擴展名列表中。 在這里查看我的演示。 此解決方案不需要您將項目映射到 Nginx 容器中。 但實際上它並不適合解決方案 1 那樣的生產
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.