gpt4 book ai didi

php - 为什么我不能在 nginx 中拥有自己的自定义根指令

转载 作者:行者123 更新时间:2023-12-04 19:37:35 26 4
gpt4 key购买 nike

操作系统: Centos7

Nginx: 1.9.14

请考虑这个简单的服务器 block

server {
listen 80;
server_name website.com;
root /usr/share/nginx/html;
index index.php index.html index.htm;

location / {
try_files $uri $uri/ /index.php?$args;
}

location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}

我已经搜遍了网络,试图弄清楚这一点。上述配置有效。位于根指令的是一个名为 index.php 的文件,其中包含这一行
<?php phpinfo();

导航到该站点会显示 PHPINFO。所以这证实了 PHP 工作正常。

假设我想将我的根指令更改为;
root /steven/html;

在此位置内,驻留相同的 index.php 文件。但我得到的不是 PHPINFO;
File not found.

我所做的只是更改了根指令,因为我宁愿把文件放在那里。 /var/log/nginx/error.log 揭示;
2016/05/24 04:03:41 [error] 473#473: *7 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 125.238.2.111, server: website.com, request: "GET / HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "website.com"
  • /usr/share/nginx/html 的文件夹权限都是 root:root 755
  • /steven/html 的文件夹权限也是 root:root 755

  • 有人知道为什么吗?我真的很感激在这里大开眼界。

    我知道会询问以下内容,因此我将包括这些内容。

    Nginx 编译自源码
    nginx version: nginx/1.9.14
    built by gcc 4.8.5 20150623 (Red Hat 4.8.5-4) (GCC)
    built with OpenSSL 1.0.2g 1 Mar 2016
    TLS SNI support enabled
    configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --add-module=/root/custom-nginx/nginx-1.9.14/src/http/modules/ngx_cache_purge/ --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --user=nginx --group=nginx --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-http_auth_request_module --with-threads --with-stream --with-stream_ssl_module --with-file-aio --with-ipv6 --with-http_v2_module --without-mail_pop3_module --without-mail_imap_module --without-mail_smtp_module --without-http_uwsgi_module --without-http_scgi_module --without-http_memcached_module --with-openssl=../../openssl-1.0.2g --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic'

    fastcgi_params 的内容
    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;

    健全性检查
    [root@ip-172-31-2-48 /]# ls -l /steven/html
    total 4
    -rw-r--r--. 1 root root 17 May 24 03:49 index.php

    最佳答案

    我不会经常用响亮的"is"来回答我自己的问题!

    @Darren 让我将 PHP-FPM 的监听端口从默认的 9000 更改为其他端口。

    我试图这样做,但由于某种原因 php-fpm.service 不允许这样做。哦亲爱的..

    ERROR: unable to bind listening socket for address '127.0.0.1:9001': Permission denied (13)

    我找到了这个。

    https://unix.stackexchange.com/questions/180460/php-fpm-error-unable-to-bind-listening-socket-for-address-127-0-0-19003-perm
    cat /etc/selinux/config

    更改 SELINUX=强制执行 SELINUX=禁用

    这不仅允许 PHP-FPM 进程的端口号绑定(bind)到 9001,而且还修复了当我想指定自己的根指令时找不到文件的问题。

    也许有人会知道如何创建一个 SELINUX 规则来允许它而不是完全禁用它。

    现在有可能所有人都对 有问题。找不到文件 - 这是你需要检查的。

    再次感谢@Darren,但我永远不会发现这一点。

    关于php - 为什么我不能在 nginx 中拥有自己的自定义根指令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37404416/

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