1

我需要在 Django 中从后端向前端推送消息,最后我找到了这个 Twisted + Django 服务器,但是在产品环境中我们使用 nginx 代理对 Django 的请求,我很困惑hendrix和 nginx 可以一起工作吗?

如果是,那么如何编写nginx.conf?

4

2 回答 2

0

是的,您可以使用 nginx 代理传递到 hendrix,就像使用任何其他 WSGI/ASGI 容器一样。

这个问题有执行此操作的示例配置。

于 2017-01-19T20:53:41.757 回答
0

今天我尝试将我原来的 nginx 配置更改为反向代理 hendrix,这里有一些片段:

   upstream django {
        server localhost:8000;
    }
    server {
    listen 80;
    server_name localhost;

    location /static/ {
            #root /var/www;
            alias /var/www/static;
    }

    location / {
            proxy_pass http://django;
            proxy_pass_header Server;
            proxy_set_header Host $host;
            proxy_redirect off;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
    }
    }

然后我重新启动 nginx 并启动 hendrix: hx start --log ./test.log

它终于奏效了,但我发现了一些警告信息:*

WebSocket 连接到“...”失败:WebSocket 握手期间出错:“连接”标头值不是“升级”:keep-alive socket.io.js:2371

*

在一些谷歌消息之后,我发现 nginx 版本必须更高 1.3.13,所以我将 nginx 更新到 1.10.1(稳定),警告消息消失。

但是如果想在我的生产环境中使用 hendrix,需要多个 hendrix-worker,所以我尝试:hx start -w 2,输出失败:

root@proxy:/vagrant/hendrix_taste/hendrix/examples/django_hx_chatserver/example_app# hx start -w 2                                                                                                                                              
Adding media resource for URL 'static' at path '/vagrant/hendrix_taste/hendrix/examples/django_hx_chatserver/example_app/servedstatics'                                                                                                         
...
Adding media resource for URL 'static/admin' at path '/usr/local/lib/python2.7/dist-
'/vagrant/hendrix_taste/hendrix/examples/django_hx_chatserver/example_app/chat/static/chat/js'                                                                                           
Ready and Listening on port 8000...                                                                                                                                                                                                             
{0: 0, 1: 1, 2: 2, 4: 4}                                                                                                                                                                                                                        
{'main_web_tcp': 4}                                                                                                                                                                                                                             
Unhandled Error                                                                                                                                                                                                                                 
Traceback (most recent call last):                                                                                                                                                                                                              
  File "/usr/local/lib/python2.7/dist-packages/hendrix/deploy/base.py", line 180, in run                                                                                                                                                        
    getattr(self, action)(fd)                                                                                                                                                                                                                   
  File "/usr/local/lib/python2.7/dist-packages/hendrix/deploy/base.py", line 226, in start                                                                                                                                                      
    self.launchWorkers(pids)                                                                                                                                                                                                                    
  File "/usr/local/lib/python2.7/dist-packages/hendrix/deploy/base.py", line 265, in launchWorkers                                                                                                                                              
    DeployServerProtocol(args), 'hx', args, childFDs=self.childFDs, env=environ                                                                                                                                                                 
  File "/usr/local/lib/python2.7/dist-packages/twisted/internet/posixbase.py", line 340, in spawnProcess                                                                                                                                        
    processProtocol, uid, gid, childFDs)                                                                                                                                                                                                        
--- <exception caught here> ---                                                                                                                                                                                                                 
  File "/usr/local/lib/python2.7/dist-packages/twisted/internet/process.py", line 756, in __init__                                                                                                                                              
    self.proto.makeConnection(self)                                                                                                                                                                                                             
exceptions.AttributeError: 'DeployServerProtocol' object has no attribute 'makeConnection'                                                                                                                                                      

