
我正在嘗試從本地網路上運行 Ubuntu 18.04 的伺服器提供 Intranet 應用程式
該應用程式已編寫Flask
,我已使用gunicorn
以下命令部署了它
me@appserver:~$ authbind gunicorn -w 4 -b 127.0.0.1:80 app:app
透過顯示連接埠轉送透過 SSH 連接到伺服器,我可以使用 Firefox 打開應用程式並與其互動。但是,當我嘗試從子網路上的另一台電腦連線時,連線被拒絕。
我可以 telnet 到連接埠 22 好吧,即
me@clientmachine:~$ telnet 123.45.67.89 22
Trying 123.45.67.89...
Connected to 123.45.67.89.
但我在連接埠 80 上收到連線被拒絕的訊息
me@clientmachine:~$ telnet 123.45.67.89 80
Trying 123.45.67.89...
telnet: Unable to connect to remote host: Connection refused
ufw
我已經使用with 指令設定了防火牆
me@appserver:~$ sudo ufw allow 80
這給了我
me@appserver:~$ sudo ufw status
Status: active
To Action From
-- ------ ----
80 ALLOW Anywhere
80/tcp ALLOW Anywhere
22/tcp ALLOW Anywhere
80/udp ALLOW Anywhere
80 (v6) ALLOW Anywhere (v6)
80/tcp (v6) ALLOW Anywhere (v6)
22/tcp (v6) ALLOW Anywhere (v6)
80/udp (v6) ALLOW Anywhere (v6)
我也證實了這一點
me@appserver:~$ netstat -an | grep :80
tcp 0 0 127.0.0.1:80 0.0.0.0:* LISTEN
me@appserver:~$ lsof -i:80
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
gunicorn 1818 me 5u IPv4 31215 0t0 TCP localhost:http (LISTEN)
gunicorn 1822 me 5u IPv4 31215 0t0 TCP localhost:http (LISTEN)
gunicorn 1823 me 5u IPv4 31215 0t0 TCP localhost:http (LISTEN)
gunicorn 1824 me 5u IPv4 31215 0t0 TCP localhost:http (LISTEN)
gunicorn 1825 me 5u IPv4 31215 0t0 TCP localhost:http (LISTEN)
和
me@appserver:~$ sudo iptables -L -n | grep :80
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:80
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:80
ACCEPT udp -- 0.0.0.0/0 0.0.0.0/0 udp dpt:80
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:80
ACCEPT udp -- 0.0.0.0/0 0.0.0.0/0 udp dpt:80
然而,nmap
從客戶端運行顯示80但說已關閉
me@clientmachine:~$ nmap 123.45.67.89
Starting Nmap 7.01 ( https://nmap.org ) at 2019-07-24 16:41 AEST
Nmap scan report for appserver.myuni.edu (123.45.67.89)
Host is up (0.00041s latency).
Not shown: 997 filtered ports
PORT STATE SERVICE
22/tcp open ssh
80/tcp closed http
知道為什麼會這樣以及如何解決它嗎?這種事情可以透過網路管理員管理的子網路上的設定來管理嗎?
答案1
該訊息Connection refused
表示該連接埠上沒有任何內容正在偵聽,該連接埠已關閉。如果防火牆阻止訪問,您將收到一條有關逾時的訊息。
問題在於您啟動應用程式時僅偵聽環回介面127.0.0.1
。因此,它僅在您的輸出確認的該介面上可用netstat
。如果您想讓它在外部介面上可用,您必須在啟動時提供該 IP,或0.0.0.0
允許所有介面。
me@appserver:~$ authbind gunicorn -w 4 -b 123.45.67.89:80 app:app