[英]Nginx throwing 404 error on php cripts and on subdomain only
I have a problem on nginx : everything worked fine until i tried to add a subdomain. 我在nginx上遇到问题:一切正常,直到尝试添加子域。 Now, every time I try to get a php file from my browser, I get a 404 error.
现在,每次我尝试从浏览器获取php文件时,都会收到404错误。 But the strangest thing is that on my main domain, it still works.
但是最奇怪的是,在我的主域上,它仍然有效。 There are no error logs on nginx, and on access log I can see the 404 HTTP response.
在nginx上没有错误日志,在访问日志中我可以看到404 HTTP响应。 Does anyone have an answer ?
有人有答案吗?
/etc/nginx/sites-available/domain.com /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;
}
The 2 last lines of the error log 错误日志的最后2行
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"
It seems to behave normally : it returns 403 forbidden when trying to acces to a folder without index, and 404 when trying to get a non existing file. 它似乎正常运行:尝试访问没有索引的文件夹时,它返回403禁止,而试图获取不存在的文件时,则返回404。 But, here is noting about 404 on php files, even if requested.
但是,这里注意到有关php文件的404,即使有要求也是如此。
last lines of access.log 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"
On main domain, php files have a 200 response and are displayed in browser. 在主域中,php文件的响应为200,并显示在浏览器中。 But, in subdomain, they return a 404 response, like they were not here.
但是,在子域中,它们返回404响应,就像它们不在这里一样。 But, index.php is in the same folder than main_style.css, and main_style.css returns a 200 response and is displayed.
但是,index.php与main_style.css位于同一文件夹中,并且main_style.css返回200响应并显示。
I really don't know what to do for this to work. 我真的不知道该怎么办。 If anyone could help he would be very welcome.
如果有人可以帮助他,将非常欢迎。
EDIT : Here is a the HTTP response when trying to GET https://sub.domain.com/ , via hurl.it. 编辑:这是试图获得当一个HTTP响应https://sub.domain.com/ ,通过hurl.it. enter image description here
在此处输入图片说明
PS : sub.domain.com is not my domain name. PS:sub.domain.com不是我的域名。 I just changed it to avoid this post being above my website in Google.
我只是对其进行了更改,以避免此帖子出现在我的Google网站上方。
You are missing a root
directive. 您缺少
root
指令。
Where location /
and location ~ \\.php$
share the same root, it is usual to specify the root in the server
block and allow both locations to inherit the same value. 如果
location /
和location ~ \\.php$
共享相同的根,通常在server
块中指定根,并允许两个位置继承相同的值。
For example: 例如:
server {
...
root /var/www/domain.com/www;
location / {
...
}
location ~ \.php$ {
...
}
}
See this document for more. 有关更多信息,请参见此文档 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.