nginx reverse proxy and paths - nginx

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

Related

nginx proxy_pass with rewrite in url not keeping the original paths

I'm trying to setup nginx to forward incoming requests in the following way:
http://localhost/service -> http://localhost:8080
http://localhost/service/foo -> http://localhost:8080/foo
Now I am able to achieve the first line with the following config:
...
upstream service {
server service:8080;
}
...
location /service/ {
server_name_in_redirect off;
proxy_pass http://service;
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;
}
but when I add extra paths (e.g. /foo) the url is rewritten in this way
http://localhost/foo/
therefore I will never be able to reach localhost:8080/foo. Any ideas on how to make this work?
Thanks in advance

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

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

Nginx Passing to Servers Based on URI

I have been setting up Nginx on my router, and creating subdomains (with CNAMES) to access various components within my network. It has mostly been fairly easy, until I have come to the cameras which are proving to be a problem.
They are basic IP cameras and to date I had opened each one on a different port. They have basic authentication, and once that has been entered I am presented with a live view.
Like all the other components I have set up so far (and they all work) I started by configuring one:
server {
listen 80;
server_name cam.example.co.uk;
location / {
proxy_pass http://192.168.1.101:2001;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
Hitting cam.example.co.uk from either LAN or WAN gives me a username and password prompt and then the live view loads.
Since there are 9 cameras, I thought it would be a good idea to use /1, /2, /3 etc. at the end to direct me to each one rather can creating subdomains.
server {
listen 80;
server_name cam.example.co.uk;
location /1/ {
proxy_pass http://192.168.1.101:2001;
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 /2/ {
proxy_pass http://192.168.1.102:2002;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
With that I got 404 not found errors, and messages in the logs such as:
"/usr/html/new/index.htm" failed (2: No such file or directory)
Some Googling later I found out that I may need to specify the URI as well in the proxy_pass line, so I changed them to look like:
proxy_pass http://192.168.1.102:2002/new/index.htm;
This then results in the username and password prompt, but when the credentials are entered, all I am left with is a blank screen. It worked fine when it was just location / so no idea why nothing is showing now.
I have a feeling that it is putting the URI in somewhere, but I have no idea where/why or what to do about it.
EDIT
Been Googling and trying various things:
location /1 {
resolver 127.0.0.1;
set $backend "http://192.168.1.101:2001/new/index.htm";
proxy_pass $backend;
proxy_buffering on;
proxy_redirect http://192.168.1.101:2001/new/index.htm http://cam.example.co.uk/1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
Then going to this in the browser cams.example.co.uk/1 brings up the username and password prompt, but then displays a blank page. Looking at the Chrome developer tools I can see unexpected token errors, and it looks like it isn't loading the .js files properly.
If the proxy_pass directive is specified with a URI, then when a request is passed to the server, the part of a normalized request URI matching the location is replaced by a URI specified in the directive.
Try this:
server {
listen 80;
server_name cam.example.co.uk;
location /1/ {
proxy_pass http://192.168.1.101:2001/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_redirect http://192.168.1.101:2001/ http://cam.example.co.uk/1/;
}
location /2/ {
proxy_pass http://192.168.1.102:2002/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_redirect http://192.168.1.101:2002/ http://cam.example.co.uk/2/;
}
}
Source: http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass

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

Resources