查了好多资料都没有搞懂的几个问题。
第一个问题,如果我的location配置是这样的:
location /doc {
alias /home/user/doc;
}
那我访问http://localhost/doc/a.html的时候实际上nginx是读取了/home/usr/doc/a.html,如果我访问的是http://localhost/docs/a.html甚至是http://localhost/docsioajsfopajowejfasd那nginx实际上会尝试读取哪个文件?
第二个问题,如果我将doc配置成一个服务器,再反向代理。
server {
listen 8000;
server_name doc;
root /home/user/doc;
index index.html index.htm index.nginx-debian.html;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
}
在主服务器这样配置:
server {
listen 80;
....
location /doc {
proxy_pass http://localhost:8000/;
}
}
这样配置时访问http://localhost/doc/,如果index文件引用了静态文件,静态文件会变成404,浏览器会尝试获取http://localhost/js/xxx.js而不是http://localhost/doc/js/xxx.js,如果在pattern后面加上/变成
location /doc/ {
proxy_pass http://localhost:8000/;
}
就没有问题,但如果是第一个问题中的location配置,浏览器会正确寻找http://localhost/doc/js/xxx.js。这搞得我很困惑,结尾加不加/究竟有什么影响?为什么alias和proxy_pass会出现不同的结果?
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
不是root和proxy_pass的区别,而是proxy_pass有一个特殊逻辑,如果proxy_pass后面有路径就会触发它,注意一个“/”也是路径,如下图。
proxy_pass http://127.0.0.1/;
proxy_pass http://127.0.0.1;
官方资料:
http://nginx.org/en/docs/http...
Syntax: proxy_pass URL;
Sets the protocol and address of a proxied server and an optional URI(修改成PATH更加准确,和便于理解) to which a location should be mapped.