nginx之无法使用 Nginx + Pyramid + Gunicorn 运行两个站点

lhb25 阅读:15 2024-08-05 10:48:10 评论:0

我在我的 Web 服务器上使用捆绑 Nginx + Gunicorn + Pserve (Pyramid) 来运行多个站点。确切地说,现在只有两个站点在运行。如果我说得对,Nginx 监听 80 端口并在 5000 和 5010 端口上代理对 Gunicorn 的请求,这是我的两个域的 nginx 配置:

# $ cat cat /etc/nginx/sites-available/domain1.ru 
upstream domain1.ru { 
    server 0.0.0.0:5000; 
    server 0.0.0.0:5001; 
} 
 
server { 
    listen 80; 
    server_name www.domain1.ru domain1.ru; 
    error_log  /var/log/nginx/domain1.ru.error.log; 
    access_log  /var/log/nginx/domain1.ru.access.log; 
 
    location / { 
        try_files $uri @proxy_to_app; 
    } 
 
    location @proxy_to_app { 
        proxy_set_header        Host $http_host; 
        proxy_set_header        X-Real-IP $remote_addr; 
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for; 
        proxy_set_header        X-Forwarded-Proto $scheme; 
 
        client_max_body_size    10m; 
        client_body_buffer_size 128k; 
        proxy_connect_timeout   60s; 
        proxy_send_timeout      90s; 
        proxy_read_timeout      90s; 
        proxy_buffering         off; 
        proxy_temp_file_write_size 64k; 
        proxy_pass      http://127.0.0.1:5000; 
        proxy_redirect          off; 
    } 
} 
 
 
# $ cat cat /etc/nginx/sites-available/domain2.ru 
upstream domain2.ru { 
    server 0.0.0.0:5010; 
    server 0.0.0.0:5011; 
} 
 
server { 
    listen 80; 
    server_name www.domain2.ru domain2.ru; 
    error_log  /var/log/nginx/domain2.ru.error.log; 
    access_log  /var/log/nginx/domain2.ru.access.log; 
 
    location / { 
        try_files $uri @proxy_to_app; 
    } 
 
    location @proxy_to_app { 
        proxy_set_header        Host $http_host; 
        proxy_set_header        X-Real-IP $remote_addr; 
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for; 
        proxy_set_header        X-Forwarded-Proto $scheme; 
 
        client_max_body_size    10m; 
        client_body_buffer_size 128k; 
        proxy_connect_timeout   60s; 
        proxy_send_timeout      90s; 
        proxy_read_timeout      90s; 
        proxy_buffering         off; 
        proxy_temp_file_write_size 64k; 
        proxy_pass      http://127.0.0.1:5010; 
        proxy_redirect          off; 
    } 
} 

这两个站点运行良好,但是当我添加相同的配置时,除了将端口更改为 5015 和 5016 之外,出现了问题。当我刚开始 pserve 过程时,我得到了一个错误:

$ pserve production.ini --reload --log-file=domain3.ru-pserve.log 
Starting subprocess with file monitor 
Traceback (most recent call last): 
  File "/var/www/eba/data/gs/env/bin/pserve", line 9, in <module> 
    load_entry_point('pyramid==1.4.5', 'console_scripts', 'pserve')() 
  File "/var/www/eba/data/gs/env/lib/python2.7/site-packages/pkg_resources.py", line 378, in load_entry_point 
    return get_distribution(dist).load_entry_point(group, name) 
  File "/var/www/eba/data/gs/env/lib/python2.7/site-packages/pkg_resources.py", line 2566, in load_entry_point 
    return ep.load() 
  File "/var/www/eba/data/gs/env/lib/python2.7/site-packages/pkg_resources.py", line 2260, in load 
    entry = __import__(self.module_name, globals(),globals(), ['__name__']) 
  File "/var/www/eba/data/gs/env/lib/python2.7/site-packages/pyramid/scripts/pserve.py", line 28, in <module> 
    from pyramid.compat import WIN 
  File "/var/www/eba/data/gs/env/lib/python2.7/site-packages/pyramid/compat.py", line 114, in <module> 
    from urllib2 import urlopen as url_open 
  File "/var/www/eba/data/gs/env/opt/Python-2.7.3/lib/python2.7/urllib2.py", line 94, in <module> 
    import httplib 
  File "/var/www/eba/data/gs/env/opt/Python-2.7.3/lib/python2.7/httplib.py", line 69, in <module> 
    from array import array 
ImportError: /var/www/eba/data/gs/env/lib/python2.7/lib-dynload/array.so: cannot open shared object file: Too many open files in system 

当我再次运行 pserve 时,我又遇到了另一个错误,这里只是最后几行:

File "/var/www/eba/data/gs/env/opt/Python-2.7.3/lib/python2.7/random.py", line 70, in <module> 
    import _random 
ImportError: /var/www/eba/data/gs/env/lib/python2.7/lib-dynload/_random.so: cannot open shared object file: Too many open files in system 

下一次 - 另一个:

File "/var/www/eba/data/gs/env/lib/python2.7/site-packages/paste/deploy/loadwsgi.py", line 3, in <module> 
    from __future__ import with_statement 
ImportError: No module named __future__ 

问题是,如果我关闭第二个站点并在第三个站点再次运行 pserve - 它运行正常。

我不知道它必须是什么限制。可能是因为我在 ~/gs/env 文件夹中使用一个 environment 运行所有应用程序。端口 50155016 是免费的。

请您参考如下方法:

这听起来像是在非常大的文件夹中使用 --reload 的问题。尝试删除该参数,因为无论如何您都不应该在生产环境中使用它。


标签:nginx
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

关注我们

一个IT知识分享的公众号