nginx reverse to all pages - nginx

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}/;
}

Related

Nginx | Reverse proxy

I have a Nextjs application running on https://localhost port 3000 on my server and it is accessable through https://rgb.irpsc.com:3000/citizen/hm-2000001. The hm-2000001 part is dynamic and can range from hm-2000000 to any value.
What I want to do is to make this application accessable without specifying a port. What I mean is that when a user types https://rgb.irpsc.com/citizen/hm-2000003 in the browser address bar, the related page shows up.
I have configured this in nginx, but it seems to be not working. I'd be so grateful for any help from you guys.
Here's my nginx configuration:
location /citizen {
proxy_pass https://localhost:3000/citizen;
}
You should change your config as below:
location /citizen {
proxy_pass http://localhost:3000;
}

Can a response from an http request alter the base address in the next client request?

I have an octoprint server running at http://192.168.1.205. I also have an nginx server hosting myDomain. I want to be able to use the nginx server to pass on a request for http://myDomain/octo to http://192.168.1.205 using a reverse proxy. Here is my nginx code...
server {
server_name myDomain;
location /octo/ {
rewrite ^/octo/(.*) /$1 break; #strip /octo from url
proxy_pass http://192.168.1.205;
}
}
The first http://myDomain/octo request is passed on to http://192.168.1.205 correctly. But after the first response the code in the client makes another request to http://myDomain/moreUri. Since this uri doesn't have /octo nginx doesn't know to send it to http://192.168.1.205/moreUri. Is there a way to have nginx change something in the first response so that the client then makes following requests to http://myDomain/octo/moreUri?
I was able to accomplish this for a case where the octoprint server responded with a redirect. I used ...
proxy_redirect http://192.168.1.205/ http://myDomain/octo/;
and it worked. But that only works on redirects and the following requests were wrong again.
Is there a way to have nginx change something in the first response so
that the client then makes following requests to
http://myDomain/octo/moreUri?
I am not aware that this is possible.
What about to adjust the nginx configuration accordingly ? using location / to process all requests within that block and add an additional redirect directive to address the "Since this uri doesn't have /octo nginx doesn't know to send it to http://192.168.1.205/moreUri" should do the trick.
server {
server_name myDomain;
location / {
rewrite ^/octo/(.*) /$1 break; #strip /octo from url
rewrite ^/(.*)/(.*) /octo/$2 break; #rewrite /moreURI to /octo/moreURI
proxy_pass http://192.168.1.205;
}
}
No matter if the above nginx reconfiguration fixes your issue, i assume the root cause why you need to configure the nginx as reverse proxy in this way might be a misconfigured (or not optimally configured) application. Check the config file if it is possible to configure the applications base path. If so, set it to /octo/ (so the application itself prepends /octo/ to all the links it presents to the user and all requests to the backend automatically) and adjust the rewrite rules accordingly.

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
}

NGINX: proxy_pass for a dynamic url

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 ..

Nginx vhosts per path

How do I setup nginx conf so that "blah.com/website1" and "blah.com/website2" etc can point to separate websites that are running on the machine (vhost?)?
E.g. localhost:3000 is website1 and localhost:3001 is website2.
So when a user points their browser at blah.com/website1 they access the website localhost:3000 and localhost:3001 for website2?
I've tried a reverse proxy but that can't map paths like "blah.com/website1" and only seems to handle the server name of "blah.com".
Update:
So I've tried a config like this to use reverse proxy, but all assests on the website are not loading as they are trying to load from the domain's root. Probably because it's not a vhost? The page loads but assets are trying to load and 404ing, e.g: http://blah.com/static/css/styles.css
server {
listen 80;
server_name blah.com;
location /website1/ {
proxy_pass http://localhost:3001/;
}
}

Resources