Openresty torch module loading issue - nginx

I'm trying to use openresty with torch for a Rest api for a neural network.
First query works, any query after that fails.
Nginx Config
workers processes 1;
error_log logs/error.log;
events {
workers connections 1024
}
http {
server {
listen 5050;
location /{
default type text/html;
content_by_lua_file /home/yiftach/testFile.lua;
}
}
}
testFile.lua
require "nn"
local tensorA=torch.zeros(1,1)
ngx.say(tensorA:size()[1])
The error:
Lua entry thread aborted: runtime error: /home/yiftach/testFile.lua: attempt to index global 'torch' (a nil value)
Would appreciate any help

You didn't require the torch library.
Add local torch = require "torch" at the top.

Related

websockets in openresty proxy

I created proxy with MFA using OpenResty, it mainly works ok.
But I have problem with websockets: Firefox says that it "cannot connect with server wss://...". Looking in browser's network panel I can see switching protocols request that seems be ok. My nginx.conf looks as bellow:
worker_processes auto;
env TARGET_APPLICATION_HOST;
env TARGET_APPLICATION_PORT;
env TARGET_USE_SSL;
events {
worker_connections 1024;
}
http {
server {
listen 80;
server_name localhost;
location / {
resolver local=on ipv6=off valid=100s;
content_by_lua_block {
local http = require "resty.http"
local httpc = http.new()
httpc:set_timeout(500)
local ok, err = httpc:connect(
os.getenv("TARGET_APPLICATION_HOST"),
os.getenv("TARGET_APPLICATION_PORT"))
if not ok then
ngx.log(ngx.ERR, err)
return
end
if os.getenv("TARGET_USE_SSL") == "TRUE" then
-- Trigger the SSL handshake
session, err = httpc:ssl_handshake(False, server, False)
end
httpc:set_timeout(2000)
httpc:proxy_response(httpc:proxy_request())
httpc:set_keepalive()
}
}
}
}
It is simpler version of production proxy, but returns the same error with websockets. I tried to use proxy with pure nginx and it works ok with websockets, but I need capabilites of OpenResty (proxing different hosts basing of cookie value).
Is there any simple mistake in the above file or OpenResty does not have websocket abilities?
lua-resty-http is a HTTP(S) client libraty, it does not (and probably will not) support the WebSocket protocol.
There is another library for the WebSocket protocol: lua-resty-websocket. It implements both client and server, so it should be possible to write the proxy using this library.
I need capabilites of OpenResty (proxing different hosts basing of cookie value)
ngx.balancer does exactly what you need, check the example and this answer.

Dynamic proxy_pass for stream

I'm trying to have openresty to reverse proxy TCP dynamicaly using lua.
For the start, I have :
stream {
server {
listen 9291;
set_by_lua_block $proxy '
ngx.var.proxy = "10.128.128.3:8291"
';
proxy_pass $proxy;
}
}
But openresty -t says:
nginx: [emerg] "set_by_lua_block" directive is not allowed here in /usr/local/openresty/nginx/conf/nginx.conf:129
I found many docs on dynamic proxy_pass, but all for 'http'.
Take a look at balancer_by_lua_block directive.
You will need to use ngx.balancer API within balancer_by_lua_block.
Read all docs carefully. There are a lot of smart details.
But all you need is here, just RTFM.

Nginx config invalid parameter even though it is in documentation

