我正在使用 nginx lua docker image firesh/nginx-lua:alpine-3.4。我嘗試在 nginx.config 檔案中使用環境變數。下面是中的配置/etc/nginx/nginx.conf。
user nginx;
env ES_USERNAME;
env ES_PWD;
worker_processes 1;
events {
worker_connections 10240;
}
http {
server {
listen 8080;
server_name localhost;
set_by_lua $es_username os.getenv("ES_USERNAME");
set_by_lua $es_pwd os.getenv("ES_PWD");
location /health {
proxy_pass http://$es_username:$es_pwd@elk-es-http:9200/_cluster/health;
}
...
啟動容器后,我在日志中看到此錯誤:
2021/11/18 01:07:14 [error] 6#6: *6 failed to load inlined Lua code: set_by_lua:1: unexpected symbol near '"http://"', client: 10.0.4.122, server: localhost, request: "GET /health HTTP/1.1", host: "10.0.2.170:8080"
問題是后面的 urlproxy_pass沒有從 lua 讀取變數。它將 視為${es_username}字串而不是讀取其值。使用它的正確方法是什么?
uj5u.com熱心網友回復:
這聽起來很奇怪。我寧愿期望$es_username和$es_pwd變數都會有一個空值。set_by_lua期望一個應該回傳一個值的函式,而你什么都不回傳。正確的用法是
set_by_lua $es_username 'return os.getenv("ES_USERNAME")';
set_by_lua $es_pwd 'return os.getenv("ES_PWD")';
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/360690.html
