nginx-rtmp module on nginx test case - linux

I am trying to stream a video using nginx-rtmp with rtmp protocol.which i setup on my vm(started headless). I created a index.html and embedded on it JWPlayer with the rtmp url. On my localhost,
how can I play the video from the browser
and is my configuration all right ?
I can't able to browse the video from my real machine
index.html
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>VoD Example</title>
<script src="http://jwpsrv.com/library/s7iNvOAyEeSMdQ4AfQhyIQ.js"></script>
</head>
<body>
<div id='videotest'></div>
<script type='text/javascript'>
jwplayer('videotest').setup({
file: 'rtmp://192.168.x.xxx/vod/bird.mp4',
width: '50%',
aspectratio: '16:9'
});
</script>
</body>
</html>
here is my nginx.conf
#user nobody;
worker_processes 1;
error_log logs/error.log debug;
events {
worker_connections 1024;
}
http {
proxy_ignore_client_abort on;
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 8080;
server_name 0.0.0.0;
# rtmp stat
location /stat {
rtmp_stat all;
rtmp_stat_stylesheet stat.xsl;
}
location /stat.xsl {
# you can move stat.xsl to a different location
root /var/www/;
}
# rtmp control
location /control {
rtmp_control all;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
rtmp {
server {
listen 1935;
notify_method get;
chunk_size 8192;
application vod {
allow play all;
wait_video on;
play /var/www/html/video;
push rtmp://192.168.x.xxx/vod/;
}
}
}
I kept a videos /var/www/html/video/bird.mp4
access.log
192.168.x.xx - - [15/Sep/2018:12:25:21 +0530] "GET /stat.xsl HTTP/1.1" 304 0 "http://192.168.x.xxx:8080/stat" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/69.0.3497.81 Chrome/69.0.3497.81 Safari/537.36"
error.log
2018/09/15 12:24:49 [notice] 23499#0: nginx/1.15.1
2018/09/15 12:24:49 [notice] 23499#0: built by gcc 6.3.0 20170516 (Debian 6.3.0-18+deb9u1)
2018/09/15 12:24:49 [notice] 23499#0: OS: Linux 4.9.0-6-amd64
2018/09/15 12:24:49 [notice] 23499#0: getrlimit(RLIMIT_NOFILE): 1024:1048576
2018/09/15 12:24:49 [notice] 23500#0: start worker processes
2018/09/15 12:24:49 [notice] 23500#0: start worker process 23501
2018/09/15 12:25:45 [info] 23501#0: *2 client closed connection while waiting for request, client: 192.168.x.xx, server: 0.0.0.0:8080
2018/09/15 12:26:15 [info] 23501#0: *3 client closed connection while waiting for request, client: 192.168.x.xx, server: 0.0.0.0:8080

Related

Nginx connect() failed (111: Connection Refused) while connecting to upstream

I'm trying to deploy a MERN app to a DigitalOcean droplet using Nginx (I've used a connection string to connect to a MongoDB atlas instance). The frontend static files seem to successfully work, but I face two issues when it comes to the backend (a Nodejs/express app).
First, I get a connect() failed (111: connection refused) for every request from the frontend to the backend.
2021/07/12 23:54:32 [error] 5101#5101: *394 connect() failed (111: Connection refused) while
connecting to upstream, client: "My Ip", server: "example.com", request: "GET
/api/images HTTP/1.1", upstream: "http://127.0.0.1:5000/images", host: "example.com",
referrer: "https://example.com/"
Second, I face a 404 Not Found nginx/1.14.0 (Ubuntu) error while trying to check the routes through the Postman.
Not sure both of these are the same problem.
Here's my Nginx configuration in /etc/nginx/sites-available/default:
server {
root var/www/html;
index index.html index.htm index.nginx-debian.html;
server_name example.com www.example.com;
location /api/ {
proxy_pass http://localhost:5000/;
}
location / {
root /home/${my_non_root_user}/frontend/deploy;
try_files $uri /index.html;
}
listen [::]:443 SSL ipv6only=on; #managed by Certbot
listen 443 SSL; #managed by Certbot
ssl_certificate /etc/letsencrypt/live/${example.com}/fullchain.pem; #managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/${example.com}/privkey.pem; #managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; #managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; #managed by Certbot
}
server {
if ($host = www.example.com) {
return 301 https://$host$request_uri;
} #managed by Certbot
if ($host = example.com) {
return 301 https://$host$request_uri;
} #managed by Certbot
listen 80 default_server;
listen [::]:80 default_server;
server_name example.com www.example.com;
return 404; #managed by Certbot
}
This is how my backend server.js looks like:
import express from 'express'
import path from 'path'
import dotenv from 'dotenv'
import connectDB from './config/db.js'
import memberRoutes from './routes/memberRoutes.js'
import messageRoutes from './routes/messageRoutes.js'
import fileRoutes from './routes/fileRoutes.js'
dotenv.config()
connectDB()
const app = express()
app.use(express.json())
// routes
app.use('/api/members', memberRoutes)
app.use('/api/messages', messageRoutes)
app.use('/api/files', fileRoutes)
const __dirname = path.resolve()
app.use('/files', express.static(path.join(__dirname, '/files')))
const PORT = process.env.PORT || 5000
app.listen(
PORT,
console.log(`Server running in ${process.env.NODE_ENV} mode on port
${PORT}`)
)
When I make a request in Postman, to {{URL}}/messages (No difference it is a GET, or POST), where {{URL}} is THE_IP_TO_DROPLET, I get this output:
<HTML>
<head>
<title>404 Not Found</title>
</head>
<body bgcolor="white">
<center>
<h1>404 Not Found</h1>
</center>
<hr>
<center>nginx/1.14.0 (Ubuntu)</center>
</body>
</html>
Do you have any suggestion? Totally stuck in this for the whole weekend!
Thank you.

Error: unknown directive "rtmp_stat" (NGNIX fails when using NOALBS .conf file)

I've never used Linux or Raspberry Pi before but I bought one in order to create my own RTMP server. I made one with NGINX and got it working. Now I'm trying to incorporate NOALBS for more functionality. I renamed my working .conf to nginx-old.conf and copied in the NOALBS .conf and now NGINX fails with this error.
nginx: [emerg] unknown directive "rtmp_stat" in /etc/nginx/nginx.conf:26
nginx: configuration file /etc/nginx/nginx.conf test failed
nginx.service: Control process exited, code=exited, status=1/FAILURE
nginx.service: Failed with result 'exit-code'.
My .conf is as follows:
worker_processes 1;
events {
worker_connections 1024;
}
http {
sendfile off;
tcp_nopush on;
directio 512;
include mime.types;
default_type application/octet-stream;
ignore_invalid_headers on;
log_format compression '';
server {
listen 80;
server_name localhost;
# This URL provides RTMP statistics in XML
location /stat {
if ($request_method = "GET") {
add_header "Access-Control-Allow-Origin" *;
}
rtmp_stat all;
# Use this stylesheet to view XML as web page
# in browser
rtmp_stat_stylesheet /stat.xsl;
}
location /stat.xsl {
# XML stylesheet to view RTMP stats.
# Copy stat.xsl wherever you want
# and put the full directory path here
# root /path/to/stat.xsl/;
root /var/www/html/stat.xsl;
}
location /control {
rtmp_control all;
}
}
}
rtmp {
log_format compression '';
server {
listen 1935;
ping 30s;
notify_method get;
chunk_size 8192;
ack_window 8192;
sync 4ms;
interleave on;
access_log logs/rtmp-access.log compression;
# Stream to "rtmp://IPHERE/publish/live".
application publish {
live on;
wait_video on;
wait_key on;
exec_options on;
publish_notify on;
play_restart on;
drop_idle_publisher 4s;
idle_streams off;
sync 4ms;
interleave on;
}
}
}
The only thing I've edited is actually adding the stat.xsl path and my Pi's IP address (which I've scrubbed here). Can anyone help me out?
Add this header :
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

Express show Cannot GET/ at post function on Nginx

I have this code saved on my Nginx server with Let's Encrypt SSL.
My intention on this code is to be backend server.
app.use(bodyParser.urlencoded({ extended: true }));
app.get('/', function(res, req) {
res.send('GET ROOT');
});
app.post('/', function(res, req) {
res.send('POST ROOT');
});
app.listen(port, () => {
console.log('We are live on ' + port);
});
The code works great on local.
But when I deployed it on remote server, first I request GET example.com/ it returned GET ROOT. Then I did POST example.com/ it returned GET ROOT. It should be POST ROOT.
And this is my server config snippet from sites-available
server {
listen 80 default_server;
listen[::]: 80 default_server;
server_name example.com;
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;
}
# some ssh default path
}
The server is successfully propagated with a domain, and I think there is no problem with loading index.html or something with GET function. And I run the js with nodemon at this development phase.
Please advice..
I thought it was about CORS and I put default CORS configuration, but still nothing's changed.
Then I add this post function without a get with a same endpoint.
app.post('/post', (req, res) => {
res.send('POST POST');
});
It returned this result
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>Cannot GET /post</pre>
</body>
</html>
For Nginx error log when I do GET and POST request (retrieved from /var/log/nginx/error.log.1 )
2017/10/24 16:27:19 [error] 1667#1667: *14 connect() failed (111: Connection refused) while connecting to upstream, client: 125.163.124.227, server: officially.id, request: "GET $
2017/10/24 20:52:06 [error] 1667#1667: *72 connect() failed (111: Connection refused) while connecting to upstream, client: 94.23.35.126, server: officially.id, request: "GET / H$
2017/10/24 21:43:26 [notice] 2556#2556: signal process started
2017/10/24 21:48:01 [error] 2758#2758: *2 connect() failed (111: Connection refused) while connecting to upstream, client: 114.124.167.30, server: officially.id, request: "GET /t$
2017/10/24 21:57:04 [notice] 2877#2877: signal process started
2017/10/24 22:00:30 [emerg] 2919#2919: invalid value "=" in /etc/nginx/sites-enabled/default:53
2017/10/24 22:00:39 [emerg] 2924#2924: invalid value "=" in /etc/nginx/sites-enabled/default:53
2017/10/24 22:10:15 [notice] 3060#3060: signal process started
2017/10/25 03:34:42 [error] 3063#3063: *36 connect() failed (111: Connection refused) while connecting to upstream, client: 71.6.202.198, server: officially.id, request: "GET /xx$
When I tried to capture my app.js error and output, nothing is written there except string logged at app.listen(...).

Nginx wrong request interpretation

I got strange request received/interpreted by nginx.
What I'am doing...
Sending request in shell:
$ echo -ne "GET / HTTP/1.1\r\nHost: localhost\r\n\r\n" | nc localhost 80
Got log /var/log/nginx/access.log:
127.0.0.1 - - [23/Aug/2016:11:49:26 +0000] "#x\xEF\x01\xDCe\xCA\xB6t HTTP/1.1" "-" 400 172 "-" "-" "-"
Somehow nginx interpreted GET / HTTP/1.1\r\nHost: localhost\r\n\r\n as #x\xEF\x01\xDCe\xCA\xB6t HTTP/1.1. Seems like it changes first several bytes.
Env:
Openwrt, Linux-3.18
nginx-1.4.7
nginx.conf:
user nobody nogroup;
worker_processes 1;
error_log /var/log/nginx/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;
log_format main '$remote_addr - $remote_user [$time_local]"$request" "$request_uri" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
large_client_header_buffers 4 16k;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root /www;
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;
}
}
}
Strace:
At the same time I can see in strace correct request recv(3, "GET / HTTP/1.1\r\nHost: localhost\r"..., 1024, 0) = 35.
So as a conclusion: something wrong with nginx or its configuration.
Update: Simple solution to start using the new version;)

