下面是我的nginx配置文件。
在default.conf上,第一个位置是用来访问/usr/share/nginx/html目录的,访问http://47.91.152.99也可以。但是,当我为目录/usr/share/ nginx /public directory添加一个新位置时,当我访问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;
}
}
发布于 2016-12-12 12:59:51
下面的错误块(在你的例子中);
location ^~ /test/ {
root /usr/share/nginx/public;
index index.html index.htm;
}
正在告诉nginx在文件夹(root) /usr/share/nginx/public中查找目录'test‘。如果根目录中没有'test‘文件夹,它将返回404。为了解决您的问题,我建议您尝试使用别名而不是根。如下所示:
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
。在理想的情况下,这将设置在顶部,紧跟在常规服务器块设置之后。
希望能有所帮助。
发布于 2018-11-11 09:11:02
root指令导致的错误
location ^~ /test/ {
root /usr/share/nginx/public;
index index.html index.htm;
}
使用alias指令修复
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也会向上移动。订单是
更多关于nginx location priority的信息。此外,您还可以随时查看官方文档。位置块http://nginx.org/en/docs/http/ngx_http_core_module.html#location的nginx文档
发布于 2020-06-22 13:36:41
当你的应用是vuejs时,你需要这样写,可以防止404,注意双/test/
location ^~/test/ {
alias /usr/local/soft/vuejs/;
try_files $uri $uri/ /test/index.html;
}
https://stackoverflow.com/questions/41099318
复制相似问题