gpt4 book ai didi

nginx 位置 404 未找到

转载 作者:行者123 更新时间:2023-12-02 22:11:44 30 4
gpt4 key购买 nike

这是我的 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;
}
}

最佳答案

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

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

告诉 nginx 在文件夹(root)/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;
}
}

您还应该考虑的一件事...位置 block 越“精确”,它在您的配置中的位置就应该越高。就像 location =/50x.html 一样。在完美的世界中,它将设置在顶部,就在常规服务器 block 设置之后。

希望有帮助。

关于nginx 位置 404 未找到,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41099318/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com