gpt4 book ai didi

php - Nginx 仅在 php 脚本和子域上抛出 404 错误

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:18:30 26 4
gpt4 key购买 nike

我在 nginx 上遇到问题:一切正常,直到我尝试添加子域。现在,每次我尝试从浏览器获取 php 文件时,都会收到 404 错误。但最奇怪的是,在我的主域上,它仍然有效。 nginx 上没有错误日志,在访问日志上我可以看到 404 HTTP 响应。有人有答案吗?

/etc/nginx/sites-available/domain.com

server {
listen 80;
server_name domain.com *.domain.com;

access_log /var/www/domain.com/logs/nginx_access.log vhosts;
error_log /var/www/domain.com/logs/nginx_error.log;

location / {
root /var/www/domain/www;
index index.php index.html index.htm;
}
error_page 500 501 502 503 504 /50x.html;

location = /50x.html {
root /var/www/nginx-default;
}

location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
include snippets/fastcgi.conf;
}
}

server {
listen 443 ssl http2;
server_name domain.com *.domain.com;

access_log /var/www/domain.com/logs/nginx_access.log vhosts;
error_log /var/www/domain.com/logs/nginx_error.log;

root /var/www/domain.com/www;

####### SSL Directives #######
ssl on;
ssl_certificate /var/www/domain.com/config/ssl/fullchain.domain.com.pem;
ssl_certificate_key /var/www/domain.com/config/ssl/privkey.domain.com.pem;
ssl_session_timeout 5m;
ssl_protocols TLSv1.2 TLSv1.1 TLSv1;
ssl_ciphers ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:DHE-RSA-AES256-SHA;
ssl_prefer_server_ciphers on;

location / {
root /var/www/domain.com/www;
index index.php index.html index.htm;
}

location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
include snippets/fastcgi.conf;
}


###
# Rewrite rules
###

###
# Rewrite rule 1 : redirect www.domain.com to domain.com
###

if ($host ~* ^www\.(.*)domain\.com$) {
rewrite ^(.*) //domain.com/$1 permanent;
}
}

server {
listen 443 ssl;
server_name sub.domain.com;

location / {
root /var/www/domain.com/sub/www;
index index.php index.html index.htm;
}

###
# SSL
###

ssl on;
ssl_certificate /var/www/domain.com/config/ssl/fullchain.sub.domain.com.pem;
ssl_certificate_key /var/www/domain.com/config/ssl/privkey.sub.domain.com.pem;
ssl_session_timeout 5m;
ssl_protocols TLSv1.2 TLSv1.1 TLSv1;
ssl_ciphers ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES25$;
ssl_prefer_server_ciphers on;

location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
include snippets/fastcgi.conf;
}


###
# Logging
###

access_log /var/www/domain.com/logs/nginx_access.log vhosts;
error_log /var/www/domain.com/logs/nginx_error.log;

}

server {
listen 80;
server_name sub.domain.com;

location / {
root /var/www/domain.com/sub/www;
index index.php index.html index.htm;
}

location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
include snippets/fastcgi.conf;
}


###
# Logging
###

access_log /var/www/domain.com/logs/nginx_access.log vhosts;
error_log /var/www/domain.com/logs/nginx_error.log;
}

错误日志的最后两行

2017/11/26 15:44:37 [error] 25444#25444: *10 directory index of "/var/www/domain.com/sub/www/files/" is forbidden, client: 00.00.000.000, server: sub.domain.com, request: "GET /files/ HTTP/2.0", host: "sub.domain.com"
2017/11/26 15:46:10 [error] 25444#25444: *14 open() "/var/www/domain.com/sub/www/this_file_does_not_exist.p" failed (2: No such file or directory), client: 00.00.000.000, server: sub.domain.com, request: "GET /this_file_does_not_exist.p HTTP/2.0", host: "sub.domain.com"

它似乎表现正常:尝试访问没有索引的文件夹时返回 403 forbidden,尝试获取不存在的文件时返回 404。但是,这里要注意 php 文件上的 404,即使有请求也是如此。

access.log 的最后几行

domain.com 00.00.000.000 - - [26/Nov/2017:15:45:32 +0100] "GET /index.php HTTP/2.0" 200 176 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36"
sub.domain.com 00.00.000.000 - - [26/Nov/2017:15:45:42 +0100] "GET /index.php HTTP/2.0" 404 261 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36"
sub.domain.com 00.00.000.000 - - [26/Nov/2017:15:46:10 +0100] "GET /this_file_does_not_exist.p HTTP/2.0" 404 260 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36"
sub.domain.com 00.00.000.000 - - [26/Nov/2017:15:46:24 +0100] "GET /main_style.css HTTP/2.0" 200 2931 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36"

在主域上,php 文件有一个 200 响应并显示在浏览器中。但是,在子域中,他们返回 404 响应,就像他们不在这里一样。但是,index.php 与 main_style.css 位于同一文件夹中,并且 main_style.css 返回 200 响应并显示。

我真的不知道该怎么做才能让它发挥作用。如果有人能提供帮助,他将非常欢迎。

编辑:这是尝试获取 https://sub.domain.com/ 时的 HTTP 响应,通过 hurl.it。 enter image description here

PS:sub.domain.com 不是我的域名。我只是更改了它以避免这篇文章在我的网站上出现在谷歌上。

最佳答案

您缺少 root 指令。

location/location ~\.php$ 共享同一个根,通常在 server block 中指定根并允许两个位置继承相同的值。

例如:

server {
...
root /var/www/domain.com/www;

location / {
...
}
location ~ \.php$ {
...
}
}

参见 this document了解更多。

关于php - Nginx 仅在 php 脚本和子域上抛出 404 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47497613/

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