nginxnginx-reverse-proxynginx-configjwilder-nginx-proxy

Run 3 websites on one domain using nginx


Lets say i have domain abc.com which redirects my server ip 1.2.3.4

i have 3 websites running on my server using docker container,

1st website ip 1.2.3.4:50/a
2nd website ip 1.2.3.4:60/b
3rd website ip 1.2.3.4:70/c

i can access this all websites using ip

i want to use my domain to run these three websites. e.g.

abc.com/a should run 1st website 
abc.com/b should run 2nd website 
abc.com/c should run 3rd website 

i am using nginx server version 1.14.1 on RHEL

Please help

i tried these configuration file

location /a {
                rewrite ^/a(.*)$ http://1.2.3.4:50/a redirect;
        }

 location /b {
                rewrite ^/b(.*)$ http://1.2.3.4:60/b redirect;
        }

but it is redirecting and displaying the port in the url


Solution

  • Try this to solve your problem

    location /a {
            proxy_pass http://1.2.3.4:50;
            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 /b {
            proxy_pass http://1.2.3.4:60;
            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 /c {
            proxy_pass http://1.2.3.4:70;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
    

    For your information, this proxy configuration will make your abc.com/a to access the root of your http://1.2.3.4:50 website. But the /a will also be passed to the http://1.2.3.4:50 so you don't have to bother add http://1.2.3.4:50/a for each proxy

    You can configure it explicitly inside the nginx.conf

    // Nginx.conf
    http {
     ...
     server {
        server_name abc.com;
        // here is location ...
       }
     }
    

    Or by including configurations in sites-available, usually in the nginx.conf you can found this section:

    // Nginx.conf
    http {
     ...
     include /etc/nginx/sites-available/*;
     ...
    }
    

    Then you can add site.conf or main.conf, you name it yourself for example in sites-available folder that has this kind of configuration:

    server {
        server_name abc.com;
        // here is location ...
    }