Configuration Nginx for Microservices Api-Gateway - nginx

I want to proxy request to current network interface hostnames with Nginx such as:
url /catalog/categories/... proxy to hostname catalog:8080 with url /categories/...
url /vitrine/products/... proxy to hostname vitrine:8080 with url /products/...
Is any idea?

The configuration should look like this:
http {
...
server {
listen 80;
server_name yourdomain;
set $mycatalog catalog:8080;
set $myvitrine vitrine:8080;
}
location ~ ^/catalog/categories/(.*)$ {
proxy_set_header Host $host:$server_port;
proxy_pass http://$mycatalog/categories/$1$is_args$args;
}
location ~ ^/vitrine/products/(.*)$ {
proxy_set_header Host $host:$server_port;
proxy_pass http://$myvitrine/products/$1$is_args$args;
}
}

Related

nginx proxy_pass to dynamic url

I have multiple backend servers and I want to proxy all servers using a single nginx server instance. I don't want to change nginx.conf whenever I add a new backend server.
For example: Server 1 : 192.168.10.1:8080, Server2: 192.168.10.2:8080, etc
Nginx is running on example.com. I want to access Server1 by using example.com?ip=192.168.10.1, example.com?ip=192.168.10.2 etc
I tried this configuration, but it is giving 500 error page.
location / {
proxy_pass http://$arg_ip:8080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
Is there something I am missing? Is there any other way of achieving this?
server {
server_name dynamic_host;
listern 8080;
#resolver 8.8.8.8;
#seems you don't need resolver because you use ip address
location / {
if ( $arg_address != "" ) {
proxy_pass $arg_address;
#proxy_pass $arg_address$uri
#proxy_pass $arg_address$request_uri
}
}
}
the difference between the three proxy_pass
$proxy_address
example.com?address=http://192.168.10.2:8080/ goes to
http://192.168.10.2:8080/
$proxy_address$uri
example.com/test/path?address=http://192.168.10.2:8080/ goes to
http://192.168.10.2:8080/test/path
$proxy_address$request_uri
example.com/test/path?address=http://192.168.10.2:8080/&param=value goes to
http://192.168.10.2:8080/test/path?address=http://192.168.10.2:8080/&param=value
you can change the param address to ip, in this case, don't forget to change $arg_address to $arg_ip.
reference:
http://nginx.org/en/docs/http/ngx_http_core_module.html#variables

how to expose server port in upstream block of Nginx configuration?

We have a handful of homogenous application loosely under SOA pattern. Because of homogeneity, we have been able to define some neat pattern in Nginx to proxy all of our SOA apps through one configuration. Following Nginx configuration is absolutely working absolute wonders in conjunction with DNSmasq to resolve anything.yourdomain.devel eg. a.stackoverflow.devel, b.stackoverflow.devel domains and route that to appropriate app servers under your project folder via designated ports via maps.
worker_processes 2;
events {
worker_connections 1024;
}
http {
map $host $static_content_root {
hostnames;
default /path/to/project/folder;
# For typical standalone apps living in your project directory
# *.myapp.local.devel -> /path/to/project/myapp/public
~^([^\.]+\.)*(?<app>[^\.]+)\.devel$ /path/to/project/folder/$app/public; #rails pattern
}
map $app $devel_proxy_port1 {
default 3000;
domain1 3000;
domain2 4000;
}
map $app $devel_proxy_port2 {
default 3001;
domain1 3001;
domain2 4001;
}
server {
listen 127.0.0.1;
server_name ~^([^\.]+\.)*(?<app>[^\.]+)\.[^\.]+.devel$;
location / {
root $static_content_root; # Using the map we defined earlier
try_files $uri $uri/index.html #dynamic;
}
location #dynamic {
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forward-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forward-Proto http;
proxy_set_header X-Nginx-Proxy true;
proxy_redirect off;
proxy_pass http://127.0.0.1:$devel_proxy_port1;
}
}
}
Now, in order to simulate multiple servers behind Nginx load balancer. I thought of doing following proxy configuration which points to upstream rather than directly pointing to one server:port pair.
proxy_pass http://backend;
upstream backend {
server http://127.0.0.1:$devel_proxy_port1;
server http://127.0.0.1:$devel_proxy_port2;
}
I thought above would work but it always emits following error hinting the variables of map blocks are not available inside upstream context.
[emerg] 69478#0: invalid host in upstream "http://127.0.0.1:$devel_proxy_port1" in /usr/local/etc/nginx/nginx.conf:57
Is this an expected behavior?
Yes, variable can not be used inside upstream. You can create few upstream blocks with different names (upstream backend, upstream backend_domain, etc), resolve upstream name through map and put this variable to proxy_pass:
upstream backend {
server http://127.0.0.1:3000;
server http://127.0.0.1:3001;
}
upstream backend_domain1 {
server http://127.0.0.1:3002;
server http://127.0.0.1:3003;
}
upstream backend_domain2 {
server http://127.0.0.1:3004;
server http://127.0.0.1:3005;
}
...
upstream backend_domain30 {
server http://127.0.0.1:3060;
server http://127.0.0.1:3061;
}
map $app $devel_proxy {
default backend;
domain1 backend_domain1;
domain2 backend_domain2;
...
domain30 backend_domain30;
}
...
proxy_pass $devel_proxy;
...
In some cases you can skip map block using $app inside proxy_pass: proxy_pass backend_$app;, but need additional checks for $app values. Also, map allow to to map different "domains" to same applications.

