大网数据广告活动
服务器运维问答

nginx and php-fpm: "Primary script unknown" while reading response header from upstream

· 52CE 我爱测

I want pass any api/* route to php-fpm. Specifically to index.php as I use Symfony. This is the only route that should use php. Anything else will be loaded from /usr/share/nginx/html/public (just HTML files and CSS). I've attemp…

问题原文

原帖链接 · BugHunterUK · CC BY-SA 4.0

I want pass any api/* route to php-fpm. Specifically to index.php as I use Symfony. This is the only route that should use php. Anything else will be loaded from /usr/share/nginx/html/public (just HTML files and CSS).

I've attempted this, but I'm getting an error:

FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream

My nginx config is below:

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;
  }
}

I get the following error:

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" "-"

I've googled for hours, viewwed multiple other answers on Stack Exchange. I can't seem to figure out what is causing this. I use docker. Below is my 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

/usr/share/nginx/html is structured like so:

- api
    index.php
- public
    index.html
    something.html
    ...

api is a JSON API, and public is the static site.

I want any requests to api/* to call api/index.php and anything else to go via /usr/share/nginx/html/public.

已采纳解答

原帖链接 · BugHunterUK · CC BY-SA 4.0

I finally solved it!

Turns out that the root location had to match the location of the files on the php-fpm container as that's what was being past through via:

fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name

I didn't need to specify that line as it was included in: include fastcgi.conf;.

I'm not sure if this is the most elegant solution:

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;
    }
  }
}

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2019-01-13)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。