未找到nginx位置404

jam*_*.g. 12 location nginx http-status-code-404

这是我的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;
    }
}
Run Code Online (Sandbox Code Playgroud)

Old*_*art 17

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

 location ^~ /test/ {
     root /usr/share/nginx/public;
     index index.html index.htm;
 }
Run Code Online (Sandbox Code Playgroud)

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

 location ^~ /test/ {
     alias /usr/share/nginx/public;
     index index.html index.htm;
 }
Run Code Online (Sandbox Code Playgroud)

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

 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;
     }
 }
Run Code Online (Sandbox Code Playgroud)

还有一件事你应该考虑......位置块越"精确",它应该存在的配置越高.像那样location = /50x.html.在一个完美的世界中,这将在通用服务器块设置之后立即设置.

希望能帮助到你.

  • 它不起作用。我仍然收到 404 not found 响应。 (2认同)
  • 在/usr/share/nginx/public下创建test目录后,响应正常。谢谢你的回复。 (2认同)

gtz*_*lla 9

由 root 指令引起的错误

location ^~ /test/ {
    root /usr/share/nginx/public;
    index index.html index.htm;
}
Run Code Online (Sandbox Code Playgroud)

使用别名指令修复

 location ^~ /test/ {
     alias /usr/share/nginx/public;
     index index.html index.htm;
 }
Run Code Online (Sandbox Code Playgroud)

其他改进

额外提示:可以设置 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;
    }
}
Run Code Online (Sandbox Code Playgroud)

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

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

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