NGINX: proxy_pass for a dynamic url - nginx

I have a docker container which serves a web application on:
my.server.domain:8080
When I request that URL with a browser, it is automatically redirected to the login page:
http://my.server.domain:8080/login
I'm trying to proxy the app so that I can avoid to use the port number.
Two possible ways to achieve that are:
1) http://my.server.domain/appname
2) http://appname.my.server.domain
Either way, will work for me.
But I'm struggling with making the right NGINX configuration.
I tried with:
location /appname {
proxy_pass http://my.server.domain:8080/;
}
But when I load http://my.server.domain/appname it gets redirected to http://my.server.domain/login which doesn't exist.
If I use / it works as expected:
location / {
proxy_pass http://my.server.domain:8080/;
}
But that's not what I need.
I can add more directives like:
location /login {
proxy_pass http://my.server.domain:8080/login;
}
But then it will fail on the next redirect and so on ..

Related

nginx reverse to all pages

I have the following configuration that works for only 1 page:
location /mypage.html/ {
proxy_pass http://${remote_server}/;
}
When I am trying to navigate to other pages on the remote server, I get page not found.
Is there any way to keep the reverse proxy open for all pages on the remote server?
This will match everything, technically everything that starts /, and route the same request to your remote server:
location / {
proxy_pass http://${remote_server}/;
}

Redirect me to :[port]/[whatever I type]

I have an app running on localhost:8080 , I configured Nginix to make it run on localhost/
And I have another app running on localhost:3000
What I want to do is to redirect me to localhost:3000/[whatever] when I originally go to localhost/[whatever]
I wanna do something similar to this:
location /[SOMETHING] {
proxy_pass http://localhost:3000/[THAT_SAME_THING_ABOVE];
}
Is it possible to configure Nginx to do this behavior? if so how?
Thanks
PS: The app running on :8080 is a single page so :8080/[whatever] doesn't even exist now
This is the solution, just in case somebody needs it
location = / {
proxy_pass http://localhost:8080
}
location / {
proxy_pass http://localhost:3000
}

Routing in nginx URI with same prefix as another URI

Currently trying to use Nginx as a proxy and finding some difficulties with routes having another URI as a prefix. Example :
location /api/route/to/specific/application {
proxy_pass http://proxying.to/site/A;
}
location /api {
proxy_pass http://proxying.to/another/site/B;
}
When visiting http://mysite.io/api/route/to/specific/application, I get a 404 from Nginx while when I visit http://proxying.to/site/A directly I get the expected page.
Any idea how I can make it work ?
Thanks

With nginx proxy/rewrite can I keep the original URL in the browser's Location field?

Using nginx.conf features like proxy-pass/rewrite, can I keep the original URL in the browser's Location field?
I have several PlayFramework apps running on different ports (9001, 9002, ...) with proxy forwarding set up via nginx.conf. People browse to them as:
http://domain.name/App1/
http://domain.name/App2/
etc.
My nginx.conf entries look like this:
location /App1/ {
proxy_pass http://localhost:9001/;
rewrite ^/App1/(.*) http://domain.name:9001/$1;
}
If I ask for http://domain.name/App1/, what I see in the browser's Location field is http://domain.name:9001. What I wish I saw was http://domain.name/App1/, that is, I want the name App1 to remain in the URI, and I'd rather not expose the port number.
Let's say App1 has a link /location/ABC. When I click on it I see http://domain.name:9001/location/ABC when I wish I saw http://domain.name/App1/location/ABC.
Can I achieve this with nginx.conf?
P.S. I put http://domain.name explicitly in the rewrite rule because without it I was getting localhost in the browser, and my browser's localhost is not the same as the server's.
Rewrites issue redirects for browser.
If you just want to mount several locations from upstreams - you do not need rewrites, just use:
location /App1/ {
proxy_pass http://localhost:9001/;
}
But apps should use relative links or account for their absolute location.
For more complex url manipulation you can use break-rewrites:
location /App1/ {
rewrite ^/App1/(.*) /$1 break;
proxy_pass http://localhost:9001;
}

NGINX Location troubles

I have django and flask applications running on the same machine through different ports:
Django runs on server:8088
Flask runs on server:666
In NGINX.conf I have the following code:
location / {
proxy_pass http://127.0.0.1:8088;
}
location ^/server2 {
proxy_pass http://127.0.0.1:666;
}
Django has been running for over a year successfully with this set up, where as flask is a new addition. Any time I try to access one of the Flask urls I either get a "this url does not exist on this server" error, or on occasion a 500 error (when i've been fiddling).
If I write location information for a specific flask url like this:
location /server2/splash {
proxy_pass http://127.0.0.1:666/splash;
}
It works, but I obviously don't want to write individual location information for each and every URL in the flask application.
I've gone through many of the existing Nginx location posts on stackoverflow but I've not been able to get it working. Any ideas?
Thanks!
EDIT
this is an example of what I'm trying to achieve, but rather than an individual mapping for each URL, I want a single mapping that covers all URLs:
location /server2{
proxy_pass http://127.0.0.1:666/splash;
}
location /server2/split {
proxy_pass http://127.0.0.1:666/split;
}
location /server2/export {
proxy_pass http://127.0.0.1:666/export;
}
location /server2/import {
proxy_pass http://127.0.0.1:666/import;
}
Why do you use the ^ sign? Just remove it I think it will work:
location /server2 {
proxy_pass http://127.0.0.1:666;
}
Note that when you use location /server2 the server2 is still being passed to your flask application.
In this case Nginx is doing the following:
server.com/server2 => http://127.0.0.1:666/server2
server.com/server2/splash => http://127.0.0.1:666/server2/splash
In this case location is not doing a rewrite. Always check /var/log/nginx (or wherever your logs are located) to check the requests done by the browser and what Nginx looks for after the rules for your site are processed.
What you probably want is to set an upstream directive:
upstream flask_server {
server 127.0.0.1:666;
}
server {
...
location /server2 {
proxy_pass http://flask_server;
}
}

Resources