NGINX - How to Serve multiple applications in same base URL - SPA - nginx

I have two locations in my NGINX setup :
location /APP_B
location /
One for application A (/) and one for Application B. I was able to properly set up so the different subdirectories in the URL would get a proper response, however, I still get a behavior where the browser tries to download all bundle files that my application has from the / directory.
Is there any way in nginx app conf that I can tell that all requests to static files are mapped to a new URL?
Gladly appreciate any advice. Thanks.

Yes, you can do this. You need to use a location block. I'm not sure what your URLs are, however you should end up with something like this:
location /APP_B/static/ {
proxy_pass http://127.0.0.1:1111/static;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-for $remote_addr;
}
location /static/ {
proxy_pass http://127.0.0.1:2222/static;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-for $remote_addr;
}

Related

How to enforce a rewrite or redirect for an nginx reverse proxyed location

I am trying to use Nginx to reverse proxy a node.js app on a sub-directory of my main website which is also reversed proxied.
The / reverse proxy works as expected however the second location does not load any assets...
I am using the below config:
location / {
proxy_pass http://192.168.1.104:3000/;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /new/ {
proxy_pass http://192.168.1.65:3000/;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
This works in as much as when I go to the ./new url I can see the default page of the node.js app but no images, css or js assets are loaded.
When I check the browser console I can see loading errors that show that the browser is trying to load the assets from the / location...
Also the links shown on the page loaded at ./new also point to the / location so they also do not work...
I have been trying to work out how to resolve this for days and have seen that rewrite or return commands in the nginx.conf file can be used to redirect the browser but none of these seem to work to correct my issue... or I may be mis-understanding how to use them as I see a 'too many redirects' error page when trying to load from the ./new url...
I would really appreciate any pointers on how I can resolve this.
Thanks.
You need to modify your app to prepend its root path to urls, nginx does not alter proxied content.

nginx reverse proxy and paths

This is the issue I'm having with this application that we cannot modify:
Example:
I'm trying to set this app so that it goes to:
reverseproxy.com/app1/ which should redirect to:
reverseproxy.com/app1/index.html
However, when the app does a redirect it goes
reverseproxy.com/index.html which throws a 404. (note its missing "app1" in the path)
I've been having a hard time finding a clear answer on this. I assume that it must be something I am missing, here is my current conf:
location /app1 {
proxy_pass https://applicationserver.com/;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
Any help would be appreciated.
Thanks
Jon

nginx reverse proxy images and css are not loaded

I try to configure an nginx reverse proxy to access a Jenkins instance. I can open the authentication page but there is no CSS and no image. It works perfectly when direct access.
All works as if the reverse proxy does not rewrite correctly URLs defined in the html source page. Have I missed something ?
Here is my nginx configuration :
location /jenkins {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect false;
proxy_pass http://jenkins:8080/;
}
I found the solution. The nginx reverse proxy works well but Jenkins need some customization to work with reverse proxy.
The final nginx configuration :
location /jenkins/ {
proxy_pass http://jenkins:8080/jenkins/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
And the tutorial to configure jenkins behind nginx reverse proxy which solved my problem
I don't know if above statement worked for OP but I know that altering location name line did the trick for me:
location ^~ /jenkins/ {
proxy_pass http://jenkins:8080/jenkins/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
If you use the Jenkins with docker. You can add the environment part of compose file as below:
environment:
JENKINS_OPTS: "--prefix=/jenkins"
in the nginx conf file. proxy_pass must refer to the http://IP-ADDRESS:PORT/jenkins/. As mentioned before, the link as a reference is very usefull.

Jenkins behind nginx without subdirectory

I have Jenkins running inside my Glassfish installation, so Jenkins can be reached #
http://localhost:8090/jenkins/
I managed to setup nginx so Jenkins can be reached from the outside #
http://build.example.com/jenkins/
This setup works well so far, but I am not really happy with it. What I would really want to achieve is to hit
http://build.example.com
in the browser to reach Jenkins.
Here is my current nginx config:
server {
listen 80;
server_name build.example.com;
location / {
proxy_pass http://localhost:8090;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
I hope this is possible via some url rewrite, but I'm totally clueless how to do it...
Then change:
proxy_pass http://localhost:8090;
to
proxy_pass http://localhost:8090/jenkins/;
Reference: http://nginx.org/r/proxy_pass
It seems to me the problem is the Glassfish configuration.
How about setting in application.xml the following value:
<context-root/>
Instead of the default, which is the name of the WAR file, without the .war extension.
There seems to be similar questions on SO.
From http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass
location / {
rewrite /jenkins/(.*) /$1 break;
proxy_pass http://localhost:8090/jenkins/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

Conditional Location routing - nginx

Issue:
I have two applications served by one nginx 1.5.5 webserver on one domain, www.domain.com.
Because they both listen on the same locations I added a header of APP = ONE or TWO which I set when the application is first called. Example below.
location ^/APPONE {
proxy_pass http://APPONE;
add_header APP ONE;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location ^/APPTWO {
proxy_pass http://APPTWO;
add_header APP TWO;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
Due to poor application design, the applications both request the below locations not using the application name as a docroot. Like the example below.
location ~ ^/(framerwork1/|framerwork2/|framerwork3/) {
proxy_pass http://APPONE;
add_header APP ONE;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location ~ ^/(framerwork1/|framerwork2/|framerwork3/) {
proxy_pass http://APPTWO;
add_header APP TWO;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
So, I added the APP ONE or TWO header to separate the two, but my if statements to proxy them based on the header is not being accepted by Nginx. Example Below.
if ($http_host ~ 'APP=ONE') {
location ~ ^/(framerwork1/|framerwork2/|framerwork3/) {
proxy_pass http://APPONE;
add_header APP ONE;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
if ($http_host ~ 'APP=TWO') {
location ~ ^/(framerwork1/|framerwork2/|framerwork3/) {
proxy_pass http://APPTWO;
add_header APP TWO;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
Question: How can I proxy these two application successfully based on the header? Please note, I am unable to use a different server block or domain. They must be the same.
Thanks in advance.
Ok, so a request is coming in to www.domain.com/APPONE . You don't say exactly what is being requested through the URL www.domain.com/framerwork1/ but lets imagine it's an image that is required by the page served at /APPONE as that is simple to understand.
When you are setting the header through 'add_header' it is being set in the second array in "Client" -> Nginx -> PHP Proxy.
When the client then makes the request to the image at www.domain.com/framerwork1/ it has no idea that you are hoping to have a header set to be able to determine if the image was requested by APPONE, or something else.
Even if you set the header on the response for www.domain.com/APPONE clients do not blindly echo headers sent to them back to server for subsequent requests.
Or to put it bluntly, I think you've misunderstood where the headers are being passed, and that there isn't really a way to determine what application is making a request to the /framerwork1/ URL like that. You have to do this either via separate domain names (or sub-domain) or by doing it inside your application with things like looking at which application the user is logged into.

Resources