0

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

sagar
  • 47
  • 3
  • Why are you redirecting to `1.2.3.4:50/a` ? You want nginx to _proxy_ these 3 servers. And nginx runs on the same machine, so you can use `127.0.0.1:50`. No need to expose the backend servers to the world. – MSalters Feb 14 '23 at 11:20
  • Hi Thanks for reply. Because my website is running on 1.2.3.4:50 , 1.2.3.4:60 and 1.2.3.4:70 i want to access these 3 sites by url abc.com/a will show site from 1.2.3.4.:50 abc.com/b will show site from 1.2.3.4:60 and all websites are running on same server (same ip) but different ports – sagar Feb 14 '23 at 11:23
  • I am pointing out that you **shouldn't** run your docker containers on `1.2.3.4:50, 1.2.3.4:60 and 1.2.3.4:70`. Heck, put nginx in its own docker container, and connect it via the docker network. Webbrowsers will **only** talk to `nginx`. – MSalters Feb 14 '23 at 11:31

1 Answers1

0

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 ...
}
  • Hi azvya thank for reply... i am new to nginx. in which file i need to do this changes? only this code is sufficient or i need to add some code in file – sagar Feb 14 '23 at 11:28
  • Just updated the answer, check it out – Azvya Erstevan Feb 14 '23 at 11:33
  • Hi, tried with your code, giving error nginx error! The page you are looking for is temporarily unavailable. Please try again later. Website Administrator Something has triggered missing webpage on your website. This is the default error page for nginx that is distributed with Red Hat Enterprise Linux. It is located /usr/share/nginx/html/50x.html You should customize this error page for your own site or edit the error_page directive in the nginx configuration file /etc/nginx/nginx.conf. – sagar Feb 14 '23 at 11:40
  • Can you provide full nginx.conf in the question? And probably with sites-available that you've just created – Azvya Erstevan Feb 14 '23 at 11:40
  • Hi Azvya, Thanks for help...... it was permnission issue. After giving permission it is working fine. – sagar Feb 15 '23 at 11:33