From Nginx subpath to root in different port - nginx

I have one server with several web services exposed in different ports in docker containers.
With nginx I would like use subpaths to browse througth these servers.
For example:
I have Nextcloud in http://myurl:8080/
Reachable from http://myurl:80/nextcloud.
I tried different solution, probably the most closed to reach the solution is the following:
location /nextcloud/{
proxy_pass http://myurl:8080/;
}
But in this way I lost the first parameter in the url:
instead of proxying on http://myurl/nextcloud/a/b; I'm proxed on http://myurl/nextcloud/b, losing /a

location /nagios/ {
rewrite ^/nagios(/.*)$ $1 break;
proxy_pass http://10.0.21.8:80/;
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_set_header X-Forwarded-Proto https;
proxy_redirect off;
}
Reference: https://raymii.org/s/tutorials/NGINX_proxy_folder_to_different_root.html

Related

How to configure nginx to redirect with subpaths matching given pattern

I am using nginx as a reverse proxy. I want to redirect locations matching some patterns keeping exactly the same subpaths. For example, this configuration does not work for me:
location ~ ^/bpmn/?(.*)$ {
resolver 127.0.0.11 ipv6=off;
set $upstream activiti:8080;
proxy_pass http://$upstream/$1;
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;
proxy_set_header X-Forwarded-Host $server_name;
}
I would like to redirect the location matches the path "/bpmn/a/b/c" to "http://$upstream/a/b/c". My configuration works only for one level (e.g. "bpmn/a").
Thanks a lot for your help,
Regards.

Nginx - How to let proxypass destination know the request is from Nginx?

Below is my Nginx config -
location / {
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 off;
proxy_pass http://destination_server;
}
Is there any way for the destination_server know that the request is coming from the Nginx server?
A possible solution might proxy_set_header Nginx-Name $custom_name. And then on the destination_server, I can check the request header to see if the request is coming from a certain Nginx.
If there a better way for that? Thanks.

NGINX proxy http://host/jenkins to http://host:8080

I try to use NGINX as a reverse proxy for my Jenkins server. Basically when http://host/jenkins gets opened in the browser NGINX should proxy the request to http://host:8080 where Jenkins is listening.
I tried various different configurations but none really works. Here the location configuration that I use at the moment. It somehow works, but does not show any images, etc..
location /jenkins/ {
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass http://localhost:8080/;
proxy_read_timeout 90s;
# Fix potential "It appears that your reverse proxy set up is broken" error.
proxy_redirect http://localhost:8080/ https://host/jenkins/;
}
Make sure to update your Jenkins configuration
JENKINS_ARGS="--webroot=/var/cache/jenkins/war --httpPort=$HTTP_PORT --ajp13Port=$AJP_PORT --prefix=/jenkins"
Taken from https://wiki.jenkins.io/display/JENKINS/Jenkins+behind+an+NGinX+reverse+proxy

Using nginx as a reverse proxy to IIS server

I have multiple ASP.NET applications running on a single IIS server with different ports for each application.
I have installed nginx on the same server so that my clients can access all my applications only using port 80.
Nginx is running all right on port 80. My individual ASP.NET applications are also up and running.
I made these changes in nginx conf file
location /students/ {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $host;
proxy_pass http://127.0.0.1:84;
}
location /registration/ {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $host;
proxy_pass http://127.0.0.1:82;
}
Then I restarted nginx and typed the url http://127.0.0.1/students/ in my browser. Nginx served a 404 page.
I made no other changes to the conf file.
What I am doing wrong?
I believe that the problem you are having is related to the start of the URL path. Does the URL http://120.0.0.1:84/students/ return the page, or a 404? If you are expecting to go to http://127.0.0.1:80/students/ and see the page at http://127.0.0.1/, you will find that nginx does not transform the path for you with this configuration. Rather, it looks for exactly the same path at the proxied server.
You need to put the / on the end of the URL in the proxy_pass directive:
location /students/ {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $host;
proxy_pass http://127.0.0.1:84/;
}
This is a subtle but important gotcha in nginx configs! If you don't include the backslash, http://127.0.0.1:84 will be treated as a server location. If you do have the backslash, it will be regarded as a URL, and it will replace everything in the proxy URL up to the 'location' part.
If you want to transform IIS 127.0.0.1:84/students to nginx 127.0.0.1/students . try below code..
location /students {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $host;
proxy_pass http://127.0.0.1:84/students;
}
location /registration {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $host;
proxy_pass http://127.0.0.1:82/registration;
}
Try to use this directive
upstream http://localhost {
server 127.0.0.1:84;
}
and the same block for 2nd

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.

Resources