Unhandled Error                                                                                                                                                                                                                                 
Traceback (most recent call last):                                                                                                                                                                                                              
  File "/usr/local/lib/python2.7/dist-packages/hendrix/deploy/base.py", line 180, in run                                                                                                                                                        
    getattr(self, action)(fd)                                                                                                                                                                                                                   
  File "/usr/local/lib/python2.7/dist-packages/hendrix/deploy/base.py", line 226, in start                                                                                                                                                      
    self.launchWorkers(pids)                                                                                                                                                                                                                    
  File "/usr/local/lib/python2.7/dist-packages/hendrix/deploy/base.py", line 265, in launchWorkers                                                                                                                                              
    DeployServerProtocol(args), 'hx', args, childFDs=self.childFDs, env=environ                                                                                                                                                                 
  File "/usr/local/lib/python2.7/dist-packages/twisted/internet/posixbase.py", line 340, in spawnProcess                                                                                                                                        
    processProtocol, uid, gid, childFDs)                                                                                                                                                                                                        
--- <exception caught here> ---                                                                                                                                                                                                                 
  File "/usr/local/lib/python2.7/dist-packages/twisted/internet/process.py", line 756, in __init__                                                                                                                                              
    self.proto.makeConnection(self)                                                                                                                                                                                                             
exceptions.AttributeError: 'DeployServerProtocol' object has no attribute 'makeConnection'                                                                                                                                                      

Adding media resource for URL 'static' at path '/vagrant/hendrix_taste/hendrix/examples/django_hx_chatserver/example_app/servedstatics'                                                                                                         
...                                                                                   
Ready and Listening on port 8000...                                                                                                                                                                                                             
Unhandled error in Deferred:                                                                                                                                                                                                                    


Traceback (most recent call last):                                                                                                                                                                                                              
  File "/usr/local/lib/python2.7/dist-packages/twisted/internet/defer.py", line 317, in addCallback                                                                                                                                             
    callbackKeywords=kw)                                                                                                                                                                                                                        
  File "/usr/local/lib/python2.7/dist-packages/twisted/internet/defer.py", line 306, in addCallbacks                                                                                                                                            
    self._runCallbacks()                                                                                                                                                                                                                        
  File "/usr/local/lib/python2.7/dist-packages/twisted/internet/defer.py", line 588, in _runCallbacks                                                                                                                                           
    current.result = callback(current.result, *args, **kw)                                                                                                                                                                                      
  File "/usr/local/lib/python2.7/dist-packages/twisted/internet/defer.py", line 1313, in execute                                                                                                                                                
    d = maybeDeferred(f, *args, **kwargs)                                                                                                                                                                                                       
--- <exception caught here> ---                                                                                                                                                                                                                 
  File "/usr/local/lib/python2.7/dist-packages/twisted/internet/defer.py", line 150, in maybeDeferred                                                                                                                                           
    result = f(*args, **kw)                                                                                                                                                                                                                     
exceptions.TypeError: _addSubprocess() takes exactly 4 arguments (5 given)                                                                                                                                                                      
Temporarily disabling observer <twisted.logger._filter.FilteringLogObserver object at 0x20bac50> due to exception: [Failure instance: Traceback: <type 'exceptions.AttributeError'>: 'NoneType' object has no attribute '_formatter_parser'     
/usr/local/lib/python2.7/dist-packages/hendrix/deploy/base.py:283:addSubprocess                                                                                                                                                                 
/usr/local/lib/python2.7/dist-packages/twisted/internet/defer.py:707:__del__                                                                                                                                                                    
/usr/local/lib/python2.7/dist-packages/twisted/logger/_logger.py:178:failure                                                                                                                                                                    
/usr/local/lib/python2.7/dist-packages/twisted/logger/_logger.py:132:emit                                                                                                                                                                       
--- <exception caught here> ---                                                                                                                                                                                                                 
/usr/local/lib/python2.7/dist-packages/twisted/logger/_observer.py:131:__call__                                                                                                                                                                 
/usr/local/lib/python2.7/dist-packages/twisted/logger/_filter.py:131:__call__                                                                                                                                                                   
/usr/local/lib/python2.7/dist-packages/twisted/logger/_file.py:50:__call__                                                                                                                                                                      
/usr/local/lib/python2.7/dist-packages/twisted/logger/_json.py:245:<lambda>                                                                                                                                                                     
/usr/local/lib/python2.7/dist-packages/twisted/logger/_json.py:196:eventAsJSON                                                                                                                                                                  
/usr/local/lib/python2.7/dist-packages/twisted/logger/_flatten.py:87:flattenEvent                                                                                                                                                               
/usr/lib/python2.7/string.py:621:parse                                                                                                                                                                                                          
]                                                                                                                                                                                                                                               

