Bug*_*rUK 5 nginx php-fpm docker-compose
我想将任何api/*
路由传递给 php-fpm。特别是index.php
因为我使用 Symfony。这是唯一应该使用 php 的路径。其他任何内容都将从/usr/share/nginx/html/public
(仅 HTML 文件和 CSS)加载。
我已经尝试过,但出现错误:
FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream
Run Code Online (Sandbox Code Playgroud)
我的 nginx 配置如下:
upstream php {
server php:9000;
}
server {
listen 80 default_server;
server_name impressive.local;
index index.html index.php;
root /usr/share/nginx/html/public;
location /api {
root /usr/share/nginx/html/api;
try_files $uri /index.php;
}
location ~ \.php$ {
fastcgi_pass php;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi.conf;
}
}
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
php_1 | [13-Jan-2019 23:22:54] NOTICE: fpm is running, pid 1
php_1 | [13-Jan-2019 23:22:54] NOTICE: ready to handle connections
php_1 | 172.25.0.3 - 13/Jan/2019:23:22:57 +0000 "GET /api/index.php" 404
web_1 | 2019/01/13 23:22:57 [error] 10#10: *1 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream,
client: 172.25.0.1, server: impressive.local, request: "GET /api/index.php HTTP/1.1", upstream: "fastcgi://172.25.0.2:9000", host: "127.0.0.1:8080"
web_1 | 172.25.0.1 - - [13/Jan/2019:23:22:57 +0000] "GET /api/index.php HTTP/1.1" 404 27 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36" "-"
Run Code Online (Sandbox Code Playgroud)
我用谷歌搜索了几个小时,在 Stack Exchange 上查看了多个其他答案。我似乎无法弄清楚是什么导致了这种情况。我使用码头工人。下面是我的docker-compose.yml
version: '3'
services:
web:
image: nginx:alpine
volumes:
- ./web:/usr/share/nginx/html
- ./conf/impressive.template:/etc/nginx/conf.d/default.conf
ports:
- "8080:80"
links:
- php
php:
image: php:7.3.1-fpm-alpine
volumes:
- ./web:/usr/share/nginx/html
Run Code Online (Sandbox Code Playgroud)
/usr/share/nginx/html
结构如下:
- api
index.php
- public
index.html
something.html
...
Run Code Online (Sandbox Code Playgroud)
api
是一个 JSON API,并且public
是静态站点。
我想任何请求api/*
调用api/index.php
和其他任何通过去/usr/share/nginx/html/public
。
我终于解决了!
事实证明,根位置必须与 php-fpm 容器上文件的位置相匹配,因为这是通过以下方式传递的:
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name
Run Code Online (Sandbox Code Playgroud)
我不需要指定该行,因为它包含在: 中include fastcgi.conf;
。
我不确定这是否是最优雅的解决方案:
upstream php {
server php:9000;
}
server {
listen 80 default_server;
server_name _;
index index.html index.php;
root /usr/share/nginx/html/public;
location /api {
root /usr/share/nginx/html;
try_files $uri /api/index.php$is_args$args;
location ~ \.php$ {
fastcgi_pass php;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi.conf;
internal;
}
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
14964 次 |
最近记录: |