【问题标题】:Can Hendrix and Nginx work together?Hendrix 和 Nginx 可以一起工作吗?
【发布时间】:2017-01-19 12:27:52
【问题描述】:

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

如果是,那么nginx.conf怎么写?

【问题讨论】:

    标签: nginx hendrix


    【解决方案1】:

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

    This Question 有执行此操作的示例配置。

    【讨论】:

    • 太好了,今天尝试使用nginx反向代理hendrix成功~
    • 太好了,今天我尝试使用nginx反向代理hendrix成功~但是如果我想在我的产品环境中使用hendrix,我需要部署multi hendrix worker,但是我发现hendrix不支持读取从 sock 文件,当我尝试 hx start -w 4 时,我得到了错误
    【解决方案2】:

    今天我尝试将我原来的 nginx 配置更改为反向代理 hendrix,这是一些 sn-ps:

       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 期间出错 握手:“连接”标头值不是“升级”:保持活动状态 socket.io.js:2371

    *

    经过一些谷歌消息,我发现 nginx 版本必须更高 1.3.13,所以我将 nginx 更新到 1.10.1(stable),警告消息消失了。

    但如果想在我的生产环境中使用 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 包: 点冻结

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-07-09
      • 2016-01-29
      • 2017-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-25
      相关资源
      最近更新 更多