NGINX : no live upstreams while connecting to upstream - nginx

I am running multiple filebeats on a server listening on various ports . I have set of udp packets incoming on a server on port 2055 . These packets are routed to upstream filebeat server in round robbin manner . When I directly listen on a single filebeat on port 2055 , filebeat can process around 20k/second without nginx . However I route these packets through nginx the above below error is encountered
udp client: 10.224.3.178, server: 0.0.0.0:2055, upstream: "stream_backend", bytes from/to client:192/0, bytes from/to upstream:0/0
Following is my Nginx stream Block Configuration
`
stream {
log_format proxy '$remote_addr [$time_local] '
'$protocol $status $bytes_sent $bytes_received '
'$session_time "$upstream_addr" '
'"$upstream_bytes_sent" "$upstream_bytes_received" "$upstream_connect_time"';
access_log /var/log/nginx/stream.log proxy;
upstream stream_backend {
# least_conn;
# upstream_connect_time 10 ;
# random two least_time=connect;
zone backend 100k;
server 127.0.0.1:2056;
server 127.0.0.1:2057;
server 127.0.0.1:2058;
server 127.0.0.1:2059;
server 127.0.0.1:2060;
server 127.0.0.1:2061;
server 127.0.0.1:2062;
server 127.0.0.1:2063;
server 127.0.0.1:2064;
server 127.0.0.1:2065;
}
server {
listen 2055 udp;
proxy_pass stream_backend;
proxy_bind $remote_addr transparent;
proxy_buffer_size 10000k;
# upstream_connect_time 10 ;
proxy_timeout 10s;
# proxy_connect_timeout 75s;
proxy_responses 1;
# health_check udp;
}
}
`
Nginx has a bunch of timeout directives. I don't know if I'm missing something important. Any help would be highly appreciated....

I guess proxypass is not taken correctly by Nginx.
proxy_pass should be under location.
Try add under
location ~ {}

Related

Getting NGINX "failed (111: Connection refused) while proxying and reading from upstream, udp"

I have an existing reverse proxying of http/https and I wanted to add raw tcp/udp stream.
Using Module ngx_stream_core_module I added this config:
stream {
upstream app-udp{
server 192.168.165.10:50000 max_fails=0;
}
upstream app-tcp{
server 192.168.165.10:50001 max_fails=0;
}
server {
listen 192.168.134.20:6653 udp;
preread_buffer_size 16k;
preread_timeout 30s;
proxy_protocol_timeout 30s;
proxy_timeout 10m;
proxy_pass "app-udp";
}
server {
listen 192.168.134.20:6654;
preread_buffer_size 16k;
preread_timeout 30s;
proxy_protocol_timeout 30s;
proxy_timeout 10m;
proxy_pass "app-tcp";
}
}
On the backend I set up an echo service just to bounce back the packets as replay.
Testing TCP works great with sending and getting response all the way.
UDP however is failing on the response from the echo service to NGINX.
Wireshark shows:
"ICMP: icmp destination unreachable port unreachable"
The nginx error log shows:
"*352 recv() failed (111: Connection refused) while proxying and
reading from upstream, udp client: 192.168.165.10, server:
192.168.134.20:6653, upstream: "192.168.165.10:50000", bytes from/to client:35/0, bytes from/to upstream:0/35"
I couldn't find any reference online to this type of error from NGINX...
What could be the problem? Have missed some configuration on NGINX of perhaps issue with the network on the backend side? Thank you.
i had exactly the same problem. Then i added proxy_responses 0; to the configuration and the error disappeared.
stream {
upstream app-udp{
server 192.168.165.10:50000 max_fails=0;
}
upstream app-tcp{
server 192.168.165.10:50001 max_fails=0;
}
server {
listen 192.168.134.20:6653 udp;
preread_buffer_size 16k;
preread_timeout 30s;
proxy_protocol_timeout 30s;
proxy_timeout 10m;
proxy_pass "app-udp";
proxy_responses 0;
}
server {
listen 192.168.134.20:6654;
preread_buffer_size 16k;
preread_timeout 30s;
proxy_protocol_timeout 30s;
proxy_timeout 10m;
proxy_pass "app-tcp";
proxy_responses 0;
}
}
Hope this helps

Nginx proxy_pass through 2 servers and custom headers getting "lost"

