V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
tmackan
V2EX  ›  Python

curl 无法连接到主机

  •  
  •   tmackan ·
    tmacjx · 2016-08-28 12:03:00 +08:00 · 6914 次点击
    这是一个创建于 2769 天前的主题,其中的信息可能已经有所发展或是发生改变。

    最近部署一个 django 项目, virtualenv + supervisor + unicorn

    在服务器上 cmd 执行 curl '127.0.0.1:8077'可以访问

    在服务器上 cmd 执行 curl '服务器 IP:8077'显示 curl: (7) Failed connect to 120.27.202.78:8077; 拒绝连接

    执行 netstat -apn|grep 80 显示

    tcp        0      0 127.0.0.1:8077          0.0.0.0:*               LISTEN      10599/python3.4
    tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      10642/nginx: master
    
    

    然后 nginx 的配置

    server {
         listen 80;
    
         server_name 服务器 IP;
         access_log /var/log/LandsBlog/access.log;
         error_log /var/log/LandsBlog/error.log;
    
         location / {
             proxy_pass http://127.0.0.1:8077;
             proxy_set_header Host $host;
             proxy_set_header X-Real-IP $remote_addr;
             proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
         }
         location = /50x.html {
                  root html;
         }
         location /static/ {
                  alias /alidata/websites/LandsBLog/LandsBlog/Blog/static/;
                  index index.html index.htm;
         }
         location /media/ {
                  alias /alidata/websites/LandsBLog/LandsBlog/Blog/upload/;
         }
    
    }
    
    

    求解,谢谢!

    14 条回复    2016-09-01 11:17:47 +08:00
    youKnowDai
        1
    youKnowDai  
       2016-08-28 12:27:42 +08:00
    不是把本地服务反代到 80 端口了吗
    tmackan
        2
    tmackan  
    OP
       2016-08-28 12:52:45 +08:00
    ok 解决了,我的 unicorn 绑定的地址,应该填写为外网
    laoyur
        3
    laoyur  
       2016-08-28 17:32:23 +08:00
    有点没明白,看你 op 的内容,猜想你是想用 nginx 把 8077 反代到外网 80 ?
    可看你#2 的回复,解决方法又是把 gunicorn 绑定到外网,那到底用没用 nginx 啊
    tmackan
        4
    tmackan  
    OP
       2016-08-28 18:15:54 +08:00
    @laoyur 你好,我本意是要 nginx 的。那该如何做啊?
    coolloves
        5
    coolloves  
       2016-08-28 18:48:34 +08:00 via Android
    不改动的情况下,直接 curl IP 就行,不加端口
    tmackan
        6
    tmackan  
    OP
       2016-08-28 19:27:54 +08:00
    @coolloves 咳,现在 supervisor 一直起不来了= =
    laoyur
        7
    laoyur  
       2016-08-28 20:50:00 +08:00
    > 在服务器上 cmd 执行 curl '服务器 IP:8077'显示 curl: (7) Failed connect to 120.27.202.78:8077; 拒绝连接
    那是因为你 gunicorn 绑定的是 127.0.0.1:8077 ,并没有绑 0.0.0.0:8077 ,所以当然通过外网 ip:8077 不通了

    gunicorn 绑 127.0.0.1:8077 不变
    nginx 配置类似:
    listen 80;

    location / {
    try_files $uri @proxy_to_django_app;
    }

    location @proxy_to_django_app {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    # enable this if and only if you use HTTPS
    proxy_set_header X-Forwarded-Proto https;
    proxy_set_header Host $http_host;
    # we don't want nginx trying to do something clever with
    # redirects, we set the Host: header above already.
    proxy_redirect off;
    proxy_pass http://127.0.0.1:8077;
    }
    laoyur
        8
    laoyur  
       2016-08-28 20:52:23 +08:00
    @tmackan 正好刚写了个 Ubuntu 下的 Supervisor 一键安装配置脚本,有兴趣可以试用下:
    https://laoyur.com/archives/853/
    tmackan
        9
    tmackan  
    OP
       2016-08-28 21:41:27 +08:00
    @laoyur 感谢,因为我 settings 配置下面 ALLOWED_HOSTS 设置错了,所以 supervisor 起不来,现在好了= =
    julyclyde
        10
    julyclyde  
       2016-08-29 12:30:43 +08:00
    @tmackan allowed_hosts 是 django 的选项吧。你遇到的问题是 gunicorn 的选项设置错误
    tmackan
        11
    tmackan  
    OP
       2016-08-29 12:57:58 +08:00
    @julyclyde 不好意思,你从哪里看出是 gunicorn 选项设置错误
    julyclyde
        12
    julyclyde  
       2016-08-29 13:47:26 +08:00
    @tmackan 从 netstat 看出
    tmackan
        13
    tmackan  
    OP
       2016-08-29 16:45:12 +08:00
    @julyclyde 不太懂啊,我绑定的 127.0.0.1:8077 有问题吗? bind
    julyclyde
        14
    julyclyde  
       2016-09-01 11:17:47 +08:00
    @tmackan bind 127.0.0.1 显然是有问题的啊,别的机器连不过来。你的 nginx 在别的机器的话,就会稳定出现 502
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3676 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 00:13 · PVG 08:13 · LAX 17:13 · JFK 20:13
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.