Nginx作为Nexus的反向代理服务器-无法在Docker环境中连接 [英] Nginx as reverse proxy server for Nexus - can't connect in docker environment

查看:764
本文介绍了Nginx作为Nexus的反向代理服务器-无法在Docker环境中连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在docker容器上构建了环境(在boot2docker中).我有以下docker-compose.yml文件来快速设置nginx和nexus服务器:

I have environment builded upon docker containers (in boot2docker). I have following docker-compose.yml file to quickly setup nginx and nexus servers :

version: '3.2'

services:
  nexus:
    image: stefanprodan/nexus
    container_name: nexus
    ports:
      - 8081:8081
      - 5000:5000

  nginx:
    image: nginx:latest
    container_name: nginx
    ports:
      - 5043:443
    volumes:
      - /opt/dm/nginx2/nginx.conf:/etc/nginx/nginx.conf:ro

Nginx具有以下配置(nginx.conf)

Nginx has following configuration (nginx.conf)

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}

http {

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    proxy_send_timeout 120;
    proxy_read_timeout 300;
    proxy_buffering    off;
    keepalive_timeout  5 5;
    tcp_nodelay        on;

    server {
        listen         80;
        server_name    demo.com;

    return         301 https://$server_name$request_uri;
    }

    server {
        listen   443 ssl;
        server_name  demo.com;

        # allow large uploads of files - refer to nginx documentation
        client_max_body_size 1024m;

        # optimize downloading files larger than 1G - refer to nginx doc before adjusting
        #proxy_max_temp_file_size 2048m

        #ssl on;
        #ssl_certificate      /etc/nginx/ssl.crt;
        #ssl_certificate_key  /etc/nginx/ssl.key;

        location / {
            proxy_pass http://nexus:8081/;
            proxy_set_header Host $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 "https";
        }
    }
}

Nexus似乎运作良好.我在Docker主机上成功调用curl http://localhost:8081.这将返回我的nexus登录站点的html.现在,我想尝试Nginx服务器.它被配置为侦听443端口,但是SSL已被禁用(我想在进入SSL配置之前对其进行测试).如您所见,我的ngix容器将端口443映射到端口5043.因此,我尝试使用以下curl命令:curl -v http://localhost:5043/.现在,我希望我的http请求将发送到nginx并代理到proxy_pass http://nexus:8081/; nexus. Nexus主机名在docker容器网络中可见,可从nginx容器访问.不幸的是,我收到了回复:

Nexus seems to work very well. I call sucessfully curl http://localhost:8081 on docker host machine. This return me html of nexus login site. Now I want to try nginx server. It is configured to listen on 443 port, but SSL is right now disabled (I wanted to test it before diving into SSL configuration). As you can notice, my ngix container maps port 443 to port 5043. Thus, I try to use following curl command : curl -v http://localhost:5043/. Now I expect that my http request is going to be send to nginx and proxied to proxy_pass http://nexus:8081/; nexus. Nexus hostname is visible within docker container network and is accesible from nginx container. Unfortunately in reponse I receive :

*   Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 5043 (#0)
> GET / HTTP/1.1
> Host: localhost:5043
> User-Agent: curl/7.49.1
> Accept: */*
>
* Empty reply from server
* Connection #0 to host localhost left intact
curl: (52) Empty reply from server

我正在检查nginx日志,错误,访问权限,但这些日志为空.有人可以帮我解决这个问题吗?它应该只是代理请求的简单示例,但是也许我误解了一些概念?

I was checking nginx logs, error, access but these logs are empty. Can somebody help me solving this problem ? It should be just a simple example of proxying requests, but maybe I misunderstand some concept ?

推荐答案

nginx conf中是否放置了upstream指令(放在http指令中)?

Do you have an upstream directive in your nginx conf (placed within the http directive)?

upstream nexus {
    server <Nexus_IP>:<Nexus_Port>;
}

只有nginx才能正确解决它. docker-compose服务名称nexus不会在运行时注入到nginx容器中.

Only then nginx can correctly resolve it. The docker-compose service name nexus is not injected to the nginx container on runtime.

您可以在docker-compose中尝试links:

You can try links in docker-compose:

https://docs.docker.com/compose/compose-file/#links

这为/etc/hosts中的链接容器提供了alias. 但是您仍然需要upstream directive. 更新:如果可以解决,您也可以直接在nginx directives中使用这些名称,例如location.

This gives you an alias for the linked container in your /etc/hosts. But you still need an upstream directive. Update: If resolvable, you can as well use the names directly in nginx directives like location.

https://serverfault.com/questions/577370 /如何在nginx-conf中使用环境变量

这篇关于Nginx作为Nexus的反向代理服务器-无法在Docker环境中连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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