[英]Nginx reverse proxy to Yesod forwarded ip address does not work
我將 nginx 設置為 Yesod 的反向代理。 /var/log/nginx/access.log
中的IP地址是客戶端真實的IP地址。
123.123.123.123 - - [09/Oct/2020:07:11:16 +0000] "GET / HTTP/1.1" 200 ...
但是Yesod在日志中顯示的IP地址是127.0.0.1,來自nginx。
127.0.0.1 - - [09/Oct/2020:07:11:16 +0000] "GET / HTTP/1.0" 200 - "https://...
這是我的 nginx 配置:
...
server {
server_name example.com;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://127.0.0.1:3000; # Reverse proxy to your Yesod app
}
listen [::]:443 ssl ipv6only=on; # managed by Certbot
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
...
我重新啟動了 nginx 和 yesod 二進制文件。 但是Yesod列出的IP還是127.0.0.1。
我犯了什么配置錯誤?
或者我是否需要編輯我的 Yesod 二進制文件的日志代碼?
謝謝好心的讀者
答案是 Yesod 有一個配置選項可以使用轉發的 IP 標頭。
我通過閱讀包含以下內容的src/Settings.hs
找到了它:
data AppSettings = AppSettings
{ appStaticDir :: String
-- ^ Directory from which to serve static files.
, appDatabaseConf :: PostgresConf
-- ^ Configuration settings for accessing the database.
, appRoot :: Maybe Text
-- ^ Base for all generated URLs. If @Nothing@, determined
-- from the request headers.
, appHost :: HostPreference
-- ^ Host/interface the server should bind to.
, appPort :: Int
-- ^ Port to listen on
, appIpFromHeader :: Bool
-- ^ Get the IP address from the header when logging. Useful when sitting
-- behind a reverse proxy.
...
因此,您需要編輯config/settings.yaml
,其中包含以下行:
...
ip-from-header: "_env:YESOD_IP_FROM_HEADER:false"
...
或者,您也可以更改運行此命令的環境變量export YESOD_IP_FROM_HEADER=true
。
那解決了它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.