Change nginx http servers blocks dynamically - nginx

I am doing a poc on nginx server. It would listen to ports and redirect the path to different domains. The servers I am adding is dynamic in nature.
server config blocks looks like below
attatched image
I have to fetch server name|port address from an api and create servers based on it. The number of servers may increase or decrease it is dynamic in nature.
What I tried was creating new-config.conf which is already included into nginx.conf. I am writing server config dynamically into new-config.conf and restarting nginx after it.
I need something like where I don't require to restart nginx and embed server config into nginx.conf

Related

Nginx reverse proxy blocks large files to aiohttp server

Nginx used as reverse proxy on the app server for all requests to python aiohttp web application. Browser client uploading a file with size 220kb and above to the server fails through nginx. Without nginx in the loop the file upload works fine. There is no response from nginx when uploading a larger file and it just hangs, nginx only responds after killing the POST method. I have tried modifying different client buffer sizes and timeouts but that did not help.
Tried different options with the following configuration settings -> client_body_in_file_only clean;client_body_buffer_size 32K;client_max_body_size 30M;send_timeout 300s.
I do not have your setting, but here is the guide that may help you with aiohttp and NGINX. I use python-socketio and aiohttp and nginx:
Declare file upload size in web.Application(), this is easy to miss :)
web.Application(client_max_size=1024**2*30)
Check if your NGINX has both HTTP and https and declare at both places:
client_max_body_size 30M;
Uses Gunicorn or not does NOT matter but is highly recommended.
Hopes this helps.
Steve

NGINX redirect to URL if the requested server returns status code

I have a basic nginx server set up and now I want to make specific different servers.
I have one central server which hosts a website, and two java applications which both serve APIs.
Unfortunately, it is hard for me to redirect the users to a specific URL if they do something wrong in the java application.
I want it so that when the Status Code that the proxied Server (the server I got proxied to by nginx) returned is a specific one, it redirects you to a specific URL.
Fixed it using a NGINX built in feature: http://nginx.org/en/docs/http/ngx_http_core_module.html#error_page

Server static files from FTP server using NGINX

I have a local network, on which there are some old insecure services. I use nginx reverse proxy with client certificates authentication as safe entrypoint to this local network from the Internet. Till now I used it only to proxy HTTP servers using
location / {
proxy_pass http://192.168.123.45:80/;
}
and everything works fine.
But now I would like to serve static files, that are accessible through FTP on a local server, I tried simply:
location /foo {
proxy_pass ftp://user:password#192.168.100.200:5000/;
}
but that doesn't work, and I could not find anything that would simply proxy HTTP request to FTP request.
Is there any way to do this?
Nginx doesn't support proxying to FTP servers. At best, you can proxy the socket... and this is a real hassle with regular old FTP due to it opening new connections on random ports every time a file is requested.
What you can probably do instead is create a FUSE mount to that FTP server on some local path, and serve that path with Nginx like normal. To that end, CurlFtpFS is one tool for this. Tutorial: https://linuxconfig.org/mount-remote-ftp-directory-host-locally-into-linux-filesystem
(Note: For security and reliability, it's strongly recommended you migrate away from FTP when possible. Consider SSH/SFTP instead.)

Can nginx support URL based server blocks (VirtualHosts)?

Note: “VirtualHost” is an Apache term. NGINX does not have Virtual hosts, it has “Server Blocks”. (https://www.nginx.com/resources/wiki/start/topics/examples/server_blocks/).
I know about ip-based and name-based server blocks, but is it possible to have URL-based server blocks? In other words, I want http://example.com/foo and http://example.com/bar to be served from completely independent roots. This would be a trivial problem to solve with name-based server blocks if the names were different http://example1.com and http://example2.com, but since the names are the same (example.com) and only the path part of the URL is different... Can nginx support separate server blocks for these types of URL's?
See https://nginx.org/en/docs/http/request_processing.html
It seems the only options available are: IP address, port, host, and if none of those match, then it serves the default server. So, given that the name and port are the same in both cases, the only possible solution is to put a proxy server in front of nginx, and have the proxy server distribute to the backend nginx server using a different IP or port.

HAProxy vs. Nginx

I was looking at using HAProxy and Nginx for load balancing, and I had some questions:
Should I use JUST HAProxy over Nginx for the proxy server?
Is there any reason to have HAProxy and Nginx installed on the same proxy server?
Haproxy is a "load balancer" it doesn't know to serve files or dynamic content. nginx is a web server capable of many interesting things. if you only need to load balance + HA some third web server then haproxy is enough. if you need to implement some static content or some logic in routing of the requests before terminating them on a third server then you may need nginx.
The reason you can see haproxy+nginx on the same host is that it allows you to bring down single nginx instances while haproxy continues to serve requests from other hosts. Imagine having a RR DNS using A records:
myapp.com IN A 1.1.1.1
myapp.com IN A 1.1.1.2
Where 1.1.1.1 and 1.1.1.2 are two hosts with haproxy+nginx configured to load balance between them. Now for some reason your 1.1.1.1's nginx goes down. The browsers that come to 1.1.1.1 are still being served by haproxy on it which in turn gets data from 1.1.1.2's nginx.
HAProxy is definitely the better, more fully featured loadbalancer (compared to the free nginx, not nginx plus (but one could argue that as well).
One thing that HAProxy sadly still can't do is generic UDP connections. So we used HAProxy and nginx on our logging lbs. But HAProxy released support for syslog/udp in 2.3 so we are about to change that. :)
We use HAProxy together with nginx. There are a number of reasons.
Nginx can do everything (more or less) but you don't want your load balancer serving web pages. Some error in config (which might have nothing to do with load balancing) and your entire setup comes to a screeching halt. Imagine that you have a Nodejs app, a Dotnet Core app, static files served by Nginx, and a php app. You just make some mistake and your 4 apps come to a standstill. You have lost your redundancy too if you have multiple instances of each app.
Even if you say that Nginx will only do the load balancing, Nginx doesn't support PROXY Protocol which is problematic if you forward to other servers who are also not serving the pages.
In addition there is something to be said for doing one thing and doing it well. Nginx is the master toolbox today. It does almost everything. Your load balancer is supposed to be the most stable part of your setup. Wouldn't you prefer to use something that was built just for load balancing?
If you use varnish then HAProxy works well with it and in fact they are made by the same people.
If you want an added level of balance then you can also use dns as a load balancer with multiple HAPROXY instances. Dns is not meant for this perse but you will always have some weak link. Your load balancer can crash too even if it's managed by your cloud provider. Most web browsers today will try other servers if there is more than one in your dns entry so it's like a load balancer. Your dns should be very reliable thus increasing your uptime.
We use 2 haproxy instances with 2 varnish instances with two dns entries.

Resources