Fastcgi 脚本“未找到文件”/主要脚本未知

Fastcgi 脚本“未找到文件”/主要脚本未知

实际情况

我正在将静态 Web 服务器转移到容器中。

遇到的问题

当我尝试访问我的服务器时,收到“文件未找到”的提示:

  • curl 命令:curl --insecure -v -H "主机:<site_name>"https://127.0.0.1:443
  • 浏览器:https://<site_name>

以下是该站点的 NGINX 容器日志中的错误消息:

2023/07/04 13:25:38 [错误] 8#8:*1 FastCGI 在从上游读取响应标头时在 stderr 中发送:“主要脚本未知”,客户端:xxxx,服务器:site,请求:“GET / HTTP/1.1”,上游:“fastcgi://172.22.0.2:9000”,主机:“site”

以下是 php 容器日志上的 404 错误:

172.22.0.3 - 04/Jul/2023:13:25:38 +0000 “获取 /php/index.php” 404

版本

以下是我使用的所有版本:

  • 操作系统 Docker 服务器:Red Hat Enterprise Linux Server 版本 7.6
  • Docker:Docker 版本 24.0.2
  • Docker Compose :Docker Compose 版本 v2.18.1

在容器上:

  • php-fpm :PHP 8.2.0(fpm-fcgi)/ Zend Engine v4.2.0 / Zend OPcache v8.2.0
  • php 容器的操作系统:Debian 11.5
  • nginx:nginx/1.23.1
  • openssl :OpenSSL 1.1.1n
  • nginx 容器的操作系统:Debian 11.5

文件配置

PHP-FPM 容器(我从未修改过该文件,为了更好地理解,我删除了“;”行):

/usr/local/etc/php-fpm.conf

;;;;;;;;;;;;;;;;;;;;;
; FPM Configuration ;
;;;;;;;;;;;;;;;;;;;;;

include=/usr/local/etc/php-fpm.d/*.conf

;;;;;;;;;;;;;;;;;;
; Global Options ;
;;;;;;;;;;;;;;;;;;

[global]
pid = /usr/local/var/run/php-fpm.pid
error_log = /proc/self/fd/2
log_level = notice
daemonize = no

NGINX 容器:

/etc/nginx/conf.d/site1.conf

     server {
             listen 80;
             return 301 https://<site_name>$request_uri; }
     
     server {
             listen 443 ssl;
             server_name <site_name>;
             root /webservers/site1;
     
             # Self-signed with openssl
             ssl_certificate /etc/nginx/ssl/testapp1/testapp1.crt;
             ssl_certificate_key /etc/nginx/ssl/testapp1/testapp1.key;
     
             location / {
                     try_files $uri $uri/ /index.php?$query_string;
             }
     
             location ~ \.php$ {
                     fastcgi_pass php-fpm:9000;
                     fastcgi_index /webservers/oca/php/index.php;
                     #include        fastcgi_params;
                     #fastcgi_param SCRIPT_FILENAME /usr/local/var/run/$fastcgi_script_name;
                     include fastcgi.conf;
             }
     
             location ~ \.(css|js|gif|ico|jpeg|jpg|png)(.*)$ {
                     expires max;
             }
         access_log /var/log/site1.info.access.log;
          error_log /var/log/site1.info.error.log; 
}

/etc/nginx/nginx.conf

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}
http {
    index php/index.php index.html index.htm;
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    access_log  /var/log/nginx/access.log  main;
    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;    
    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;
    include /etc/nginx/conf.d/*.conf;
}

/etc/nginx/fastcgi.conf

fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;

fastcgi_param  QUERY_STRING       $query_string;

fastcgi_param  REQUEST_METHOD     $request_method;

fastcgi_param  CONTENT_TYPE       $content_type;

fastcgi_param  CONTENT_LENGTH     $content_length;

fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;

fastcgi_param  REQUEST_URI        $request_uri;

fastcgi_param  DOCUMENT_URI       $document_uri;

fastcgi_param  DOCUMENT_ROOT      $document_root;

fastcgi_param  SERVER_PROTOCOL    $server_protocol;

fastcgi_param  REQUEST_SCHEME     $scheme;

fastcgi_param  HTTPS              $https if_not_empty;

fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;

fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;

fastcgi_param  REMOTE_ADDR        $remote_addr;

fastcgi_param  REMOTE_PORT        $remote_port;

fastcgi_param  SERVER_ADDR        $server_addr;

fastcgi_param  SERVER_PORT        $server_port;

fastcgi_param  SERVER_NAME        $server_name;

# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param  REDIRECT_STATUS    200;

ls -a /etc/nginx/

conf.d

fastcgi.conf

fastcgi.conf.bak

fastcgi_params

mime.types

modules

nginx.conf

scgi_params

ssl

uwsgi_params

ls -a /web 服务器 /site1

css

js

lifecycle_policy.sh

php

files

ls -a /web 服务器 /site1/php

索引.php

docker-compose.yml

version: "3.8"
services:
  web:
    image: nginx:0.1
    ports:
      - "80:80"
      - "443:443"
    command:
      - service nginx restart
    depends_on:
      - "php-fpm"
    links:
      - "php-fpm"
  
  php-fpm:
    image: php-fpm:0.1
    ports:
      - ":9000"

我正在尝试解决它

我尝试了论坛上指示的许多配置,但没有效果。

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

fastcgi_param SCRIPT_FILENAME $request_filename;

重启 NGINX

和另一项测试...

有效做法

它可以在没有 fastcgi_param 的情况下工作:

location ~ \.php$ {
      fastcgi_pass php-fpm:9000;
      fastcgi_index php/index.php;
}

但我有空白页...

它与 fastcgi_param 一起工作:

使用 NGINX / OPENSSL / PHP-FPM (RedHat 8.7) 直接安装在虚拟机上

问题

我知道论坛上有很多关于这个问题的答案,但我没有找到一个好的解决方案。你有什么建议吗?

答案1

我终于解决了我的问题:

我不明白 .php 文件需要放在 PHP-FPM 容器中。我构建了自己的 php-fpm 映像并传输了站点文件:

FROM repo/php-fpm AS builder
LABEL maintainer="x" description="NGINX PHP"
RUN mkdir /webservers
COPY --chown=root:root site1.tar /webservers
RUN cd /webservers \
    && tar xvf site1.tar \
    && chown -R root:root site1/* \
    && chmod -R 755 site1/*
EXPOSE 9000
ENTRYPOINT ["php-fpm"]

docker 镜像构建-t php-fpm:0.1。

相关内容