【问题标题】:nginx location 404 not found未找到 nginx 位置 404
【发布时间】:2017-04-27 05:28:50
【问题描述】:

这是我的 nginx 配置文件。

在default.conf中,第一个位置用来访问/usr/share/nginx/html目录,我访问http://47.91.152.99就可以了。 但是当我为目录 /usr/share/nginx/public 目录添加一个新位置时,当我访问 http://47.91.152.99/test 时,nginx 会返回一个 404 页面。

那么,到底是怎么回事?我是不是误用了 nginx 的指令?

/etc/nginx/nginx.conf

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}


/etc/nginx/conf.d/default.conf
server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/log/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    location ^~ /test/ {
        root /usr/share/nginx/public;
        index index.html index.htm;
    }
    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

【问题讨论】:

  • 您的意思是http://47.91.152.99/test/(尾随/)?文件是否位于/usr/share/nginx/public/test/index.html?
  • 是的,后面有 /。 index.html 在目录中。
  • index.html 在哪个目录下?您的问题暗示/usr/share/nginx/public,但您的配置文件使用/usr/share/nginx/public/test

标签: nginx location http-status-code-404 nginx-location nginx-config


【解决方案1】:

以下错误块(在您的情况下);

 location ^~ /test/ {
     root /usr/share/nginx/public;
     index index.html index.htm;
 }

告诉 nginx 在文件夹(根)/usr/share/nginx/public 中查找目录“test”。如果该根目录中没有“test”文件夹,它将返回 404。为了解决您的问题,我建议您尝试使用 alias 而不是 root。像这样:

 location ^~ /test/ {
     alias /usr/share/nginx/public;
     index index.html index.htm;
 }

另外,只是为了踢球,通常可以设置索引指令,这样你就不必一直重写它......就像这样;

 server {
     listen       80;
     server_name  localhost;

     root   /usr/share/nginx/html;
     index  index.html index.htm;

     error_page   500 502 503 504  /50x.html;

     location / { }

     location ~^/test/ {
         alias /usr/share/nginx/public;
     }

     location = /50x.html {
         root   /usr/share/nginx/html;
     }
 }

您还应该考虑的一件事...位置块越“精确”,它应该驻留在配置中的位置就越高。就像location = /50x.html。在一个完美的世界里,这将被设置在顶部,就在一般服务器块设置之后。

希望对你有帮助。

【讨论】:

  • 它不起作用。我仍然收到 404 not found 响应。
  • 访问url47.91.152.99/test时,我想让nginx服务器访问根目录/usr/share/nginx/public。
  • 它将匹配以 /images/ 开头的请求(位置 / 也匹配此类请求,但前缀较短)。服务器块的最终配置应如下所示: server { location / { root /data/www; } 位置 /images/ { 根 /data; } }
  • 我在/usr/share/nginx/public下创建test目录后,响应正常。谢谢你的回复。
【解决方案2】:

根指令引起的错误

location ^~ /test/ {
    root /usr/share/nginx/public;
    index index.html index.htm;
}

使用别名指令修复

 location ^~ /test/ {
     alias /usr/share/nginx/public;
     index index.html index.htm;
 }

其他改进

额外提示:可以设置 index 指令,这样您就不必重新编写它。

server {
    listen       80;
    server_name  localhost;

    root   /usr/share/nginx/html;
    index  index.html index.htm;

    error_page   500 502 503 504  /50x.html;

    location / { }

    location ~^/test/ {
        alias /usr/share/nginx/public;
    }

    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

nginx 部分根据配置中的位置匹配位置块。理想情况下,你会颠倒你现在拥有的东西。位置块在 nginx 配置中会更高。为此, location = /50x.html 也会向上移动。顺序是

  1. 完全匹配 =
  2. 正向比赛^~ /
  3. 区分大小写的正则表达式 ~ /
  4. 不区分大小写的正则表达式 ~*
  5. 路径匹配/

更多关于nginx location priority。此外,您可以随时查看官方文档。位置块的 nginx 文档http://nginx.org/en/docs/http/ngx_http_core_module.html#location

【讨论】:

  • 版主评论:“此编辑旨在针对帖子的作者,作为编辑没有任何意义。它应该被写成评论或答案。”评论不够长。你更喜欢什么?
【解决方案3】:

当你的应用是vuejs时,你需要这样写,可以防止404,注意双/test/

   location ^~/test/ {
       alias /usr/local/soft/vuejs/;
       try_files $uri $uri/ /test/index.html;
    }

【讨论】:

    【解决方案4】:

    就我而言,如果您托管多个站点,请检查server_name,它可能会被错误地指出。也可以看看。

    【讨论】:

      【解决方案5】:

      我刚刚解决了这个(index.html not found)问题。
      对我来说,我输错了我的项目名称以匹配您的 ec2 项目名称和 nginx 路径。

      移动到 nginx/sites-enabled 以检查 nginx 路径

      1. cd /etc/nginx/sites-enabled
      2. catyour_project_name
      3. 检查您的 nginx 路径 (对我来说:root/home/ubuntu/practice/current/public;)

      移动到主目录以检查您的项目名称
      4. 光盘
      5. ls
      6. 如果你的 ec2 项目名(对我来说:实践)与你的 nginx 路径名(对我来说:实践)不匹配,那么你可能会得到“index.html not found error”

      【讨论】:

        【解决方案6】:

        还有我的 server-blocks.conf

        server {
        
                listen 80;
        
                index index.html index.htm index.nginx-debian.html;
        
                server_name 13.xxx.xxx.xx;
        
                location / {
                root /var/www/portaladmin/;
        
        
                        proxy_pass http://13.xxx.xxx.xx:80/;
        
                }
                 error_log /var/log/nginx/portaladmin-erorr.log;
        
        }
        

        还有我的 load-balancer.conf

        server {
          listen 80;
          server_name xx.xxx.xxx.xx
          access_log /home/ec2-user/logs/lb-access.log;
          error_log /home/ec2-user/logs/lb-error.log;
        
          location / {
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_pass http://13.xxx.xxx.xx:80;
                   }
        }
        

        【讨论】:

        • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center。
        猜你喜欢
        • 1970-01-01
        • 2016-06-23
        • 2016-12-27
        • 2018-03-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-05-03
        相关资源
        最近更新 更多