问题描述
使用应用程序.org / package / warp-2.1.2.1 / docs / Network-Wai-Handler-Warp.html#v%3arunrel =noreferrer> run
,它监听所有IP地址。
When running a warp application using run
, it listens on all IP adresses.
出于安全原因,我只想监听 localhost
,处理使用反向代理进行远程访问。
For security reasons, I want to listen on localhost
only, handling remote access using a reverse proxy.
如何调用 run
来仅监听特定的主机/ IP?
How do I need to call run
to only listen on a specific host/IP?
注意:这个问题故意显示没有研究工作,因为它被回答为Q& A-Style。
Note: This question intentionally shows no research effort as it was answered Q&A-Style.
推荐答案
目前接受的答案因管道和/或变形的变化而中断。 Warp不再导出 Host
构造函数。但是你不需要它,因为 HostPreference
类型支持OverloadedStrings扩展,所以你可以直接使用字符串。
The currently accepted answer got broken by changes to conduit and/or warp. Warp no longer exports a Host
constructor. But you don't need it, because the HostPreference
type supports the OverloadedStrings extension, so you can just use a string directly.
此示例还通过切换到 setPort
和 setHost
来消除弃用警告。
This example also eliminates the deprecation warnings by switching to setPort
and setHost
.
{-# LANGUAGE OverloadedStrings #-}
import Network.Wai (responseLBS)
import Network.Wai.Handler.Warp
import Network.HTTP.Types (status200)
import Network.HTTP.Types.Header (hContentType)
main = do
let port = 3000
putStrLn $ "Listening on port " ++ show port
let settings = setPort port $ setHost "127.0.0.1" defaultSettings
runSettings settings app
app req = return $
responseLBS status200 [(hContentType, "text/plain")] "Hello world!"
这篇关于使用warp监听特定主机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!