route different proxy based on subdomain request in nginx - nginx

I have one dedicated server in that server I deployed 5 nodejs application.
domain name: www.nnd.com
dedicated server ip: xxx.xx.x.60
I had domain which is pointed to my dedicated server ip.
sub domains are :
app1.nnd.com pointed to xxx.xx.x.60
app2.nnd.com pointed to xxx.xx.x.60
app3.nnd.com pointed to xxx.xx.x.60
app4.nnd.com pointed to xxx.xx.x.60
app5.nnd.com pointed to xxx.xx.x.60
now in nginx configuration file based on the subdomain I need to route proxy.
Example:
{
listen:80;
server_name:xxx.xx.x.60
location / {
#here based on subdomain of the request I need to create proxy_pass for my node application
}
}
Is there any condition and how can I get the original domain name from proxy header?

create a virtual host for each
server {
server_name sub1.example.com;
location / {
proxy_pass http://127.0.0.1:xxxx;
}
}
server {
server_name sub2.example.com;
location / {
proxy_pass http://127.0.0.1:xxxx;
}
}
And go on, change the port number to match the right port.

You can use RegExp to fetch host name like this
server {
server_name ~^(www\.)?(?<domain>.+)$;
location / {
root /sites/$domain;
}
}

You can create virtual host for every sub domain.
For Ex you have 2 sub domain abc.xyz.com and abcd.xyz.com , and you want to host it on nginx single instance by proxy_pass then you can simply create virtual host for every sub domain
server {
server_name abc.xyz.com;
location / {
proxy_pass http://127.0.0.1:8000;
}
}
server {
server_name abcd.xyz.com;
location / {
proxy_pass http://127.0.0.1:8000;
}
}
For more information you can refer here

Related

Can NGINX choose proxy_pass backend based on IP?

I have a situation where we have multiple test environments. Each environment needs to access different versions of a service and we have an NGINX proxy that sits in-front of these different services. Currently we're using multiple servers to do the proxy. Is there a way I can use NGINX allow or deny to filter which backend the environments connect to based on remote IP?
The v1 environment has IP addresses in the 10.0.1.0/24 range, and v2 is only connected to by IP's in 10.0.2.0/24.
Current Config
Simplified for brevity.
server {
listen 80;
server_name service.v1.net;
proxy_pass http://10.0.10.56:8081;
}
server {
listen 80;
server_name service.v2.net;
proxy_pass http://10.0.10.56:8082;
}
What I've tried
Clearly this doesn't work.
server {
listen 80;
server_name service.net;
location / {
# v1 proxy
allow 10.0.1.0/24;
deny all;
proxy_pass http://10.0.10.56:8081;
}
location / {
# v2 proxy
allow 10.0.2.0/24;
deny all;
proxy_pass http://10.0.10.56:8082;
}
}
Also Note...
I know this can be done with serving a proxy on different ports and iptables rules - I'm trying to figure out if NGINX can do this by itself.
You can use ngx_http_geo_module for that. (Which should just work out of the box). It sets variables depending on the client IP address, which can then be used in an if.
geo $environment {
10.0.1.0/24 v1;
10.0.2.0/24 v2;
}
server {
listen 80;
server_name service.net;
location / {
if ($environment = v1) {
proxy_pass http://10.0.10.56:8081;
}
if ($environment = v2) {
proxy_pass http://10.0.10.56:8082;
}
}
}
All other IPs will see a 404 in this case.
Although this works, be advised that using if within a location block can be very tricky: http://wiki.nginx.org/IfIsEvil

How to change the path prefix of RabbitMQ using nginx

When we enable the RabbitMQ Web Management Console then it is accessible by http://ip:15672.
Instead of this i want to access RabbitMQ managment console by this http://ip/rabitmq by using nginx
I have done changes in the nginx.
server {
listen 80;
server_name _;
location /rabbitmq {
proxy_pass http://localhost:15672;
}
}
but i dont know how to configure path prefix in rabbitnq ?
I suggest you read:
https://github.com/docker-library/rabbitmq/issues/249
In short, you can configure this in the /etc/rabbitmq/rabbitmq.conf by adding the following line:
management.path_prefix = /rabbitmq
If you are running in a docker environment, you will need to modify:
location /rabbitmq {
proxy_pass http://localhost:15672;
}
to
location /rabbitmq {
proxy_pass http://CONTAINERS_HOSTNAME:15672;
}

