大网数据广告活动
服务器运维问答

Unable to read a file and returns its result nginx

· 52CE 我爱测

Using lua-nginx-module . And I'm unable to accomplish a thing wherein I'm want to modify the mtime of a file( touch.txt ). I tried using .. os.execute("touch /app/directory/touch.txt") and this io.open('/app/directory/touch.txt',…

问题原文

原帖链接 · Viren · CC BY-SA 3.0

Using lua-nginx-module. And I'm unable to accomplish a thing wherein I'm want to modify the mtime of a file(touch.txt).

I tried using ..

os.execute("touch /app/directory/touch.txt")

and this

io.open('/app/directory/touch.txt','w').close()

But none of the above is working ..

Here how my nginx.conf looks like this ..

location / {
               auth_basic "Prohibited area";
               auth_basic_user_file /etc/apache2/.htpasswd;
               default_type 'text/plain';
               content_by_lua_block {
                  os.execute('/usr/bin/touch /app/directory/touch.txt')
                  local time = os.date("%m/%d/%Y %I:%M:%S %p")
                  ngx.say('Hello,world! '.. time)
               }
                proxy_redirect  off;
}

I see the time returned i.e (Hello,world! '.. time) in browser correctly but the mtime of touch.txt remain still the same.

Any things over here .. that I need to take care of.

已采纳解答

原帖链接 · Gerard H. Pille · CC BY-SA 3.0

    location /lua {
      content_by_lua_block {
        local res = os.execute('/usr/bin/touch /tmp/touch.txt')
        local time = os.date("%m/%d/%Y %I:%M:%S %p")
        if res == 0 then
          ngx.header["Content-type"] = "text/plain"
          ngx.say('Hello,world! '.. time)
        else
          ngx.status = ngx.HTTP_NOT_FOUND
          ngx.header["Content-type"] = "text/plain"
          ngx.say('The world is doomed '.. time)
          ngx.say('because of  ' .. res)
        end
    }

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2018-03-25)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。