I am having a problem with my Nginx configuration.
I have an Nginx server(A) that adds custom headers and then that proxy_passes to another server(B) which then proxy passes to my flask app(C) that reads the headers. If I go from A -> C the flask app can read the headers that are set but if I go through B (A -> B -> C) the headers seem to be removed.
Config
events {
worker_connections 512;
}
http {
# Server B
server {
listen 127.0.0.1:5001;
server_name 127.0.0.1;
location / {
proxy_pass http://127.0.0.1:5000;
}
}
# Server A
server {
listen 4999;
server_name domain.com;
location / {
proxy_pass http://127.0.0.1:5001;
proxy_set_header X-Forwarded-User 'username';
}
}
}
Flask app running on 127.0.0.1:5000
If I change the server A config to proxy_pass http://127.0.0.1:5000 then the Flask app can see the X-Forwarded-User but if I go through server B the headers are "lost"
I am not sure what I am doing wrong. Any suggestions?
Thanks
I can not reproduce the issue, sending the custom header X-custom-header: custom in my netcat server i get:
nc -l -vvv -p 5000
Listening on [0.0.0.0] (family 0, port 5000)
Connection from localhost 41368 received!
GET / HTTP/1.0
Host: 127.0.0.1:5000
Connection: close
X-Forwarded-User: username
User-Agent: curl/7.58.0
Accept: */*
X-custom-header: custom
(see? the X-custom-header is on the last line)
when i run this curl command:
curl -H "X-custom-header: custom" http://127.0.0.1:4999/
against an nginx server running this exact config:
events {
worker_connections 512;
}
http {
# Server B
server {
listen 127.0.0.1:5001;
server_name 127.0.0.1;
location / {
proxy_pass http://127.0.0.1:5000;
}
}
# Server A
server {
listen 4999;
server_name domain.com;
location / {
proxy_pass http://127.0.0.1:5001;
proxy_set_header X-Forwarded-User 'username';
}
}
}
thus i can only assume that the problem is in the part of your config that you isn't showing us. (you said it yourself, it's not the real config you're showing us, but a replica. specifically, a replica that isn't showing the problem)
thus i have voted to close this question as "can not reproduce" - at least i can't.

How do i configure MQTT in nginx

I need to configure MQTT in nginx , I have tried with following code
I have added one .conf file in /usr/local/etc/nginx/mqtt.conf
and mqtt.conf contains:-
log_format mqtt '$remote_addr [$time_local] $protocol $status
$bytes_received '
'$bytes_sent $upstream_addr';
upstream hive_mq {
server 127.0.0.1:18831; #node1
server 127.0.0.1:18832; #node2
server 127.0.0.1:18833; #node3
zone tcp_mem 64k;
}
match mqtt_conn {
# Send CONNECT packet with client ID "nginx health check"
send
\x10\x20\x00\x06\x4d\x51\x49\x73\x64\x70\x03\x02\x00\x3c\x00\x12
\x6e\x67\x69\x6e\x78\x20\x68\x65\x61\x6c\x74\x68\x20\x63\x68\x65
\x63\x6b;
expect \x20\x02\x00\x00; # Entire payload of CONNACK packet
}
server {
listen 1883;
server_name localhost;
proxy_pass hive_mq;
proxy_connect_timeout 1s;
health_check match=mqtt_conn;
access_log /var/log/nginx/mqtt_access.log mqtt;
error_log /var/log/nginx/in.log; # Health check notifications
#access_log /var/log/nginx/access.log;
#error_log /var/log/nginx/error.log; # Health check notifications
}
I have also added few code inside nginx.conf:-
worker_processes 1;
events {
worker_connections 1024;
}
stream {
include stream_conf.d/*.conf;
}
and i have tried to publish message from mosquitto like :
mosquitto_pub -d -h localhost -p 1883 -t "topic/test" -m "test123" -i
"thing001"
but i am not able to connect with nginx, Can any help me how to configure this .
Thanks in advance :)

How to test load balancing in nginx?

I done congfiguration in nginx for redirection and it works successfully.
But in that i want load balancing :-
for that i already create load-balancer.conf as well as give server name into that file like :-
upstream backend {
# ip_hash;
server 1.2.3.4;
server 5.6.7.8;
}
server {
listen 80;
location / {
proxy_pass http://backend;
}
}
In both instances i did same configuration
and it default uses round-robin algorithm so in that request transfer via one pc to another pc.....
but it were not working
can any one suggest me anything that secong request going to another server 5.6.7.8
so i can check load balancing.
Thankyou so much.
Create a log file for upstream to check request is going to which server
http {
log_format upstreamlog '$server_name to: $upstream_addr {$request} '
'upstream_response_time $upstream_response_time'
' request_time $request_time';
upstream backend {
# ip_hash;
server 1.2.3.4;
server 5.6.7.8;
}
server {
listen 80;
access_log /var/log/nginx/nginx-access.log upstreamlog;
location / {
proxy_pass http://backend;
}
}
and then check your log file
sudo cat /var/log/nginx/nginx-access.log;
you will see log like
to: 5.6.7.8:80 {GET /sites/default/files/abc.png HTTP/1.1} upstream_response_time 0.171 request_time 0.171

How to record reverse proxy upstream server serving request in Nginx log?

We use Nginx as a reverse proxy with this setup:
upstream frontends {
server 127.0.0.1:8000;
server 127.0.0.1:8001;
server 127.0.0.1:8002;
[...]
}
server {
location / {
proxy_pass http://frontends;
[...]
}
[...]
}
As part of the access log, I would like to record the upstream server that has served the request, which in our case just means the associated localhost port.
The variables in the documentation (http://wiki.nginx.org/HttpProxyModule#Variables) mention $proxy_host and $proxy_port but in the log they always end up with the values "frontends" and "80".
First add new logging format
log_format upstreamlog '[$time_local] $remote_addr - $remote_user - $server_name $host to: $upstream_addr: $request $status upstream_response_time $upstream_response_time msec $msec request_time $request_time';
Example output:
[18/Nov/2019:10:08:15 -0700] <request IP> - - - <config host> <request host> to: 127.0.0.1:8000: GET /path/requested HTTP/1.1 200 upstream_response_time 0.000 msec 1574096895.474 request_time 0.001
and then redefine accesslog as
access_log /var/log/nginx/access.log upstreamlog;
log_format goes to http {} section, access_log can be inside location.
Use $upstream_addr and you will get, for example, 127.0.0.1:8000 or unix:/home/my_user/www/my_site/tmp/.unicorn.sock

Resources