我已经阅读了所有类似的主题,但无法解决问题,因此我在这里发布了:
我可以在 SSH 终端上成功运行 PHP 文件:
root@skins:~/public1# php8.3 /root/public1/public/index.php
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta http-equiv="refresh" content="0;url='http://:/requirement#'" />
<title>Redirecting to http://:/requirement#</title>
</head>
<body>
Redirecting to <a href="http://:/requirement#">http://:/requirement#</a>.
</body>
但是,我无法在浏览器中运行相同的文件,并且出现 Nginx 文件未找到错误。
这是我的 Nginx site.conf:
server {
listen 80;
listen [::]:80;
server_name some.domain.com;
root /root/public1/public; ##<----THIS IS THE ACTUAL PATH
add_header X-Frame-Options "SAMEORIGIN";
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 unix:/run/php/php8.3-fpm.sock;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
fastcgi_hide_header X-Powered-By;
}
location ~ /\.(?!well-known).* {
deny all;
}
}
在 Nginx 错误日志中我有以下内容:
2024/12/25 13:55:05 [crit] 74930#74930: *5 stat() "/root/public1/public/" failed (13: Permission denied), client: 88.198.213.143, server: some.domain.com, request: "GET / HTTP/1.1", host: "some.domain.com"
2024/12/25 13:55:05 [crit] 74930#74930: *5 stat() "/root/public1/public/" failed (13: Permission denied), client: 88.198.213.143, server: some.domain.com, request: "GET / HTTP/1.1", host: "some.domain.com"
2024/12/25 13:55:05 [crit] 74930#74930: *5 realpath() "/root/public1/public" failed (13: Permission denied), client: 88.198.213.143, server: some.domain.com, request: "GET / HTTP/1.1", host: "some.domain.com"
2024/12/25 13:55:05 [error] 74930#74930: *5 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 88.198.213.143, server: some.domain.com, request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/run/php/php8.3-fpm.sock:", host: "some.domain.com"
2024/12/25 13:55:05 [error] 74930#74930: *5 open() "/root/public1/public/favicon.ico" failed (13: Permission denied), client: 88.198.213.143, server: some.domain.com, request: "GET /favicon.ico HTTP/1.1", host: "some.domain.com", referrer: "http://some.domain.com/"
有人能帮我解决 Nginx 配置问题吗?
nginx
您的 nginx 配置将站点的根目录设置在用户(www-data
)无权访问的目录中。www-data
将nginx根目录修改为有权限访问的位置。例如:
是一个常见的根目录位置。
错误消息很明确:Nginx 无权访问该文件。这很有道理:
/root
这是 root 用户的主目录 –除了 root 用户之外,任何人都无权访问它,当然也不是暴露于远程攻击的 Web 服务器。简单的解决方案是将文件移动到运行 Nginx Web 服务器的用户(可能是某些 Ngninx 服务用户)可以访问的地方。
在 Ubuntu 中,默认情况下,Nginx 在
www-data
用户下运行。因此,您可以创建一个目录供 Nginx 使用,如下所示:这将授予 root 读/写访问权限、Nginx 读取访问权限,而其他所有人则完全无访问权限。