nginx reverse proxy between 2 https servers

I'm a bit new to using nginx so I'm likely missing something obvious. I'm trying to create an nginx server that will reverse proxy to a set of web servers that use https.
I've been able to get it to work with one server list this:
server {
listen $PORT;
server_name <nginx server>.herokuapp.com;
location / {
proxy_pass https://<server1>.herokuapp.com;
}
}
However, as soon I try to add in the 'upstream' configuration element it no longer works.
upstream backend {
server <server1>.herokuapp.com;
}
server {
listen $PORT;
server_name <nginx server>.herokuapp.com;
location / {
proxy_pass https://backend;
}
}
I've tried adding in 443, but that also fails.
upstream backend {
server <server1>.herokuapp.com:443;
}
server {
listen $PORT;
server_name <nginx server>.herokuapp.com;
location / {
proxy_pass https://backend;
}
}
Any ideas what I'm doing wrong here?

nginx proxy_pass application with prefix

I need to serve multiple instances of same application for different users.
Say I have users as user1, user2 and user3. My nginx.conf will be like below.
server {
listen 80;
server_name localhost;
location /user1/ {
proxy_pass http://myapp1;
}
location /user2/ {
proxy_pass http://myapp2;
}
location /user3/ {
proxy_pass http://myapp3;
}
}
The application will redirect user back and forth several times. The userX prefix is lost at first proxy pass and next calls are sent to /.
I am using nginx inside a docker container and already read and tried below.
I simply followed a workaround as below to get done what I needed.
upstream user1 {
server myapp1;
}
upstream user2 {
server myapp2;
}
upstream user3 {
server myapp3;
}
server {
listen 80;
server_name localhost;
location / {
//Used a lua script to identify the user
proxy_pass http://$userX;
}
}

How can i pass subdomain as proxy_pass value in nginx?

i am currently in situation where i need to get/catch sub-domain and pass that sub-domain value to proxy_pass in Nginx config.
e.g.
if user enters
http://google.com.mydomain.com
then it should do proxy pass as
proxy_pass http://www.google.com/;
in above example google.com is sub-domain
can this be doable ?
How can i achieve something like that in nginx ?
currently i am using configuration where sub-domain values are hard-coded in config files , but there are many sub-domains , so i need to do it like this way, but don't know correct syntax.
server {
listen 80;
server_name subdomain.domain.com;
charset utf-8;
location / {
proxy_pass http://www.subdomain/;
}
}
I am using * as A record to redirect all sub-domains to my web-host, i.e. wildcard DNS.
update:
i have found code snippet from https://stackoverflow.com/a/22774520/1642018
server {
listen 80;
# this matches every subdomain of domain.
server_name .domain.com;
location / {
set $subdomain "";
if ($host ~* "^(.+)\.domain.com$") {
set $subdomain $1;
}
proxy_pass http://$subdomain;
}
}
but the request is showing my default page which is in my default web root.
Two things.
1- A resolver (a dns server for your nginx in order to resolve google.com, you may add at your hosts or you are able to add resolver statement)
2- You will need to resolve how your client will deals with different domains, I mean google.com is different than google.com.ar or google.fr)
At this example I made work it for your example google.com
worker_processes 4;
error_log /var/log/nginx/error.log;
events {
worker_connections 1024;
}
http {
server {
listen 80;
location / {
set $subdomain "";
if ($host ~* "^(.+)\.domain.com$") {
set $subdomain $1;
}
resolver 8.8.8.8;
proxy_pass "http://$subdomain.com";
}
}
}
I hope that this configuration helps to you.
I would capture the subdomain using a map then proxy pass if the variable is defined:
map $host $subdomain {
~^(?<sub>.+)\.[^\.]+\.[^\.]+$ $sub;
}
server {
listen 80 default_server;
server_name _;
location / {
if ($subdomain) {
proxy_pass http://$subdomain;
}
}
}

Resources