Can't configure NGINX to cache the images

I have been trying to cache images that are stored in nginx server, I am quite new to nginx I have installed it and configure it with php5-fpm, I have been reading many tutorial regarding caching images and php files. I have succeeded in caching the PHP files but I can't cache the images here is part of the nginx config file:
fastcgi_cache_path /etc/nginx/cache levels=1:2 keys_zone=MYAPP:100m inactive=60m;
fastcgi_cache_key "$scheme$request_method$host$request_uri";
fastcgi_cache_use_stale error timeout invalid_header http_500;
add_header X-Cache $upstream_cache_status;
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /usr/share/nginx/html;
index index.html index.htm;
# Make site accessible from http://localhost/
server_name localhost;
set $skip_cache 0;
# POST requests and urls with a query string should always go to PHP
if ($request_method = POST) {
set $skip_cache 1;
}
if ($query_string != "") {
set $skip_cache 1;
}
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_cache MYAPP;
fastcgi_cache_valid 200 60m;
}
location ~* ^.+\.(jpg|jpeg|gif|png)$ {
access_log off; log_not_found off; expires 1d;
}
location ~ /\. { deny all; access_log off; log_not_found off; }
}
now when I run curl -I http://xxx.xxx.xxx.xxx/script.php I can see the md5 of the script created in /etc/nginx/cache folder and I can see the X-Cache : HIT
However, when I run curl -I http://xxx.xxx.xxx.xxx/image.jpg no file created in the /etc/nginx/cache and I have the following result in the console:
HTTP/1.1 200 OK
Server: nginx/1.4.6 (Ubuntu)
Date: Tue, 17 Jun 2014 08:14:37 GMT
Content-Type: image/jpeg
Content-Length: 55936
Last-Modified: Tue, 17 Jun 2014 04:30:11 GMT
Connection: keep-alive
ETag: "539fc453-da80"
Expires: Wed, 18 Jun 2014 08:14:37 GMT
Cache-Control: max-age=86400
Accept-Ranges: bytes
now it looks fine however when I run it again the expiry date will keep changing as if it is calling new image not the cache
HTTP/1.1 200 OK
Server: nginx/1.4.6 (Ubuntu)
Date: Tue, 17 Jun 2014 08:16:34 GMT
Content-Type: image/jpeg
Content-Length: 55936
Last-Modified: Tue, 17 Jun 2014 04:30:11 GMT
Connection: keep-alive
ETag: "539fc453-da80"
Expires: Wed, 18 Jun 2014 08:16:34 GMT
Cache-Control: max-age=86400
Accept-Ranges: bytes
my questions are:
1- should the cached images be shown in the cache folder?
2- does fastcgi capable of caching the images?
3- what can I do to fix the image caching problem?
Sorry for the beginner questions, but I can't find the answer.

Resources