nginx lua scripts manipulate pits in file directories or default paths to files

BUG s have recently appeared online, and it is after server migration that a lua script that finds nginx cannot collect log data for nginx

After sorting, it was found that because lua could not find the corresponding file, the nginx data could not be written to the file.That is, lua cannot create the create file directory and therefore cannot find the file.

It is clear that the server is good before migration, how can migration be restarted and then error?This piece was written by a departing colleague and was not taken seriously when he was busy handing over at that time.Pit!

repro steps

  1. Start nginx/usr/servers/nginx/sbin/nginx nginx configuration
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.luacontent

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. testLocalhost:8181/lua What I need is a directory at the root of nginx: /usr/servers/nginx to create the directory test Instead of finding it, the directory test was created under /usr/servers/nginx/sbin Not what I wanted.

Solution

Create folders using absolute paths

os.execute("mkdir /usr/servers/nginx/test") No problem passing the test

Why is that?What about lua's default directory?

Why is that?How can I be OK without migrating? I suspect the departing colleague's starting name is different from mine. But I can't find it After passing the query for resources, the test finally found out where the problem is. ** that is, specify the nginx root directory with the -p name, then lua defaults to this as the root directory /usr/servers/nginx/sbin/nginx -p pwd/ -c conf/nginx.conf **

Otherwise, if nginx:/usr/servers/nginx/sbin/nginx is started by normal means, then the default lua root directory is/usr/servers/nginx/sbin To avoid the lua script operation files caused by different ways of starting nginx, folder errors, and absolute paths can be used in the future

Reference material

nginx lua best practices

Tags: Operation & Maintenance Nginx PHP Apache SSL

Posted on Fri, 22 May 2020 23:54:39 -0400 by davidx714