【问题标题】:nginx/uwsgi don't serve any requestnginx/uwsgi 不提供任何请求
【发布时间】:2014-07-15 04:11:57
【问题描述】:

我有 nginx、uwsgi、django 堆栈。曾经有一个来自 nginx 的 https 服务,但我正在尝试禁用它。

这是我的 nginx 配置文件:

nginx.conf:

events {
worker_connections 768;
# multi_accept on;
}

http {

proxy_buffer_size 128k;
proxy_buffers 4 256k;
proxy_busy_buffers_size 256k;

##
# Basic Settings
##

sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# server_tokens off;

# server_names_hash_bucket_size 64;
# server_name_in_redirect off;

include /etc/nginx/mime.types;
default_type application/octet-stream;

##
# Logging Settings
##

access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;

##
# Gzip Settings
##

gzip on;
gzip_disable "msie6";

# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

##
# nginx-naxsi config
##
# Uncomment it if you installed nginx-naxsi
##

#include /etc/nginx/naxsi_core.rules;

##
# nginx-passenger config
##
# Uncomment it if you installed nginx-passenger
##

#passenger_root /usr;
#passenger_ruby /usr/bin/ruby;

##
# Virtual Host Configs
##

include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}

网站可用/mysite.com:

upstream django {
server 127.0.0.1:44000;
}

server {
listen          80 default;
server_name     www.mysite.com mysite.com;


charset         utf-8;
access_log      /var/log/nginx/mysite_access.log;
error_log       /var/log/nginx/mysite_error.log;

location /static/ {
    root   /opt/myapp/myapp/;
    index  index.html index.htm;
}

location / {
  uwsgi_pass  django;
  include     uwsgi_params;
}
}

当我用 uwsgi 运行它时,我得到了这个错误:

django.core.exceptions.SuspiciousOperation: Invalid HTTP_HOST header (you may need to set ALLOWED_HOSTS): www.mysite.com
[pid: 12865|app: 0|req: 1/2] 84.94.36.146 () {36 vars in 571 bytes} [Mon May 26 07:12:55 2014] GET /favicon.ico => generated 0 bytes in 457 msecs (HTTP/1.1 500) 0 headers in 0 bytes (0 switches on core 0)

我将“www.mysite.com”放在 settings.py 的 ALLOWED_HOSTS 部分,但没有帮助。 这些是我在 settings.py 中添加的行:

ALLOWED_HOSTS = [
'.mysite.com', # Allow domain and subdomains
'.mysite.com.', # Also allow FQDN and subdomains
]

当我用 runserver 运行它时,我得到了这个错误:

[26/May/2014 06:53:09] code 400, message Bad request syntax ('\x00;\x02\x00\x0c\x00QUERY_STRING\x00\x00\x0e\x00REQUEST_METHOD\x03\x00GET\x0c\x00CONTENT_TYPE\x00\x00\x0e\x00CONTENT_LENGTH\x00\x00\x0b\x00REQUEST_URI\x0c\x00/favicon.ico\t\x00PATH_INFO\x0c\x00/favicon.ico\r\x00DOCUMENT_ROOT\x0f\x00/etc/nginx/html\x0f\x00SERVER_PROTOCOL\x08\x00HTTP/1.1\x0b\x00REMOTE_ADDR\x0c\x0084.94.36.146\x0b\x00REMOTE_PORT\x05\x0051792\x0b\x00SERVER_PORT\x02\x0080\x0b\x00SERVER_NAME\x10\x00www.mysite.com\t\x00HTTP_HOST\x10\x00www.mysite.com\x0f\x00HTTP_CONNECTION')
[26/May/2014 06:53:09] ";
                         QUERY_STRINGREQUEST_METHODGET
                                                      CONTENT_TYPECONTENT_LENGTH
                                                                                REQUEST_URI
                                                                                           /favicon.ico PATH_INFO
DOCUMENT_ROOT/etc/nginx/htmlSERVER_PROTOCOHTTP/1.1                                                               /favicon.ico
                                                  REMOTE_ADDR
                                                             84.94.36.146
                                                                         REMOTE_PORT51792
                                                                                         SERVER_PORT80
                                                                                                      SERVER_NAMEwww.mysite.com HTTP_HOSTwww.mysite.comHTTP_CONNECTION" 400 -

我读到这个错误可能是由 HTTPS 请求引起的,但在我看来,我没有将 HTTP 请求重定向到任何地方的 HTTPS,因为我删除了所有这些配置。

有什么想法吗?

谢谢

【问题讨论】:

  • 看起来您正在将请求传递给 django 运行服务器而不是 uWSGI。
  • @roberto 我不这么认为,如果请求没有到达 uWSGI,它不会在日志上显示任何内容。而且 uWSGI 和 runserver 都在监听 44000 端口。
  • 写入日志的不是 uWSGI。 uWSGI 有一个非常独特的日志格式。你看到的是来自 django runserver。不,您不能将两者都绑定在同一个端口上。必须使用 uWSGI 代替 runserver
  • 确保它们不会同时监听同一个端口,但是每次我激活其中一个时,它都会监听 44000 端口,该端口也被 nginx 使用。所以如果runserver从nginx得到请求,我想不出uWSGI为什么不...
  • 它们是两个不同的问题:生成 runserver 一个是因为您使用(来自 nginx)uwsgi 协议(而 runserver 使用 HTTP)。第二个是 HTTP_HOST 值与您的 django ALLOWED_HOSTS 设置不一致。由于您已经设置了它,我想您不是在浏览器栏中写域名,而是使用 localhost 或 nginx 实例的 ip。最终,您可以强制(仅用于测试)nginx 使用 uwsgi_param HTTP_HOST foobar 将 HTTP_HOST 设置为特定值;

标签: django nginx https uwsgi


【解决方案1】:

我通过使用解决了它

uwsgi --socket :8001 --wsgi-file test.py

而不是

python manage.py runserser

【讨论】:

    【解决方案2】:

    它现在正在工作。 问题是 ALLOWED_HOSTS 已经在 local_settings.py 中用旧域定义了。我更新了域并且它起作用了。 所有的nginx配置都是正确的,所以这里附加的文件很有用。 感谢您的宝贵时间。

    【讨论】:

      猜你喜欢
      • 2015-12-29
      • 2016-03-05
      • 1970-01-01
      • 2014-08-03
      • 2013-02-19
      • 1970-01-01
      • 2012-05-31
      • 2019-09-11
      相关资源
      最近更新 更多