【问题标题】:configuring django with nginx on AWS EC2 Instance在 AWS EC2 实例上使用 nginx 配置 django
【发布时间】:2019-01-19 04:31:14
【问题描述】:

我目前正在使用 nginx 配置 django 应用程序以在 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 的外观的 sn-p

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 在我的应用程序命名空间中的 urls 模式遇到了问题。

任何帮助将不胜感激。

(注意:我已经用标准的 python manage.py runserver 0.0.0.0:8000 运行该站点一个月,到目前为止它工作正常。)

【问题讨论】:

  • 我觉得这其实和nginx没什么关系。查看您的日志,我认为您应该检查您的 django 代码,/%7B$%20static%20'js/main.js'%20%%7D 是{ static 'js/main.js' } 的 url 转义 str,这表明您的 django 模板无法正常工作。
  • @hcheung 出现语法错误?我只是对静态文件在本地和开发服务器上的工作方式感到困惑,但在生产服务器上却没有。
  • 正如我所说,您的 nginx 设置看起来不错(除了可以进行一些微调,但通常还可以)。 Nginx 只解析传递给它的内容,不多也不少。如果它是在开发服务器上工作而不是在生产上工作,请检查浏览器开发者控制台下的 url 组合以查看差异并回溯到 django 源代码。

标签: python django amazon-web-services nginx amazon-ec2


【解决方案1】:

所以我意识到这与 URL 的编码有关。

当我点击链接时,在浏览器的地址栏中,我看到了

"GET /%7B$%%20url%20'members:member_login'%20%%7D HTTP/1.1" 400 173 "-" "-" "-"

如果我将其简化为:

/url'members:member_login'

它有效。这是因为我删除了当您向 URL 发出请求时添加的所有特殊字符。

所以我对我的问题没有完整的答案,因为我不确定如何让 nginx/django 自动解析这些问题。但我在正确的轨道上。

【讨论】:

    猜你喜欢
    • 2019-07-08
    • 1970-01-01
    • 2015-05-03
    • 2013-12-19
    • 2023-03-06
    • 1970-01-01
    • 2017-10-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多