NGINX 服务器块已启用但网站 returns 404 未找到错误
NGINX server block enabled but website returns 404 not found error
我已经设置了 Virtual Box 客户机 运行 Ubuntu 服务器 18.0.4。我正在尝试在我的本地系统上为 LEMP 堆栈上的 Wordpress 网站 运行 设置测试环境。按照网上的一些文章设置 php7.2-fpm 和 nginx 服务器以及 mysql 社区版。 LEMP 设置似乎没问题,因为我已经使用包含 phpinfo 函数的测试文件对其进行了验证。出于测试目的,已在 virtual box guest 上配置了虚拟静态 IP 地址。
NGINX 中有两个服务器块 - 默认,指向 phpinfo 和 knowhow.com,指向预期的 Wordpress 网站。符号 link 存在于 sites-enabled 目录中,knowhow.com 文件设置在 sites-available 目录中。但是,当我尝试使用 /knowhow.com 访问 Wordpress 站点时,出现 404 Not Found 错误。
仔细研究了一下,发现 knowhow.com 配置文件中的某些重写规则可能不正确。我不知道正确的格式应该是什么。我想访问我的网站。因此,理想情况下,所有请求都应该转到 index.php。下面提供了 knowhow.com 配置文件的内容。有人可以帮忙吗?
# Default server configuration
#
server {
listen 80;
listen [::]:80;
root /var/www/knowhow.com/html;
# Add index.php to the list if you are using PHP
index index.php index.html index.htm index.nginx-debian.html;
server_name knowhow.com www.knowhow.com;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
#try_files $uri $uri/ =404;
try_files $uri $uri/ /index.php?q=$uri&$args;
}
# pass PHP scripts to FastCGI server
#
location ~ \.php$ {
include snippets/fastcgi-php.conf;
#
# # With php-fpm (or other unix sockets):
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
# # With php-cgi (or other tcp sockets):
# fastcgi_pass 127.0.0.1:9000;
}
}
我已经解决问题了!实际上,这从一开始就不是问题。配置文件已正确定义。只是,访问网站的方式不正确。我试图从我的本地主机(在虚拟机之外)以 static-ip-address/knowhow.com 的形式访问该站点。我应该简单地以 knowhow.com 或 www.knowhow.com 访问该站点。使用 IP 地址不正确,因为服务器块文件 (knowhow.com) 应自动将 Web 请求重定向到目标服务器上适当的网站根路径。我已经更新了 /etc/hosts 文件以指向 knowhow.com 和 www.knowhow.com 的静态 IP 地址。傻我!
对于所有的混乱,我们深表歉意。我的设置按预期工作。干杯!
我已经设置了 Virtual Box 客户机 运行 Ubuntu 服务器 18.0.4。我正在尝试在我的本地系统上为 LEMP 堆栈上的 Wordpress 网站 运行 设置测试环境。按照网上的一些文章设置 php7.2-fpm 和 nginx 服务器以及 mysql 社区版。 LEMP 设置似乎没问题,因为我已经使用包含 phpinfo 函数的测试文件对其进行了验证。出于测试目的,已在 virtual box guest 上配置了虚拟静态 IP 地址。
NGINX 中有两个服务器块 - 默认,指向 phpinfo 和 knowhow.com,指向预期的 Wordpress 网站。符号 link 存在于 sites-enabled 目录中,knowhow.com 文件设置在 sites-available 目录中。但是,当我尝试使用 /knowhow.com 访问 Wordpress 站点时,出现 404 Not Found 错误。
仔细研究了一下,发现 knowhow.com 配置文件中的某些重写规则可能不正确。我不知道正确的格式应该是什么。我想访问我的网站。因此,理想情况下,所有请求都应该转到 index.php。下面提供了 knowhow.com 配置文件的内容。有人可以帮忙吗?
# Default server configuration
#
server {
listen 80;
listen [::]:80;
root /var/www/knowhow.com/html;
# Add index.php to the list if you are using PHP
index index.php index.html index.htm index.nginx-debian.html;
server_name knowhow.com www.knowhow.com;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
#try_files $uri $uri/ =404;
try_files $uri $uri/ /index.php?q=$uri&$args;
}
# pass PHP scripts to FastCGI server
#
location ~ \.php$ {
include snippets/fastcgi-php.conf;
#
# # With php-fpm (or other unix sockets):
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
# # With php-cgi (or other tcp sockets):
# fastcgi_pass 127.0.0.1:9000;
}
}
我已经解决问题了!实际上,这从一开始就不是问题。配置文件已正确定义。只是,访问网站的方式不正确。我试图从我的本地主机(在虚拟机之外)以 static-ip-address/knowhow.com 的形式访问该站点。我应该简单地以 knowhow.com 或 www.knowhow.com 访问该站点。使用 IP 地址不正确,因为服务器块文件 (knowhow.com) 应自动将 Web 请求重定向到目标服务器上适当的网站根路径。我已经更新了 /etc/hosts 文件以指向 knowhow.com 和 www.knowhow.com 的静态 IP 地址。傻我!
对于所有的混乱,我们深表歉意。我的设置按预期工作。干杯!