nginx lua脚本 操作文件目录或者文件的默认路径的坑

最近线上出现了BUG,就是在服务器迁移之后,发现 nginx 的lua 脚本不能采集nginx的log数据

经过排查发现是因为 lua 不能找到对应文件,所以不能将nginx 数据写入 文件中。即 lua 不能创建创建文件目录,因此找不到文件。

明明 服务器未迁移之前好好的,怎么迁移重启之后就 报错了? 这块是 离职同事写的,那时候比较忙交接的时候也没有怎么认真看。 坑啊!

重现步骤

  1. 启动nginx /usr/servers/nginx/sbin/nginx nginx 配置
user  nginx;
worker_processes  1;

#error_log  logs/error.log;
#error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; lua_package_path "/usr/local/servers/lualib/?.lua;;"; lua_package_cpath "/usr/local/servers/lualib/?.so;;"; #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; lua_shared_dict task 10m; lua_code_cache on; init_worker_by_lua_file conf/lua/task.lua; server { listen 8001; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; index index.html index.htm; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} } server { listen 8181; server_name _; lua_code_cache on; location /lua { default_type 'text/html'; content_by_lua_file conf/lua/test.lua; } location /task { default_type 'text/html'; content_by_lua_file conf/lua/task.lua; } location /flow/analysis { proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto http; default_type 'text/html'; content_by_lua_file conf/lua/analysis.lua; } } # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} # HTTPS server # #server { # listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} } 

test.lua 内容

local osTime = os.time()
local osTimeNum = (tonumber(osTime)+86400) local utcPath = tostring(os.date("%Y-%m",osTimeNum)) ngx.say("hello world4mkdir-----------:"..utcPath); os.execute("mkdir test") 
  1. 测试 localhost:8181/lua 我需要的要求是在 nginx 的根目录下: /usr/servers/nginx 创建目录 test 结果 发现并没有,而是在 /usr/servers/nginx/sbin 下面创建了 目录 test 并不是我想要的。

解决办法

创建文件夹使用绝对路径

os.execute("mkdir /usr/servers/nginx/test") 通过测试没有问题了

为什么会这样? lua 的默认目录又是怎么回事?

为什么会这样? 没有迁移之前 怎么没有问题? 我怀疑是 离职同事的启动命名和我的不一样导致的。 可是又找不出来 在通过查询资源之后,测试之后 终于发现了是哪里的问题了  **即通过 -p 命名指定nginx根目录,那么 lua 就默认以这个为根目录 /usr/servers/nginx/sbin/nginx -p pwd/ -c conf/nginx.conf **

否则如果通过 常规的手段启动nginx : /usr/servers/nginx/sbin/nginx 那么默认的lua的根目录就是 /usr/servers/nginx/sbin 为了避免 因为nginx 启动方式的不同而导致 lua 脚本 操作文件,文件夹出错误, 以后都是用 绝对路径即可

参考资料

猜你喜欢

转载自www.cnblogs.com/wu2198bk/p/12942004.html