【发布时间】:2017-09-17 17:19:22
【问题描述】:
我正在关注Setting up Django and your web server with uWSGI and nginx 教程。我被困在Configure nginx for your site 部分。我尝试了所有在互联网上找到的东西,主要是我试图修复我的配置 mysite_nginx.conf 文件,但现在看来它一直都是正确的。
这是我的配置文件 mysite_nginx.conf,当然是从 /usr/local/etc/nginx/sites-enabled/ 符号链接的。
# mysite_nginx.conf
# the upstream component nginx needs to connect to
upstream django {
server 127.0.0.1:8001;
}
# configuration of the server
server {
listen 8000;
server_name localhost;
charset utf-8;
client_max_body_size 75M;
location /media {
alias /Users/username/dev/my_site/mysite/media;
}
location /static {
alias /Users/username/dev/my_site/mysite/static;
}
location / {
uwsgi_pass django;
include /Users/username/dev/my_site/mysite/uwsgi_params;
}
}
我遇到的问题
访问localhost:8000/media/img.jpg 或localhost:8000/static/img.jpg 总是返回404 Not Found。在 nginx 日志中,所有请求都映射到 /usr/local/Cellar/nginx/version/html/,这是指向 /usr/local/var/www/ 的符号链接,其中存在 index.html 和 5xx.html 文件。
我的static/ 和media/ 被添加到/usr/local/Cellar/nginx/version/html/ 所以在nginx 错误日志中我看到了对/usr/local/Cellar/nginx/version/html/static/img.jpg 的请求。
对我有用的解决方案
在nginx.conf 文件中,我包含了启用站点的路径。在此之后,我的所有请求都映射到了我作为位置别名添加的绝对路径,并且一切都按预期工作。
...
http {
include mime.types;
include /usr/local/etc/nginx/sites-enabled/*.conf;
default_type application/octet-stream;
...
问题
在我提到的教程中没有提到这一点,所以我想它应该在不编辑nginx.conf 文件的情况下工作?但它对我不起作用,我错过了什么吗?
【问题讨论】: