Nginx How do i route to reverse proxy by location - nginx

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

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

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

Correct proxy path in nginx.conf

we have two servers, A and B. Server A is accessed worldwide. He has nginx installed. That's what I have in conf:
location /test {
proxy_pass http://localserver.com;
}
What it should do, is translate the addreess http://globalserver.com/test (that is server A) to internal server address http://localserver.com. However, it does append the location path, that is, itries to look for http://localserver.com/test, which is not available at all. How can I make the proxy pass to the correct address, throwing out the last part in the location?
That should work. Nginx should strip the '/test' path on the upstream local server. So what I can say is that is not the cause. To make it a bit better, try this:
location /test/ {
proxy_pass http://localserver.com/;
}
The 2 slashes I added at the first 2 lines will avoid mistakenly match '/testABC' and send the wrong request to the upstream local server, for example.
Do you have a
proxy_redirect
line in the same location block? If your upstream local server has redirects, then a mistake on that line will cause an issue like you described.
[UPDATE] Found the root cause why the original config didn't work and mine works: nginx does NOT replace URI path part if the proxy_pass directive does not have a URI path itself. So my fix of adding a slash (slash is treated as a URI path) at the end triggers the URI path replacement.
Reference: http://wiki.nginx.org/HttpProxyModule#proxy_pass
If it is necessary to transmit URI in the unprocessed form then directive proxy_pass should be used without URI part
location /some/path/ {
proxy_pass http://127.0.0.1;
}
try to add as specified here http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass:
proxy_pass http://localserver.com/;
try rewrite
location /test {
rewrite ^ $scheme://$host/;
proxy_pass http://localserver.com;
}
some helpful links...
http://nginx.org/en/docs/http/ngx_http_rewrite_module.html#rewrite
http://wiki.nginx.org/Pitfalls

Resources