I am tryin to run nginx latest version with the following configuration, but I get nginx: [emerg] invalid parameter "route=bloomberg" in /etc/nginx/nginx.conf:13
docker run --rm -ti -v root_to_local_nginx_directory:/etc/nginx:ro -p 3080:80 --name=mynginx --entrypoint nginx nginx
# nginx.conf file inside root_to_local_nginx_directory
http {
map $cookie_route $route_from_cookie {
~.(?P<version>w+)$ $route;
}
split_clients "${remote_addr}" $random_route {
50% server bloomberg.com route=bloomberg;
* server yahoo.com route=yahoo;
}
upstream backend {
zone backend 64k;
server bloomberg.com route=bloomberg;
server yahoo.com route=yahoo;
sticky route $route_from_cookie $randomroute;
}
server {
# ...
listen 80;
location / {
proxy_set_header Host $host;
proxy_pass http://backend;
}
}
}
Why is this? According to the documentation this should be correct http://nginx.org/en/docs/http/ngx_http_upstream_module.html#upstream.
The route=string parameter of the server directive within the upstream context is considered to be an enterprise-grade feature, and is thus only available through the commercial subscription, in NGINX Plus, not in OSS NGINX. (If you look closer into the documentation, you'll notice it's grouped together with the other parameters under a separate "available as part of our commercial subscription" subsection.)
Additionally, you're also trying to use some similar "server" parameters within the split_clients context as if they were actual directives interpreted by nginx, even though everything is supposed to be string literals in that context; it's unclear whether or not that part is responsible for any errors, but even if not, it's a bad style to introduce such confusion into your configuration.
References:
http://nginx.org/en/docs/http/ngx_http_upstream_module.html#server
http://nginx.org/en/docs/http/ngx_http_split_clients_module.html#split_clients
https://www.nginx.com/products/nginx/
The reason why you are seeing the error is because the split_clients module does not support the route parameter. Alternatively, you can do something along the lines:
upstream bloomberg {
server bloomberg.com route=bloomberg;
}
upstream yahoo {
server yahoo.com route=yahoo;
}
split_clients "${remote_addr}" $random_route {
50% bloomberg;
* yahoo;
}

rtmp Nginx use control endpoint for record?

I have nginx compiled with the rtmp module. I start nginx and run the following to start streaming my webcam which works:
ffmpeg -i /dev/video0 -f flv rtmp://localhost/live/test
I then try to use the control module to start recording:
curl "http://localhost:8080/control/record/start?app=application&name=test&rec=rec1"
But the recording event doesn't seem to trigger.
Here is a simplified version of my nginx.conf file:
rtmp {
server {
# ... more code here
recorder rec1 {
record all manual;
record_suffix all.flv;
record_path /tmp/rec;
record_unique on;
}
}
}
http {
server {
listen 8080;
server_name localhost;
location /control {
rtmp_control all;
}
}
# ... more code here
}
Note: checked both port 8080 and 1935 are open with nmap.
Note/Update:
I noticed that if I change app= to live I get an actual error message:
<html>
<head><title>500 Internal Server Error</title></head>
<body bgcolor="white">
<center><h1>500 Internal Server Error</h1></center>
<hr><center>nginx/1.11.1</center>
</body>
</html>
As opposed to the command with the actual app name which returns nothing. This tells me it is working to some extent but I still don't end up with a recoding.
I'm also trying to switch name=test to name=live neither causes an error response.
Complete nginx.conf file.
Update #2:
I'm watching /var/log/nginx/error.log while I use the curl command above. Everytime I use it the following is logged:
client 127.0.0.1 closed keepalive connection
I eneded up fixing this. The problem was two fold.
application wasn't my application name, it was live.
the rec1 block wasn't inside the live application block, it was an unrelated block of code for hls.

How to call a Lua method on startup in NGINX?

I'm using Lua with NGINX. My code has a lua method called "rabbitmq.createExchange". I want to call this method, when nginx server starts.
I tried following code, but i failed with error "content_by_lua_block directive is not allowed here". Any solutions?
http {
lua_package_path '/app/nginx/src/?.lua;;';
server {
listen 4000;
include 'src/proj1.conf';
}
content_by_lua_block {
local rabbitmq = require 'src/rabbitmq.lua';
rabbitmq.createExchange("nginx", "topic");
}
}
You probably want init_by_lua_block - the content_by_lua_block directive is specifically for responding to a particular location, whereas the init_ version is for startup:
init_by_lua_block
syntax: init_by_lua_block { lua-script }
context: http
phase: loading-config
Similar to the init_by_lua directive except that this
directive inlines the Lua source directly inside a pair of curly
braces ({}) instead of in an NGINX string literal (which requires
special character escaping).
For instance,
init_by_lua_block {
print("I need no extra escaping here, for example: \r\nblah") }
(From the init_by_lua section, "Runs the Lua code [...] on the global Lua VM level when the Nginx master process (if any) is loading the Nginx config file.")

Resources