title: Nginx RTMP dynamic stream name
body: <p>I'm trying to create a simple RTMP/HLS server to be able to stream to and to view streams from, however, I am unable to change the name of the directory to which <code>nginx-rtmp-module</code> (module) saves the data.</p>
  <p>Everything works just fine, I am able to stream to server and then view the HLS stream in browser, however, the only way to do so is to enter the <strong>Stream Key</strong> as the target folder on the server to get the m3u8 file.</p>
  <p><strong>Currently I have the following settings:</strong></p>
  <ol>
  <li>Stream key is generated on the server and then shown to the client</li>
  <li>Client enters: rtmp://live.local/live as the endpoint and then inserts the key generated in step 1</li>
  <li>Upon the request to start the stream, module calls <code>on_publish</code> to <a href="http://app.local/stream/start" rel="nofollow noreferrer">http://app.local/stream/start</a> where at first, it checks if the key exists in the database, and if it is, it then creates new database entry for stream (with UUID as the primary key), receives the key obtained through entry creation and returns it to the script</li>
  <li>Script takes the UUID obtained in step 3, then it uses SHA-512 algorithm to generate unique string (which was planned to be used as the directory name)</li>
  <li>After hashing is performed, script returns 301 with location header</li>
  <li>The stream itself is now renamed, however the folder in which files are stored is still the Stream Key instead of hashed value obtained in step 4 and returned in step 5.</li>
  </ol>
  <p>Does anybody know on how to:</p>
  <ol>
  <li>Rename stream (I guess I've done that already)</li>
  <li>Rename directory for the stream files</li>
  <li><strong>OR</strong> is there a way to dynamically rewrite all requests, for example, client requests <a href="http://live.local/stream/here_is_the_hashed_uuid/index.m3u8" rel="nofollow noreferrer">http://live.local/stream/here_is_the_hashed_uuid/index.m3u8</a>, php takes the hash value, gets the list of active streams, hashes every streams' uuid and if matched, returns files from the other directory</li>
  </ol>
  <p>PS: Just a small example of what I have now and what I want to achieve</p>
  <p><strong>Stream key:</strong> test_stream_key_user_1<br />
  <strong>Hashed key:</strong> 73E179A6DB3796A3120319BFE80763427A2122253A5C1D347461268304B28CEB98CE3813DDF5FA8B7173937ED9386169FD3BF8E8C3765BC53BB151C7F5B1431E</p>
  <p><strong>Current directory naming:</strong> /tmp/hls/test_stream_key_user_1/index.m3u8<br />
  <strong>Desired directory naming:</strong> /tmp/hls/73E179A6DB3796A3120319BFE80763427A2122253A5C1D347461268304B28CEB98CE3813DDF5FA8B7173937ED9386169FD3BF8E8C3765BC53BB151C7F5B1431E/index.m3u8</p>
  <p>PPS: I thought that my nginx config files might help.</p>
  <p><strong>RTMP configuration</strong></p>
  <pre><code>server {
  listen 1935;
  ping 30s;
  notify_method get;
  notify_update_timeout 10s;
  
  application live {
      live on;
      hls on;
      hls_nested on;
      hls_path /tmp/hls;
      hls_fragment 3;
      hls_playlist_length 60;
      on_publish http://app.local/api/stream/start;
      on_done http://app.local/api/stream/stop;
      on_update http://app.local/api/stream/update;
      }
  }
  </code></pre>
  <p><strong>Server Configuration (APP)</strong></p>
  <pre><code>server {
      listen 80;
      listen [::]:80;
  
      root #replaced#;
  
      index index.php index.html index.htm index.nginx-debian.html;
  
      server_name app.local www.app.local;
  
      location / {
              proxy_pass http://localhost:3000;
              proxy_http_version 1.1;
              proxy_set_header Upgrade $http_upgrade;
              proxy_set_header Connection 'upgrade';
              proxy_set_header Host $host;
              proxy_cache_bypass $http_upgrade;
      }
  
      # PHP-FPM Configuration Nginx
      location ~ \.php$ {
              try_files $uri =404;
              fastcgi_split_path_info ^(.+\.php)(/.+)$;
              fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
              fastcgi_index index.php;
              fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
              include fastcgi_params;
      }
  
      location /api {
              try_files $uri $uri/ /index.php?$query_string;
      }
  
      location /sitemap.xml {
              try_files $uri $uri/ /index.php?$query_string;
      }
  
      location /login {
              try_files $uri $uri/ /index.php?$query_string;
      }
  
      location /broadcasting {
              try_files $uri $uri/ /index.php?$query_string;
      }
  
  
      location ~ /\.ht {
              deny all;
      }
  }
  </code></pre>
  <p><strong>Server Configuration (LIVE)</strong></p>
  <pre><code>server {
      listen 80;
      listen [::]:80;
  
  root #replaced#;
  
      index index.html index.htm index.nginx-debian.html;
  
      server_name live.local www.live.local;
  
  location /stat {
      rtmp_stat all;
      rtmp_stat_stylesheet original_stat.xsl;
  }
  
  location /control {
      rtmp_control all;
  }
  
  location ~ \.php$ {
              try_files $uri =404;
              fastcgi_split_path_info ^(.+\.php)(/.+)$;
              fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
              fastcgi_index index.php;
              fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
              include fastcgi_params;
      }
  
      location /stream {
              try_files $uri $uri/ /index.php?$query_string;
      }
  
  location /hls {
      add_header 'Access-Control-Allow-Origin' '*' always;
              add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
              add_header 'Access-Control-Allow-Headers' 'Range';
  
      if ($request_method = 'OPTIONS') {
          add_header 'Access-Control-Allow-Headers' 'Range';
          add_header 'Access-Control-Max-Age' 1728000;
          add_header 'Content-Type' 'text/plain charset=UTF-8';
          add_header 'Content-Length' 0;
          return 204;
              }
  
      types {
          application/dash+xml mpd;
          application/vnd.apple.mpegurl m3u8;
          video/mp2t ts;
      }
      root /tmp;
      add_header Cache-Control no-cache;
      }
  
  location /dash {
      root /tmp;
      add_header Cache-Control no-cache;
      add_header 'Access-Control-Allow-Origin' '*' always;
      }
  }
  </code></pre>