one sub-domain name for multiple services - nginx

I am using nginx proxy server and i don't want to use another sub-domain.
is it possible to redirect one domain name to multiple servers.
eg: my register domain name: user.example.com
and my app servers are : 192.168.0.1:7000 and 192.168.0.2:8000
i and looking to do is when i hit user.example.com redirects to 192.168.0.1:7000 and when i hit user.example.com/1 this will redirect to 192.168.0.2:8000

Yes, such a configuration might be implemented using NGINX as reverse proxy, it is described very well in official documentation, e.g. in "NGINX reverse proxy" guide. Basically it is just
location /some/path/ {
proxy_pass 192.168.0.1:7000;
}
location /another/path/ {
proxy_pass 192.168.0.2:8000;
}

Related

Nginx proxy_pass to change root domain only behind ALB

I need to make a domain migration for an app hosted on ElasticBeanstalk behind ALB. This app uses various subdomains so I need to keep the subdomain and the request and change only the domain... I tried to setup a Nginx proxy_pass directive but I might have made mistakes as it does not work:
Practically what I want to achieve is a proxy for every type of request (post, get, put, delete...) that would redirect requests like anySubdomain.potato.net/anything/else to anySubdomain.carrot.io/anything/else
On the DNS side both wildcard subdomains *.potato.net and *.carrot.io are pointing to the same ElasticBeanstalk environment. So far I added this instruction into my Nginx configuration files, but it does not work as the potato.net domain is not redirected and lands directly on the hosted app without beeing proxied:
location ~ ^/(?<subdomain>\.potato\.net)/(?<path>.*)?$ {
# this is the desired directive at the end:
# proxy_pass https://$subdomain.carrot.io/$path;
# for test purposes I add the debug path below to output
# some info and make sure it was proxied:
proxy_pass https://$subdomain.carrot.io/proxydebug/$path;
}
Is there something wrong in that directive?

How can I add a query parameter to proxy_pass using Nginx Proxy Manager

I'm trying to use Nginx Proxy Manager to add a query parameter to a path before passing it to a server using proxy_pass. The problem I'm having is that the query parameter doesn't seem to be added at all.
This is what I have added under "Custom Nginx Configuration":
location ~ /(sv|en)(.*) {
proxy_pass http://192.168.1.2:3000$2?lang=$1;
}
If I change proxy_pass to return 307, just to test if the regex is correct, I see the expected url in the browser. (for example ?lang=en) is added.
Based on what I have found online, this should work in regular nginx, but I have also found that Nginx Proxy Manager uses openresty. Could that cause any problems? Should I write it in some other way?

How to set exceptions for NGINX load balancer

Is it possible to configure NGINX loadbalancer in least_conn mode to make exception for certain paths?
I want to configure loadbalancer in such way that all requests required for single login operation are sent to the same backend application instance.
I have frontend app accessing duplicated backend app via nginx load balancer. All apps are deployed on Tomcat 8.5 and backend instances have configured session replication between Tomcats.
My problem is that when user is authenticated using OAuth-2.0 authorization_code grant method, frontend app gets authorization code but due to conneting to backend through load balancer it tries to obtain token using this code from another machine resulting in InvalidGrantException.
Using ip_hash mode or it's variations isn't solution for this problem as it is unstable when application is accessed through VPN.
Yes you can achieve what you want by declaring two locations and treat them differently. See example below and check this question where it explains how the priority works.
http {
upstream myapp1 {
least_conn;
server srv1.example.com;
server srv2.example.com;
server srv3.example.com;
}
server {
listen 80;
location / {
proxy_pass http://myapp1;
}
location /my-special-path/ {
proxy_pass http://srv1.example.com;
}
}
}
Above is a solution mainly based in your first statement that you want routing based on certain paths. If your problem is more complicated i.e these paths are dynamically created etc you can share an example to be easier to understand your specific situation.
UPDATE
Based on comment. I would really suggest to go troubleshoot your backend in order to be synced. That being said if you really want a solution for the exact problem from your nginx I would do the following:
On every response I would add a specific header which specific backend answered this request. add_header X-Upstream $upstream_addr;
On this specific path I would serve the request based on the value of that header. proxy_pass http://$http_x_upstream;
So the config would look like this:
http {
...
server {
...
location / {
add_header X-Upstream $upstream_addr always;
proxy_pass http://myapp1;
}
location /authorize/ {
add_header X-Upstream $upstream_addr always;
proxy_pass http://$http_x_upstream;
}
}
}
NOTE: Security. If you go down this path be careful that you are routing your requests based on a value that your client can manipulate. So be sure that you are at least validating this value. Check this answer for validating headers with nginx.

How to make nginx redirect to a different port

I have the following location block in the nginx.conf file.
Location /e-ui/ {
proxy_pass https://hostname:9000
}
I need to know how I can redirect sites in the following manner:
https://abc/e-ui/ should be redirected to https://abc:9000
OR
https://xyz/e-ui/ should be redirected to https://xyz:9000
The server name is changing ( as the code is deployed on many servers ). Is there anyway I can get the server name from the /etc/hosts file if it is already defined there and use it somehow to redirect the sites as mentioned above ?
Thank you.

Edit a header value in nginx

Background
So I've got a server running a tomcat application hidden behind an Apache proxy. The proxy provides a more user friendly url as well as SSL encryption with automatic redirects so that the app is only accessible on https.
I'm busy migrating this to an nginx proxy.
One of the issues I've had is that upon login, my app sends back a "LocationAfterLogon" header in the http response in the form of
http://192.168.x.x:8080/myapp/index.jsp.
That IP address returned is from the proxied server not visible on the internet. So then the browser gets a connection error trying to navigate to it.
As a workaround, I've used nginx directives:
proxy_hide_header: to hide the LocationAfterLogin header coming back from the proxied server
add_header: to add a new LocationAfterLogin url.
So my config looks as follows
#header for location after logon of demo app
add_header LocationAfterLogon http://example.com/demo/index.jsp;
#hide the real LocationAfterLogon
proxy_hide_header LocationAfterLogon;
The Problem
I need to be able to do a regex replace or similar on LocationAfterLogon because it won't always be to index.jsp, depending on which url was intercepted by the login page.
I am aware that I can also rewrite the tomcat app to send back a relative URL instead, but I'd like to do it all in nginx config.
I've also read about nginx more_set_headers. Haven't tried it yet. Does it allow me to edit the headers?
Apache has the Header edit directive which I was using previously, so I'm looking for something like that.
TL;DR
Is is possible to edit a header location using regex replace or similar in Nginx?
You can use the map directive to rewrite your header:
map $upstream_http_locationafterlogon $new_location {
~regexp new_value;
}
proxy_hide_header LocationAfterLogon;
add_header LocationAfterLogon $new_location;
See the documentation: http://nginx.org/en/docs/http/ngx_http_map_module.html

Resources