Traceback (most recent call last):                                                                                                                                                                                                              
  File "/usr/local/lib/python2.7/dist-packages/hendrix/deploy/base.py", line 283, in addSubprocess                                                                                                                                              
    self._lock.run(self._addSubprocess, self, fds, name, factory)                                                                                                                                                                               
  File "/usr/local/lib/python2.7/dist-packages/twisted/internet/defer.py", line 707, in __del__                                                                                                                                                 
    debugInfo=debugInfo)                                                                                                                                                                                                                        
  File "/usr/local/lib/python2.7/dist-packages/twisted/logger/_logger.py", line 178, in failure                                                                                                                                                 
    self.emit(level, format, log_failure=failure, **kwargs)                                                                                                                                                                                     
  File "/usr/local/lib/python2.7/dist-packages/twisted/logger/_logger.py", line 132, in emit                                                                                                                                                    
    self.observer(event)                                                                                                                                                                                                                        
--- <exception caught here> ---                                                                                                                                                                                                                 
  File "/usr/local/lib/python2.7/dist-packages/twisted/logger/_observer.py", line 131, in __call__                                                                                                                                              
    observer(event)                                                                                                                                                                                                                             
  File "/usr/local/lib/python2.7/dist-packages/twisted/logger/_filter.py", line 131, in __call__                                                                                                                                                
    self._observer(event)                                                                                                                                                                                                                       
  File "/usr/local/lib/python2.7/dist-packages/twisted/logger/_file.py", line 50, in __call__                                                                                                                                                   
    text = self.formatEvent(event)                                                                                                                                                                                                              
  File "/usr/local/lib/python2.7/dist-packages/twisted/logger/_json.py", line 245, in <lambda>                                                                                                                                                  
    lambda event: u"{0}{1}\n".format(recordSeparator, eventAsJSON(event))                                                                                                                                                                       
  File "/usr/local/lib/python2.7/dist-packages/twisted/logger/_json.py", line 196, in eventAsJSON                                                                                                                                               
    flattenEvent(event)                                                                                                                                                                                                                         
  File "/usr/local/lib/python2.7/dist-packages/twisted/logger/_flatten.py", line 87, in flattenEvent                                                                                                                                            
    aFormatter.parse(event["log_format"])                                                                                                                                                                                                       
  File "/usr/lib/python2.7/string.py", line 621, in parse                                                                                                                                                                                       
    return format_string._formatter_parser()                                                                                                                                                                                                    
exceptions.At

我的 python 包: pip freeze

Warning: cannot find svn location for distribute==0.6.24dev-r0
Automat==0.3.0
Django==1.8.11
GnuPGInterface==0.3.2
Jinja2==2.9.4
MarkupSafe==0.23
PyYAML==3.12
Twisted==16.0.0
apt-xapian-index==0.44
argh==0.26.2
argparse==1.2.1
attrs==16.3.0
characteristic==14.3.0
chardet==2.0.1
command-not-found==0.2.44
configobj==4.7.2
constantly==15.1.0
decorator==3.3.2
## FIXME: could not find svn URL in dependency_links for this package:
distribute==0.6.24dev-r0
hendrix==2.0.2
incremental==16.10.1
ipython==0.12.1
language-selector==0.1
pathtools==0.1.2
pexpect==2.3
pyOpenSSL==16.2.0
pyasn1==0.1.9
pyasn1-modules==0.0.8
pychalk==0.0.5
python-apt==0.8.3ubuntu7.1
python-debian==0.1.21ubuntu1
service-identity==16.0.0
simplegeneric==0.7
six==1.10.0
txsockjs==1.2.2
ufw==0.31.1-1
virtualenv==15.1.0
watchdog==0.8.3
wsgiref==0.1.2
zope.interface==4.0.5
于 2017-01-20T05:45:04.227 回答