domain.com needs to be re routed to IP_ADDRESS:8080 nginx config weird configuration

I have the following configuration in my nginx conf file for my domain
server {
listen 80;
server_name domain.com
location / {
proxy_pass http://localhost:8080;
proxy_set_header host http://domain.com;
proxy_cache_bypass $http_upgrade;
}
}
and when I got to domain.com -> nothing I have to append :8080 and Bam! I get the app just fine
however, I want to be able to just redirect domain.com -> to localhost:8080 where the app is running.
How can I fix this in my nginx config file under sites-available
Thank you,
have a /nginx/sites-available conf file as following afterward make a symbolic link to it at /nginx/sites-enabled/
server {
listen 80;
server_name domain.com;
location / {
proxy_pass http://localhost:3000;
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
inside javascript file
create the server with the private ip addr of the server NOT LOCAL HOST
like the following
//change ip address form localhost to the private IP address of server
app.listen(3000, '192.241.191.56', function () {
console.log('Example app listening on port 3001!');
});

Dynamic routing with NGINX

I am an amateur of NGINX, I want to setup NGINX as a Reverse Proxy for my web server.
I would like to know that the NGINX these things as listed below:
When a browser send request with URL: http://nginxproxy.com/client/1.2.3.4/, this request should be passed to the client with IP 1.2.3.4 http://1.2.3.4/, the browser should still show the URL nginxproxy/client/1.2.3.4/
And the same for:
nginxproxy.com/client/2.3.4.5 --> //2.3.4.5
nginxproxy.com/client/2.3.4.6 --> //2.3.4.6
All the others requests that doesn't mach the pattern should come to my default server myserver.
Can I do this by using NGINX?
After researching, I tried with the below configuration:
But unlucky, It doesn't work. The address was changed to http:/1.2.3.4 on browser's address bar, instead of http:/nginxproxy.com/client/1.2.3.4 as expected.
server {
listen 80;
location ~ ^/client {
rewrite ^/client/?(.*) /$2 break;
proxy_pass $scheme://$1;
}
location / {
proxy_pass http://myserver.com;
}
}
Any help is much appreciated.
Doing some more research and based on #Cole input, here is my answer:
location ~ ^/client/(?<site>[^/]+)/? {
rewrite ^.*\/client\/(?<site>[^\/]+)\/?(.*) /$2 break; #passing all the remaining request URIs after <site> group to client server
proxy_pass $scheme://$site;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host/client/$site; #this help to keep the address as it is on the browser's address bar
proxy_set_header X-Forwarded-Proto $scheme;
}
location / {
proxy_pass $scheme://myserver.com
}
server {
listen 80;
location /client/ {
rewrite ^/client/(?<site>[^/]+)/? $scheme://$site;
}
location / {
proxy_pass $scheme://myserver.com;
}
}

How to redirect to specific upstream servers based on request URL in Nginx?

I'm using Nginx as a load balancer for my 5 app servers.
I'd like to redirect to specific servers based on the request URL, for instance:
acme.com/category/* => Server #1
acme.com/admin/* => Server #2
api.acme.com => Server #3
Fallback for any other URL => Server #4, #5
My config looks like:
upstream backend {
least_conn;
server 10.128.1.1;
server 10.128.1.2;
server 10.128.1.3;
server 10.128.1.4;
server 10.128.1.5;
}
server {
listen 80;
server_name _;
location / {
proxy_set_header Host $host;
proxy_pass http://backend;
}
}
I have no idea how to do this, since I'm not very familiar with Nginx - any one has some clues?
Read the documentation, eveything is well explained in it. There's particularly a beginner's guide explaining basics. You would end up with :
upstream backend {
least_conn;
server 10.128.1.4;
server 10.128.1.5;
}
server {
server_name _;
location / {
proxy_set_header Host $host;
proxy_pass http://backend;
}
}
server {
server_name acme.com;
location /admin/ {
proxy_set_header Host $host;
proxy_pass http://10.128.1.2;
}
location /category/ {
proxy_set_header Host $host;
proxy_pass http://10.128.1.1;
}
location / {
proxy_set_header Host $host;
proxy_pass http://backend;
}
}
server {
server_name api.acme.com;
location / {
proxy_set_header Host $host;
proxy_pass http://10.128.1.3;
}
}
You will also need to rewrite the URL otherwise /whatever/ will get forwarded to the backend server
location /admin/ {
rewrite ^/admin^/ /$1 break;
proxy_pass http://10.128.1.2;
}

Resources