NGINX reverse proxy to a host with no root subdirectory - nginx

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

Related

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

How to reverse proxy app in a subdirectory?

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

Nginx microservice gateway config within kubernetes cluster

Basically we have a set of microservices we have deployed to a kubernetes cluster hosted in AWS. We would like to run these through a gateway configuration in nginx.
Our current configuration which doesn't work looks something like this-
upstream some-api1 {
server some-api1:80;
}
upstream some-api2 {
server some-api2:80;
}
upstream some-api3 {
server some-api3:80;
}
server {
listen 80;
server_name gateway.something.com;
location /api1 {
proxy_pass http://some-api1;
}
location /api2 {
proxy_pass http://some-api2;
}
location /api3 {
proxy_pass http://some-api3;
}
}
Our services have been built with dotnet core, so the underlying urls would be something like http://some-api1/{api/controllername} . I'm always getting a 404 when I try hitting these endpoints through postman, which tells me it can't resolve these mappings.
However I'm able to access an api within the cluster using an explicit config for an api like so(which is what I don't want to do)-
server {
listen 80;
server_name someapi1.something.com;
location /{
proxy_pass http://some-api1;
}
}..
If someone could shed some light on what's wrong with the configuration or recommend the best approach for this it would be greatly appreciated.
As #Luminance suggests, you're seeing traffic to /api1 go to some-api1/api1 instead of just some-api1 on the base path for that target service (which is what your app would respond to). Following https://gist.github.com/soheilhy/8b94347ff8336d971ad0 you could re-write that target like
location /api1 {
rewrite ^/api1(.*) /$1 break;
proxy_pass http://some-api1;
}

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