找不到nginx位置404 [英] nginx location 404 not found

查看:403
本文介绍了找不到nginx位置404的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的Nginx配置文件.

Here are my nginx configure files.

在default.conf上,第一个位置用于访问/usr/share/nginx/html目录,在我访问 http://47.91.152.99 . 但是,当我为目录/usr/share/nginx/public目录添加新位置时,nginx在我访问 http://47.91.152.99/test .

On the default.conf, the first location is used to access /usr/share/nginx/html directory, it is ok while I access http://47.91.152.99. But when I add up a new location for directory /usr/share/nginx/public directory, nginx return me a 404 page while I access http://47.91.152.99/test.

那怎么了?我会滥用nginx的指令吗?

So, what is the matter? Am I misuse the directive of 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;
    }
}

推荐答案

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

The following erroneous block (in your case);

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

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

is telling nginx to look for directory 'test' into the folder (root) /usr/share/nginx/public. If there's no 'test' folder in that root, it will return 404. To paliate to your problem, i suggest you try using alias instead of root. Like so:

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

此外,仅针对踢操作,通常可以设置index指令,因此您不必一直都在重写它……像这样;

Also, just for kicks, index directive can be set generally so you don't have to re-write it all the time... like so;

 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.在理想的情况下,应该在常规服务器块设置之后立即进行设置.

One thing you should also consider... the more 'precise' the location block, the higher in your config it should reside. Like that location = /50x.html. In a perfect world, that would be set up top, right after the general server block settings.

希望有帮助.

这篇关于找不到nginx位置404的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