How to reverse proxy app in a subdirectory? - nginx

I'd like to reverse proxy my application under server.com/myapp/homem where server.com/myapp is the proxied address and /home is an URL handled by my app.
What is the best / recommended solution to do that so that the app will generate proper paths<img src="/myapp/static/...">
Which of the two following approaches I should take:
1. X-Forwarded-Path: I proxy GET Path=/home and add an X-Forwarded-Path: /myapp
2. Base_url set in config: I proxy GET Path=/myapp/home and set base_url = server.com/myapp

The nginx rewrite directive can handle what you are looking for
You server block should look similar to the following snippet
server {
listen 80;
rewrite ^/myapp/(.*)$ /$1 last;
location /home {
proxy_set_header X-Forwarded-Path myapp;
proxy_pass http://1.2.3.4;
}
}
Take a look at either of the following for more information
https://www.nginx.com/blog/creating-nginx-rewrite-rules/
http://nginx.org/en/docs/http/ngx_http_rewrite_module.html

Related

nginx - route to application via path

I want to use nginx to route to different application via path. I use the following nginx config (minimal example):
server {
listen 80;
http://example.com;
location /myapp/ {
rewrite ^/myapp/(.*)$ /$1 break;
proxy_pass http://localhost:port;
}
}
The request is working but some application do route back the response to the root. Instead of
http://example.com/myapp/*
I end up again on
http://example.com/*
Is there a way, that nginx does route all request from /myapp/* to http://localhost:port/* (application root) and route back the response to /myapp/*
So far I didn't find a solution.
Thanks

Nginx How do i route to reverse proxy by location

Currently i'm using nginx server and using nodejs server as reverse proxy.
I want to make the nginx server proxy_pass different server by location.
So, lets say my domain is subdomain.domain.com.
When loading subdomain.domain.com/, this should serve static files.
When loading subdomain.domain.com/example1/req1/req2, this should route to 127.0.0.1:3000/req1/req2.
When loading subdomain.domain.com/example2/req1/req2, this should route to 127.0.0.1:8080/req1/req2.
But my configuration routes subdomain.domain.com/example1/req1/req2 to 127.0.0.1:3000/example1/req1/req2 resulting error. (nodejs returns Cannot GET /example1)
How should I write this nginx conf file properly?
Try use rewrite directive in location block.
Something like:
location /example1/ {
rewrite ^/example1/(.*)$ /$1 break;
proxy_pass http://xxxxxx;
}
You can check documents related to rewrite module at http://nginx.org/en/docs/http/ngx_http_rewrite_module.html
You need to add below code snippet to your nginx.conf.
server subdomain.domain.com;
location / {
rewrite ^/example1/(.*)$ /$1 break;
proxy_pass http://127.0.0.1:3000;
}
}

NGINX reverse proxy to a host with no root subdirectory

Usually, when doing a reverse proxy setup, you have a server on your backend which looks like this:
http://localhost:8084/app-root
and you can proxy_pass
location /app-root {
proxy_pass http://localhost:8084;
}
It will proxy www.my-domain.com/app-root to the internal server http://localhost:8084/app-root.
Great!
Can someone explain what needs to be done if the server insists on hosting from the root as so:
http://localhost:8084/index.html
http://localhost:8084/images/image1.jpg
I want this to be accessible via
http://www.my-domain.com/app/index.html
http://www.my-domain.com/app/images/image1.jpg
You can use rewrite of nginx. Something like this should work
location /app/ {
rewrite /app/(.*) /$1 break;
proxy_pass http://localhost:8084;
}

Nginx: how to add /something to a uri and still keep it working

I have a nginx instance running. My config is something like the following.
server {
listen 80;
listen 443;
location / {
...
proxy_pass http://127.0.0.1:8080;
...
proxy_redirect http://127.0.0.1:8080 example.com;
}
}
I have some software running in 8080 and I want that the user enters example.com/somepath and be able to be redirected to the root 127.0.0.1:8080 through my domain. The software should receive all urls without /somepath but the browser should still show /somepath in the name.
I am quite new so sorry for the basic question I could not find any relevant info on how to do this exactly: I tried rewrite rules and setting location /mysoftware { tests with no luck.
The client browser uses /somepath/... to access /...in the application. This means that nginx must rewrite the URI before passing it upstream.
The proxy_pass directive has a basic rewrite capability. See this document for details. For example:
location /somepath/ {
proxy_pass http://127.0.0.1:8080/;
...
}
Alternatively, you might use a rewrite ... break statement. See this document for details. For example:
location /somepath {
rewrite ^/somepath/?(.*)$ /$1 break;
proxy_pass http://127.0.0.1:8080;
...
}
The difficult part is preventing your application from breaking out of /somepath. The proxy_redirect directive can handle the 3xx responses from your application. But the location of resource files (.css and .js) and the target for hyperlinks, can cause problems for applications that are not aware that they need to stay inside a subdirectory.

Nginx proxy redirect without changing url

I have a nginx (:80) and an upstream server (:8080) running on my machine.
I want to proxy all requests to /assets/(*.?) to upstream's /upstream/$1 location.
The upstream server redirects (302) /upstream/file_id to the /real/file/location.ext
Here is my code:
location /assets/ {
rewrite ^/assets/(.*) /upstream/$1 break;
proxy_pass http://127.0.0.1:8000;
}
This seems to work, but on the client side I get the redirected location:
http://myserver.com/real/file/location.ext
I kinda want to hide it so that it stays:
http://myserver.com/assets/file_id
The idea behind this is to make the upstream server find the real file's location, but let the nginx serve the file without giving away its real location. Is this even possible?
first you're using 8000 in proxy_pass, but you're mentioning your port is 8080.
Second, remove the rewrite line should do the trick, because youre actually using the rewrite rule here and never get to the proxy_pass line. Something like the following should work:
location /assets/ {
include proxy_params;
proxy_pass http://127.0.0.1:8080;
}
There are also proxy_rewrite and proxy_redirect commands which might help you in getting this upstream-redirect handled internally by nginx.
Hope that helps!

Resources