【问题标题】:why nginx location returns 404 error?为什么 nginx 位置返回 404 错误?
【发布时间】:2018-04-01 09:40:11
【问题描述】:

这是我的 /usr/local/nginx/conf/nginx.conf:

server {
        listen       8080;
        server_name  localhost;

        location / {
            root   html;
            index  index.html index.htm;
        }

        location /test{
                root /var/www/test;
                index index.html index.htm;
        }

当我输入:http://localhost:8080/:我得到了 nginx 欢迎页面:好

当我输入 http://localhost:8080/test 时:出现 404 错误 (我在 /var/www/test 中创建了 index.html 和 index.htm)

PS 我确实重新加载了:

/usr/local/nginx/sbin/nginx -s reload

【问题讨论】:

    标签: nginx


    【解决方案1】:

    将/test 删除到根指令。您只需注明/var/www

    server {
        listen       8080;
        server_name  localhost;
    
        location / {
            root   html;
            index  index.html index.htm;
        }
    
        location /test {
            root /var/www;
            index index.html index.htm;
        }
    

    【讨论】:

    • 我当然重新加载了。你能告诉我更多关于权利和所有者的信息吗?
    • NGinx 服务器在哪个用户下运行?
    • Nginx 以 root 用户运行。 nginx 似乎没问题:当我将端口 8080 更改为 8089 时,我需要转到 lcoalhost:8089。奇怪的是:当我注释指令“位置/”时,我仍然可以转到/位置
    • 看起来 nginx 只路由 / 。无论我放什么位置 /zaza,去 /zaza 都会给我 404 错误
    【解决方案2】:

    这里的问题是,nginx 会在我们提供的根目录中搜索我们添加为位置的文件夹,在这种情况下是位置\test 和根/var/www/test。由于/test 不在/var/www/test 中,它显示错误。如果使用 /var/www 代替它会正常工作,因为 nginx 使用 root + 位置。

    要确认,您遇到了同样的问题,请查看 nginx 错误日志。如果您没有更改日志位置,那么以下命令将完成这项工作。

    sudo tail -n 20 /var/log/nginx/error.log
    

    如果它显示如下消息,您可以确认问题相同。

    [error] 7480#7480: *1 open() "/var/www/test/test" failed (2: No such file or directory), client: XXX.XXX.XX.XX, server: , request: "GET /app HTTP/1.1", host: "XX.XX.XXX.XX"
    

    要解决此问题,请更改以下代码,

    
    server {
    
        listen       8080;
        server_name  localhost;
    
        location / {
            root   html;
            index  index.html index.htm;
        }
    
        location /test {
            root /var/www;
            index index.html index.htm;
        }
    
    

    修改后重启nginx服务

    sudo systemctl restart nginx
    

    【讨论】:

      猜你喜欢
      • 2015-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-15
      • 1970-01-01
      • 2018-12-11
      • 2019-08-16
      • 2017-06-09
      相关资源
      最近更新 更多