我的网页程序在 app1 目录下, 当用 http://server.com/app1/ 访问时,一切正常,但是用 http://server.com/app1 访问时,页面里的超链接会漏掉这个二级目录,例如
<link type="text/css" rel="stylesheet" href="static/style.css" />
指向的是 http://server.com/static/sytle.css 而不是 http://server.com/app1/static/sytle.c... 导致资源获取失败。
我知道可以通过添加规则来跳转
rewrite ^([^.]*[^/])$ $1/ permanent;
但是这样就使url上多出了一个斜杠,有没有其他办法?
以下是我的nginx配置:
set $dir /home/user/apps;
location ~ static/(.*)$ {
alias $dir/$uri;
}
location ~ ^/([^/]+) {
set $app $1;
include uwsgi_params;
uwsgi_pass unix:///tmp/uwsgi_vhosts.sock;
uwsgi_param UWSGI_CHDIR $dir/$app/;
uwsgi_param UWSGI_PYHOME $dir/$app/;
uwsgi_param PATH_INFO /;
uwsgi_param UWSGI_SCRIPT index;
uwsgi_param SERVER_NAME $server_name.$app;
}
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
正如评论里 DaiJie 所说的,出现这个问题的非常正常,web server 和浏览器默认行为是正确的。
建议题主使用绝对路径或者含域名的完整地址来引用静态资源。