Nginx가 지정된 청취 포트를 열지 않습니다.

Nginx가 지정된 청취 포트를 열지 않습니다.

nginx(포트 7999에서 수신)에서 백엔드의 bitbucket 서버(포트 7998에서 수신)로 SSH 트래픽을 프록시하려고 합니다. nginx와 bitbucket은 모두 Docker 컨테이너 내에서 실행됩니다. nginx 컨테이너에 로그인하면 telnet bitbucket.domain-name.com 7998연결됩니다. 호스트 컴퓨터에서 다음을 netstat -pnlt얻습니다.

Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp6       0      0 :::2377                 :::*                    LISTEN      24477/dockerd       
tcp6       0      0 :::7946                 :::*                    LISTEN      24477/dockerd       
tcp6       0      0 :::80                   :::*                    LISTEN      24477/dockerd       
tcp6       0      0 :::443                  :::*                    LISTEN      24477/dockerd       
tcp6       0      0 :::7999                 :::*                    LISTEN      24477/dockerd   

하지만 컴퓨터에서 이 작업을 수행하면 다음과 같은 결과가 발생합니다 .git clone ssh://[email protected]:7999/project_key/repo_name.git

Cloning into 'repo_name'...
ssh: connect to host domain-name.com port 7999: Connection refused
fatal: Could not read from remote repository.

그리고 내가 할 때 telnet domain-name.com 7999나는 telnet: Unable to connect to remote host: Connection refused.

문제는 nginx가 도커 컨테이너 내부의 포트 7999에서 수신 대기하지 않는 것 같습니다. 하지만 내가 볼 수 있는 호스트에서는 dockerd포트 7999를 수신하고 있습니다. nginx 구성이 정확하지 않을 수도 있지만 확실하지 않을 수도 있습니다. 다음은 구성 파일의 관련 비트입니다.

docker-compose.yaml(nginx)

services:
    nginx:
        ports:
            - "80:8080"
            - "443:8443"
            - "7999:7997"

nginx.conf(nginx 컨테이너 내부)

stream {
    server {
        listen 7997;
        proxy_pass bitbucket1.cybertron.ninja:7998;
    }
}

다음은 nginx 컨테이너 내에서 실행된 일부 출력입니다.

root@6d123f454eef:/# netstat -pnlt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name                                                                        
tcp        0      0 0.0.0.0:8080            0.0.0.0:*               LISTEN      10/nginx: master pr                                                                     
tcp        0      0 0.0.0.0:8443            0.0.0.0:*               LISTEN      10/nginx: master pr                                                                     
tcp        0      0 127.0.0.11:44703        0.0.0.0:*               LISTEN      -            

이 문제를 해결하는 방법에 대한 아이디어가 있습니까? 나는 당황했다.

답변1

귀하가 제공한 약간의 정보를 사용하여 귀하의 사례에 대한 테스트 환경을 설정했습니다. 귀하가 제공한 내용이 귀하가 변경한 전부라고 가정하면 작동하지 않습니다.

공식 nginx 컨테이너 내부의 기본 nginx.conf에는 블록 include내부에 지시문이 있습니다 http.

http {
    # ...
    include /etc/nginx/conf.d/*.conf;
}

.conf 파일을 conf.d 디렉토리에 넣으면 nginx는 다음 오류 메시지와 함께 실패합니다:

2021/09/23 05:58:30 [emerg] 1#1: 여기 /etc/nginx/conf.d/blah.conf:1에서는 "stream" 지시문이 허용되지 않습니다.

귀하의 nginx가 실행 중이므로 nginx가 시작된 후 해당 디렉토리에 구성을 배치하고 nginx를 다시 시작하거나 다시 로드하지 않았다고 가정할 수 있습니다.

그만큼stream지시문은 구성의 루트에 있어야 하며 수정된 nginx.conf도 제공해야 합니다.

컨테이너에서 nginx.conf를 복사합니다.

docker cp test_nginx_1:/etc/nginx/nginx.conf .

여기에 구성을 추가하거나 별도의 디렉터리가 있는 두 번째 include 지시문을 추가하고 볼륨으로 마운트합니다.

예를 들어 블록 외부에 추가합니다 http {}.

include /etc/nginx/conf.stream.d/*.conf;

이것을 docker-compose.yml에 추가하세요.

services:
nginx:
    image: nginx
    volumes:
        - ./config/nginx.conf:/etc/nginx/nginx.conf:ro
        - ./config/nginx:/etc/nginx/conf.d:ro
        - ./config/nginx_stream:/etc/nginx/conf.stream.d:ro

run docker-compose up -d하면 작동합니다.

관련 정보