【问题标题】:NGINX server block enabled but website returns 404 not found errorNGINX 服务器块已启用,但网站返回 404 not found 错误
【发布时间】:2020-03-12 04:59:11
【问题描述】:

我已经设置了一台运行 Ubuntu Server 18.0.4 的 Virtual Box 客户机。我正在尝试在本地系统上为在 LEMP 堆栈上运行的 Wordpress 网站设置测试环境。跟着网上的一些文章,搭建了php7.2-fpm和nginx服务器以及mysql社区版。 LEMP 设置似乎很好,因为我已经使用包含 phpinfo 函数的测试文件对其进行了验证。出于测试目的,已在虚拟机来宾上配置了一个虚拟静态 IP 地址。

NGINX 中有两个服务器块 - 默认值,指向 phpinfo 和 knowhow.com,指向预期的 Wordpress 网站。符号链接存在于启用站点的目录中,并且在站点可用目录中设置了knowhow.com 文件。但是,当我尝试使用 /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;
    }

}

【问题讨论】:

  • 您的测试 phpinfo 文件在磁​​盘上的什么位置,您使用什么 URL 访问它?
  • /var/www/html/。我以 static-ip-ipaddress/info.php 的形式访问它

标签: nginx ubuntu-18.04 nginx-location nginx-config php-7.2


【解决方案1】:

我已经解决了这个问题!实际上,这从一开始就不是问题。配置文件定义正确。只是,访问该网站的方式不正确。我试图从我的本地主机(在 vm 之外)以 static-ip-address/knowhow.com 的形式访问该站点。我应该只是以 knowhow.com 或 www.knowhow.com 的身份访问该站点。使用 IP 地址不正确,因为服务器块文件 (knowhow.com) 会自动将 Web 请求重定向到目标服务器上相应的网站根路径。我已经更新了我的 /etc/hosts 文件以指向 knowhow.com 和 www.knowhow.com 的静态 IP 地址。傻我! ?

对所有的困惑感到抱歉。我的设置按预期工作。干杯! ??

【讨论】:

    猜你喜欢
    • 2019-12-09
    • 1970-01-01
    • 2015-01-04
    • 1970-01-01
    • 2012-09-20
    • 2018-04-17
    • 2016-09-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多