How can dynamically create upstream block with Openresty and Lua? - nginx

I need to create an upstream block in the default.conf dynamically with Openresty and Lua.
upstream my_gateway{
for server in #SERVERS_ENV:
server xxx.xxx.xxx.xx:yyyy max_fails=3 fail_timeout=30s;
}

ngx.balancer is what you are looking for.

Related

does nginx support tcp and http proxy on single instance?

I have a requirement where I have a single nginx instance to act as a VIP for http requests and also the mysql db cluster.
Is it possible to put the same configs under one host? TCP syntax looks different than the http. Please help me with some sample config which will work.
This worked for me.
https://docs.nginx.com/nginx/admin-guide/load-balancer/tcp-udp-load-balancer/
http {
# http content
}
upstream servers{
server server1;
server server2 backup;
}
}
stream {
upstream mygroup {
least_conn;
server db_master:3309;
server db_slave:3309;
}
server {
listen 3309;
proxy_pass mygroup;
proxy_timeout 5s;
proxy_connect_timeout 1s;
}
}

how to proxy RMI call with nginx

does anyone know how to proxy RMI with nginx?
Nginx v1.9+
my current nginx server block.
stream {
upstream QA1{
server 10.168.85.39:30900;
}
upstream QA2 {
server 10.51.67.17:30900;
}
server {
listen 30900;
proxy_pass QA1;
}
server {
listen 30901;
proxy_pass QA2;
}
}
I'm getting a timeout error on the client-side
My current solution is to convert RMI to HTTP, and proxy HTTP with Nginx.

Load on a server is high using Nginx

I have a server which continuously sends http requests to a Nginx server. Nginx will upstream it to 4 servers. But i am seeing a problem where load balancing is not working well as one particular server receives 50% of requests. My nginx config has below.
upstream cmdc {
server cmdc2b:5600 max_fails=3 fail_timeout=30s;
server cmdc2a:5600 max_fails=3 fail_timeout=30s;
server cmdc1d:5600 max_fails=3 fail_timeout=30s;
server cmdc1c:5600 max_fails=3 fail_timeout=30s;
keepalive 30;
}
Can someone help me here? Does any other parameter affects this ?

Nginx Reverse Proxy upstream not working

I'm having trouble figuring out load balancing on Nginx. I'm using:
- Ubuntu 16.04 and
- Nginx 1.10.0.
In short, when I pass my ip address directly into "proxy_pass", the proxy works:
server {
location / {
proxy_pass http://01.02.03.04;
}
}
When I visit my proxy computer, I can see the content from the proxy ip...
but when I use an upstream directive, it doesn't:
upstream backend {
server 01.02.03.04;
}
server {
location / {
proxy_pass http://backend;
}
}
When I visit my proxy computer, I am greeted with the default Nginx server page and not the content from the upstream ip address.
Any further assistance would be appreciated. I've done a ton of research but can't figure out why "upstream" is not working. I don't get any errors. It just doesn't proxy.
Okay, looks like I found the answer...
two things about the backend servers, at least for the above scenario when using IP addressses:
a port must be specified
the port cannot be :80 (according to #karliwsn the port can be 80 it's just that the upstream servers cannot listen to the same port as the reverse proxy. I haven't tested it yet but it's good to note).
backend server block(s) should be configured as following:
server {
# for your reverse_proxy, *do not* listen to port 80
listen 8080;
listen [::]:8080;
server_name 01.02.03.04;
# your other statements below
...
}
and your reverse proxy server block should be configured like below:
upstream backend {
server 01.02.03.04:8080;
}
server {
location / {
proxy_pass http://backend;
}
}
It looks as if a backend server is listening to :80, the reverse proxy server doesn't render it's content. I guess that makes sense, since the server is in fact using default port 80 for the general public.
Thanks #karliwson for nudging me to reconsider the port.
The following example works:
Only thing to mention is that, if the server IP is used as the "server_name", then the IP should be used to access the site, means in the browser you need to type the URL as http://yyy.yyy.yyy.yyy or (http://yyy.yyy.yyy.yyy:80), if you use the domain name as the "server_name", then access the proxy server using the domain name (e.g. http://www.yourdomain.com)
upstream backend {
server xxx.xxx.xxx.xxx:8080;
}
server {
listen 80;
server_name yyy.yyy.yyy.yyy;
location / {
proxy_pass http://backend;
}
}

What does upstream mean in nginx?

upstream app_front_static {
server 192.168.206.105:80;
}
Never seen it before, anyone knows, what it means?
It's used for proxying requests to other servers.
An example from http://wiki.nginx.org/LoadBalanceExample is:
http {
upstream myproject {
server 127.0.0.1:8000 weight=3;
server 127.0.0.1:8001;
server 127.0.0.1:8002;
server 127.0.0.1:8003;
}
server {
listen 80;
server_name www.domain.com;
location / {
proxy_pass http://myproject;
}
}
}
This means all requests for / go to the any of the servers listed under upstream XXX, with a preference for port 8000.
upstream defines a cluster that you can proxy requests to. It's commonly used for defining either a web server cluster for load balancing, or an app server cluster for routing / load balancing.
If we have a single server we can directly include it in the proxy_pass directive. For example:
server {
...
location / {
proxy_pass http://192.168.206.105:80;
...
}
}
But in case if we have many servers we use upstream to maintain the servers. Nginx will load-balance based on the incoming traffic, as shown in this answer.

Resources