web-services – docker nginx连接在连接到上游时被拒绝

我使用闪亮的服务器在端口3838上构建一个web-app,当我在我的服务器中使用nginx它运行良好.但当我在我的服务器上停止nginx并尝试使用docker nginx时,我发现该网站出现’502-Bad Gate Way’错误,nginx日志显示:

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

我通过这个命令安装docker-nginx:

sudo docker pull nginx

我的docker命令行是这样的(为了清除我添加一些缩进):

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以防万一.

我的nginx conf文件如下所示:

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应用程序.

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;
    }
}

转载注明原文:web-services – docker nginx连接在连接到上游时被拒绝 - 代码日志