다른 하위 도메인과 포트도 허용하도록 nginx 서버를 구성하려면 어떻게 해야 합니까?

다른 하위 도메인과 포트도 허용하도록 nginx 서버를 구성하려면 어떻게 해야 합니까?

Ubuntu/Nginx에서 실행되는 서버가 있습니다. 다른 내부 포트에서 실행되는 하위 도메인이 있습니다. 하나의 애플리케이션을 대중에게 공개하고 싶지만 어떤 도메인/서버 이름과도 연결하고 싶지 않습니다.

다음은 내 구성 파일입니다.

server {
    server_name app.example.com www.app.example.com;
    access_log /home/hub-app/logs/app.example.com.access.log;
    
    location / {
        proxy_set_header Host $host;
        proxy_pass http://127.0.0.1:8082;
        proxy_redirect off;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';      
        proxy_cache_bypass $http_upgrade;       
        proxy_http_version 1.1;     
        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 $scheme; 
    }

    listen 443 ssl; 
    ssl_certificate /etc/letsencrypt/live/app.example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/app.example.com/privkey.pem;
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;


}   

server {
    server_name example.com www.example.com;
    access_log /home/hub-public/logs/example.com.access.log;
    
    location / {
        proxy_set_header Host $host;
        proxy_pass http://127.0.0.1:8081;
        proxy_redirect off;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';      
        proxy_cache_bypass $http_upgrade;       
        proxy_http_version 1.1;     
        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 $scheme; 
    }

    listen 443 ssl;
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; 
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; 
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; 

}

위의 내용은 잘 작동하며 지정된 도메인(예: example.com 및 app.example.com)을 가리킵니다. 이제 MY_PUBLIC_IP:8080에서 실행할 다른 가상 서버를 추가하고 싶습니다. 포트 8080은 다른 도메인에서 액세스할 수 없어야 합니다.example.com:8080/app.example.com:8080사용할 수 없어야 합니다.

답변1

당신이 사용할 수있는 default_server.

Nginx는 해당 서버를 기본 서버로 선언합니다. 그 후 Nginx는 HTTP 호스트 헤더가 다른 서버 블록과 일치하지 않는 경우 요청을 처리하기 위해 기본 서버를 활용합니다.

Nginx의 default_server 란 무엇입니까?

예:

server {
    listen 8080 default_server;
    
    root /www/default;
        
    location / {
        index index.html;
    }
}


나는 그것을 허니팟 스캐너 봇에 사용하고 그들이 phpAdmin이나 그와 유사한 것을 찾고 있다면 그들에게 똥 이모티콘을 스트리밍합니다 :)

답변2

@Klamberext 답변이 실제로 질문에 대한 답변이라고 생각하지 않습니다. 즉, nginx 웹 서버에는기본 서버개념. 해당 주제에 대한 공식 문서 페이지는 여기에서 찾을 수 있습니다.nginx가 요청을 처리하는 방법. 그러나 일부 네트워크 인터페이스/포트 조합을 수신하는 서버 블록 중 하나는 항상 기본 블록으로 작동합니다. 해당 서버 블록을 명시적으로 지정하지 않으면 구성의 첫 번째 서버 블록이 됩니다. 즉, 이 줄이 있는 서버 블록은 server_name app.example.com www.app.example.com;443 TCP 포트에서 들어오는 모든 요청에 ​​대한 기본 서버 블록이 되어 HTTP Host헤더가 일치하지 않거나 example.com헤더 가 전혀 www.example.com없는 요청을 처리한다는 의미입니다.Host

@Klamberext가 이미 말했듯이, 일반적인 관행은 HostHTTP 헤더가 제공 중인 도메인과 일치하지 않는 모든 요청을 포착하기 위해 스텁 서버 블록을 정의하는 것입니다. 다음에서 예를 볼 수 있습니다.이것그래서 대답하십시오. 일반적으로 이러한 서버 블록에는 return 444;연결을 즉시 닫는 특수 nginx 반환 코드인 명령문이 포함되어 있습니다. 그러나 반대의 것이 필요한 것 같고 이를 달성하려면 두 개의 서버 블록이 필요합니다. 이미 말했듯이 TCP 포트 8080에서 수신 대기하는 단일 서버 블록은 헤더 설정에 관계없이 기본 서버 블록으로 작동하기 때문입니다 Host. :

server {
    listen 8080;
    server_name example.com www.example.com app.example.com www.app.example.com;
    return 444;
}
server {
    listen 8080 default_server;
    ... your config here
}

대안으로 Host서버 블록 내부의 헤더 값을 확인할 수 있습니다. 예를 들어 도메인 example.com및 해당 하위 도메인을 차단할 수 있습니다.

server {
    listen 8080;
    if ($http_host ~ \.?example\.com$) { return 444; }
    ... your config here
}

관련 정보