Nginx: redirection to (private ip) virtual machines

5,375

You should look at the proxy_redirect instruction. The doc covers exactly the situation you have, where you want a redirect response coming back from the proxy to be rewritten to the outside world view of the url.

You probably just need in each or your location an extra line

proxy_redirect default;
Share:
5,375

Related videos on Youtube

Brandt
Author by

Brandt

I like to say I handle out-of-this-world data. And I do. Is super cool.

Updated on September 18, 2022

Comments

  • Brandt
    Brandt over 1 year

    I want to setup a set of web services for my colleagues from my laboratory; simple things like wiki, cms, git, etc, so that we can document our projects better. I'm an physicist, not CS, so sorry if I miss something obvious.

    The services would be behind the same (public) IP address. For such, I'd setup virtual machines as the service providers and installed Nginx on the host machine to redirect the corresponding calls to the VMs.

    The problem I'm having seems to be related to the (url) path used after the hostname. I don't know if it is related to the Nginx redirection or to the web services themselves.

    Currently, the scenario is the following:

    • The host machine has a public IP address, linked to a domain name. Let me use here, for the sake of simplicity, "www.example.com".
    • The virtual machines have private IP addresses. One has Drupal installed, the other one Mediawiki and a third one with Gitlab.
      • The "drupal" machine has the IP 192.168.56.20
      • The "wiki" machine has the IP 192.168.56.11
      • The "git" machine has the IP 192.168.56.19
    • The host machine has (besides VirtualBox) Nginx installed. My aim with Nginx is to have a simple solution for the following redirections:
      • www.example.com/drupal ---> goes to the "drupal" machine
      • www.example.com/wiki ---> goes to the "wiki" machine
      • www.example.com/git ---> goes to the "git" machine
    • To configure Nginx I simply modified the default config file (/etc/nginx/sites-enabled/default).

    The problem(s) I'm having: - When I try to access www.example.com/wiki (from any machine on the network) or localhost/wiki (from the host machine), the url is changed to (the usual) www.example.com/wiki or localhost/Main_Page, respectively, and I get the "404 Not Found" error. - The same happens when I try localhost/git: I get the "404 Not Found" error, after being redirected to localhost/users/sign_in. - When I try localhost/drupal everything works fine, but only until I change the web site path (for example, I click on the "register" button), then localhost/user/register does not work anymore ("404 Not Found").

    I understand that this errors are related the url. It is clear to me that Nginx doesn know what to do with (e.g) localhost/Main_Page since I did not tell him what to do with it, but how do I fix that? I mean, who's in charge of that between Nginx and the service(s)?

    Log and config follow. Modifications I've made to Nginx' config file were taken from this nice tutorial.

    Any help? Thanks in advance.

    Here is the log of me trying to acces (as described above) the "/wiki", "/git" and "/drupal (and clicking on the 'register' button)" from the "localhost":

    127.0.0.1 - - [29/Jul/2015:12:44:41 +0200] "GET /wiki HTTP/1.1" 301 0 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.81 Safari/537.36"
    127.0.0.1 - - [29/Jul/2015:12:44:41 +0200] "GET /Wiki HTTP/1.1" 404 208 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.81 Safari/537.36"
    127.0.0.1 - - [29/Jul/2015:12:45:17 +0200] "GET /wiki HTTP/1.1" 301 0 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.81 Safari/537.36"
    127.0.0.1 - - [29/Jul/2015:12:45:17 +0200] "GET /Main_Page HTTP/1.1" 404 208 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.81 Safari/537.36"
    127.0.0.1 - - [29/Jul/2015:12:45:24 +0200] "GET /git HTTP/1.1" 302 111 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.81 Safari/537.36"
    127.0.0.1 - - [29/Jul/2015:12:45:24 +0200] "GET /users/sign_in HTTP/1.1" 404 208 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.81 Safari/537.36"
    127.0.0.1 - - [29/Jul/2015:12:45:31 +0200] "GET /drupal HTTP/1.1" 200 2255 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.81 Safari/537.36"
    127.0.0.1 - - [29/Jul/2015:12:45:36 +0200] "GET /user/register HTTP/1.1" 404 208 "http://localhost/drupal" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.81 Safari/537.36"
    

    Here is my Nginx config file:

    server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;
    
    root /usr/share/nginx/html;
    index index.html index.htm;
    
    # Make site accessible from http://localhost/
    server_name localhost;
    
    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ =404;
        # Uncomment to enable naxsi on this location
        # include /etc/nginx/naxsi.rules
    }
    
    location /wiki {
        rewrite ^/wiki(.*) /$1 break;
        proxy_pass http://192.168.56.11;
    }
    
    location /drupal {
        rewrite ^/drupal(.*) /$1 break;
        proxy_pass http://192.168.56.20;
    }
    
    location /git {
        rewrite ^/git(.*) /$1 break;
        proxy_pass http://192.168.56.19;
    }
    
    location /google {
        rewrite ^/google(.*) /$1 break;
        proxy_pass http://www.google.com;
    }
    
    location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
        expires max;
        log_not_found off;
    }
    
    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
    }
    
    }