我目前正在配置一个 django 应用程序以使用 nginx 在 prod 环境中运行。我真的一直在努力解决这个问题,感觉好像我已经搜索了所有的堆栈溢出。
这是我的应用程序的 nginx 配置文件
# the upstream component nginx needs to connect to
upstream django {
server unix:/tmp/uwsgi.sock;
# server 127.0.0.1:8001; # for a web port socket (we'll use this first)
}
# configuration of the server
server {
# the port your site will be served on
listen 8000;
# the domain name it will serve for
server_name 54.172.211.18 172.31.38.120 unclique.io; # substitute your machine's IP address or FQDN
charset utf-8;
# max upload size
client_max_body_size 75M; # adjust to taste
# Django media
location /media {
alias /path/to/your/mysite/media; # your Django project's media files - amend as required
}
location /static {
alias /home/ec2-user/UnClique/UnClique/static/; # your Django project's static files - amend as required
}
# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass unix://tmp/uwsgi.sock;
include /home/ec2-user/UnClique/config/uwsgi_params; # the uwsgi_params file you installed
}
}
着陆页看起来很好(看起来)。但是,当我单击应将我带到网站上其他页面的链接时,我从 nginx 收到 400 Bad Request 错误。
这是我的 error.logs 的片段
108.51.36.126 - - [12/Aug/2018:02:24:16 +0000] "GET /%7B$%20static%20'js/main.js'%20%%7D HTTP/1.1" 400 173 "-" "-" "-"
108.51.36.126 - - [12/Aug/2018:02:24:16 +0000] "GET /%7B$%20static%20'js/main.js'%20%%7D HTTP/1.1" 400 173 "-" "-" "-"
108.51.36.126 - - [12/Aug/2018:02:24:18 +0000] "GET /%7B$%%20url%20'members:member_signup'%20%%7D HTTP/1.1" 400 173 "-" "-" "-"
108.51.36.126 - - [12/Aug/2018:02:24:20 +0000] "GET /%7B$%%20url%20'members:member_login'%20%%7D HTTP/1.1" 400 173 "-" "-" "-"
108.51.36.126 - - [12/Aug/2018:02:26:25 +0000] "GET / HTTP/1.1" 200 2787 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36" "-"
108.51.36.126 - - [12/Aug/2018:02:26:25 +0000] "GET /%7B$%20static%20'js/main.js'%20%%7D HTTP/1.1" 400 173 "-" "-" "-"
108.51.36.126 - - [12/Aug/2018:02:26:25 +0000] "GET /%7B$%20static%20'js/main.js'%20%%7D HTTP/1.1" 400 173 "-" "-" "-"
108.51.36.126 - - [12/Aug/2018:02:26:26 +0000] "GET /%7B$%%20url%20'members:member_signup'%20%%7D HTTP/1.1" 400 173 "-" "-" "-"
108.51.36.126 - - [12/Aug/2018:02:26:28 +0000] "GET /%7B$%%20url%20'members:member_login'%20%%7D HTTP/1.1" 400 173 "-" "-" "-"
108.51.36.126 - - [12/Aug/2018:02:26:35 +0000] "GET /%7B$%%20url%20'members:member_signup'%20%%7D HTTP/1.1" 400 173 "-" "-" "-"
108.51.36.126 - - [12/Aug/2018:02:27:08 +0000] "GET / HTTP/1.1" 200 2787 "http://54.158.154.23:8000/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36" "-"
108.51.36.126 - - [12/Aug/2018:02:27:08 +0000] "GET /%7B$%20static%20'js/main.js'%20%%7D HTTP/1.1" 400 173 "-" "-" "-"
108.51.36.126 - - [12/Aug/2018:02:27:08 +0000] "GET /%7B$%20static%20'js/main.js'%20%%7D HTTP/1.1" 400 173 "-" "-" "-"
108.51.36.126 - - [12/Aug/2018:02:31:02 +0000] "GET / HTTP/1.1" 200 2787 "http://54.158.154.23:8000/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36" "-"
108.51.36.126 - - [12/Aug/2018:02:31:02 +0000] "GET /%7B$%20static%20'js/main.js'%20%%7D HTTP/1.1" 400 173 "-" "-" "-"
108.51.36.126 - - [12/Aug/2018:02:31:02 +0000] "GET /%7B$%20static%20'js/main.js'%20%%7D HTTP/1.1" 400 173 "-" "-" "-"
108.51.36.126 - - [12/Aug/2018:02:31:03 +0000] "GET /%7B$%%20url%20'members:member_signup'%20%%7D HTTP/1.1" 400 173 "-" "-" "-"
似乎 nginx 在处理我的应用程序命名空间中的 url 模式时遇到了问题。
如有任何帮助,我们将不胜感激。
(注意:我已经让网站使用标准 python manage.py runserver 0.0.0.0:8000 运行了一个月,到目前为止一切正常。)
请您参考如下方法:
所以我意识到它与 URL 的编码有关。
当我点击链接时,在浏览器的地址栏中,我看到
"GET /%7B$%%20url%20'members:member_login'%20%%7D HTTP/1.1" 400 173 "-" "-" "-"
如果我将其剥离为:
/url'members:member_login'
它有效。这是因为我删除了您向 URL 发出请求时添加的所有特殊字符。
所以我没有我的问题的完整答案,因为我不确定如何让 nginx/django 自动解析这些。但我走在正确的轨道上。
