skip to Main Content

So i’ve came across a cool project and i wanted to recreate it. It is my first time using nginx and also my first time learning things about a reverse proxy. I’ve currently have a reverse proxy running and it works (I guess). But the Proxy currently only works with other ports. I have 3 servers that are running nginx. I use one of them as my reverse proxy. I can access the other servers with different ports. See here (reverse-proxy.conf):

server {
        listen 80;
        root /var/www/html;
        server_name localhost;

        location / {
                    proxy_pass http://192.168.2.20;
        }
}

server {
        listen 8080;
        root /var/www/html;
        server_name localhost;

        location / {
                    proxy_pass http://192.168.2.30;
        }
}

Are there a way to use the reverse proxy without using different ports? Or is my solution ok? At the end i just need a reverse proxy that is able to communicate with 2 other servers.

2

Answers


  1. So one thing here people use reverse proxy in a different ways

    But most generic usecase is redirect using location.

    Please find the below example.

    server {
            listen 80;
            root /var/www/html;
            server_name localhost;
    
            location /a {
                        proxy_pass http://192.168.2.20;
            }
            location /b {
                        proxy_pass http://192.168.3.20;
            }
    }
    

    Another is giving weight to each proxy.

    Please find the below example

    stream { 
    upstream stream_backend { 
        server http://192.168.2.20 weight=75;
        server http://192.168.3.20 weight=25; 
    } 
    server { 
        listen 80;
        root /var/www/html;
        server_name localhost;
    
        location / {
        proxy_pass stream_backend;
    } 
    

    }

    In above 192.168.2.20 will receive 75% of the load and 192.168.3.20 will receive 25% of the load. In case if you want to distribute the equal load to both(or round-robin method) Please remove the weight.

    Login or Signup to reply.
  2. I think you may not understand how Nginx work about proxy.
    Nginx can reverse Proxy L7 http or L4 stream
    and you set the proxy listen on any port or URL you want and proxy to any server or port or URL you want.

    server {
            listen 80;
            root /var/www/html;
            server_name localhost;
    
            location / {
                        proxy_pass http://192.168.2.20:2323/URL;
            }
    }
    server {
            listen 8080;
            root /var/www/html;
            server_name localhost;
    
            location / {
                        proxy_pass unix:/tmp/backend.socket;
            }
    }
    

    Here is a reference for you about the proxy_pass directive.
    proxy_pass

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search