【发布时间】:2016-09-24 11:54:53
【问题描述】:
我在一台服务器上托管多个 django 应用程序。
用户访问时:http://dev-app.example.com/testapp1
我需要使用从 $uri 动态生成的根路径来提供 /static,因为我没有在应用程序中使用相同的资产,而是使用相同的路径。
我的 nginx 配置文件树:
nginx
├── sites-enabled
│ ├── myconf
│ myapps/
│ ├── testapp1
│ └── testapp2
我的配置文件:
server {
listen 8088;
server_name dev-app.example.com;
location = favicon.ico { access_log off; log_not_found off; }
location /static
# The line below isn't working even if the $uri has the string I want to concatenate
root /home/user$uri/current;
}
include /etc/nginx/myapps/*;
}
testapp1 文件:
location /testapp1 {
include uwsgi_params;
uwsgi_pass unix:/home/user/testapp1/current/testapp1.sock;
}
testapp2 文件:
location /testapp2 {
include uwsgi_params;
uwsgi_pass unix:/home/user/testapp2/current/testapp2.sock;
}
我想使用 nginx 内置变量 $uri 相应地为每个请求的应用程序在 myconf 文件中构建 /static 位置的根路径。
当用户打开http://dev-app.example.com/whatever 我想提供这个:
# In the myconf file
location /static {
root /home/user/whatever/current;
}
【问题讨论】:
-
你能展示更大的 nginx 配置吗?我不明白这应该如何工作......
-
@GwynBleidD 我刚刚做了,谢谢。
-
检查是否理解正确,您的应用程序在
//yourdomain.com/testapp1和//yourdomain.com/testapp2上提供服务,但对于这两个应用程序,您的静态文件位于//yourdomain.com/static。对吗? -
@GwynBleidD 是的,静态文件必须在 //yourdomain.com/static 中,这样每个应用程序都可以拥有自己的资产。 /static 将始终是 testapp1 和 testapp2 中的静态文件的位置,但 /static 重定向到的根路径是根据用户的请求相应更改的位置。