如何從 http 請求中阻止端口服務器 (How to block port server from http request)


問題描述

如何從 http 請求中阻止端口服務器 (How to block port server from http request)

我有 Nginx 有一個反向代理來將客戶端請求連接到 docker 運行容器。問題是您可以通過鍵入 serverhost.fr:port_number 來訪問容器。我希望客戶端只能通過特定主機名訪問正在運行的容器。這是我的 docker‑compose.yml 文件:

version: '3'
services:
    api:
        image: username/backendimgage:latest
        ports:
            ‑ "8081:8000"
        restart: always
    front:
        depends_on:
            ‑ api
        image: username/frontendimage:latest
        ports:
            ‑ "8080:36073"
        restart: always

我試圖用 ufw 阻止,它破壞了一切。我嘗試使用 if 語句在 Nginx 上發送 401 代碼,例如

if ( $host = serverhost.fr:port_number ){ return 401; } 

OR

6
if ( $host ~* portnumber ){ return 401; }

但它不起作用。我沒有大的操作背景,所以我有點迷路了。


參考解法

方法 1:

If you're running the nginx proxy outside of Docker, you must connect to the published ports: from your containers.

By default when you publish ports: they're published on all interfaces of the host system. However, you can specify an alternate bind address. If you set a backend service to only publish on the 127.0.0.1 localhost interface, it won't be reachable from off host.

version: '3'
services:
    api:
        image: username/backendimgage:latest
        ports:
            ‑ "127.0.0.1:8081:8000"
            #  ^^^^^^^^^

(If you're trying to connect from one container to another by using its host port – something like http://host.docker.internal:8081 on MacOS/Windows platforms – this will interfere with that, but you should be able to use Docker‑native inter‑container communication there.)

(by Pierre MonierDavid Maze)

參考文件

  1. How to block port server from http request (CC BY‑SA 2.5/3.0/4.0)

#webserver #nginx-reverse-proxy #linux #docker #server






相關問題

高性能 IIS 的最佳實踐/工具? (Best practices / tools for high performance IIS?)

找不到 httpd.conf (Unable to find httpd.conf)

auto_prepend_file 多個文件 (auto_prepend_file multiple files)

如何正確配置 Intellij IDEA 中的 JBoss?我得到錯誤:未指定服務器實例 (How to configure correctly the JBoss in the Intellij IDEA? I get Error:server instance not specified)

在後台android更新數據到服務器 (update data to server in the background android)

根據網絡連接將用戶引導到不同的頁面 (Direct user to different page based on network connection)

與 apache 網絡服務器和 Node.js REST 服務器共享 HTTP 端口 80 (Share HTTP port 80 with apache webserver and Node.js REST server)

有沒有像 ASP.NET 的 webrick 之類的東西? (Is there anything like webrick for ASP.NET?)

Apache、lighttpd、nginx、切諾基,什麼是最好的組合? (Apache, lighttpd, nginx, cherokee, what's the best combination?)

在我的域服務器上使用 Ruby on Rails 應用程序 (Consuming a Ruby on Rails app on my domain server)

在同一台 Ubuntu 服務器上安裝和配置 PHP5 和 7 (Install and configure PHP5 and 7 on same Ubuntu server)

如何從 http 請求中阻止端口服務器 (How to block port server from http request)







留言討論