docker nginx连接到上游时被拒绝 [英] docker nginx connection refused while connecting to upstream

查看:296
本文介绍了docker nginx连接到上游时被拒绝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在服务器上使用nginx时,我使用闪亮的服务器在端口3838上构建了一个Web应用程序。但是,当我在服务器上停止nginx并尝试使用docker nginx时,发现该站点出现 502-Bad Gate Way错误,nginx日志显示:

I use shiny server to build a web-app on port 3838, when i use nginx in my server it works well. But when I stop nginx on my server and try to use docker nginx, I find the site comes to a '502-Bad Gate Way' error and nginx log shows:

2016/04/28 18:51:15 [error] 8#8: *1 connect() failed (111: Connection refused) while connecting to upstream, ...

我通过以下命令安装docker-nginx:

I install docker-nginx by this command:

sudo docker pull nginx

我的docker命令行类似于(清除我添加一些缩进):

My docker command line is something like (for clear i add some indent):

sudo docker run --name docker-nginx -p 80:80 
    -v ~/docker-nginx/default.conf:/etc/nginx/conf.d/default.conf
    -v  /usr/share/nginx/html:/usr/share/nginx/html nginx

我在主目录中创建一个名为docker-nginx的文件夹,将我的Nginx conf文件移动到该文件夹​​中,然后删除我在etc / nginx目录中的原始conf,以防万一。

I create a folder name 'docker-nginx' in my home dir, move my nginx conf file in this folder, and then remove my original conf in etc/nginx dir just in case.

我的nginx conf文件如下:

My nginx conf file looks like this:

server {
    listen 80 default_server;
    # listen [::]:80 default_server ipv6only=on;

    root /usr/share/nginx/html;
    index index.html index.htm;

    # Make site accessible from http://localhost/
    server_name localhost;

    location / {
            proxy_pass http://127.0.0.1:3838/;
            proxy_redirect http://127.0.0.1:3838/ $scheme://$host/;
            auth_basic "Username and Password are required";
            auth_basic_user_file /etc/nginx/.htpasswd;
            # enhance the performance
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
            proxy_set_header Host $host;
    }
}


推荐答案

您必须直接定义上游。当前,您的nginx无法代理到您的Web应用程序。

You have to define upstream directly. Currently your nginx can not proxy to your web application.

http://nginx.org/en/docs/http/ngx_http_upstream_module.html

upstream backend {
    server backend1.example.com       weight=5;
    server backend2.example.com:8080;
    server unix:/tmp/backend3;

    server backup1.example.com:8080   backup;
    server backup2.example.com:8080   backup;
}

server {
    location / {
        proxy_pass http://backend;
    }
}

这篇关于docker nginx连接到上游时被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