user1669296 user1669296 - 2 months ago 13
Apache Configuration Question

How to configure nginx to route to different IP?

Providing it's possible, how can I use nginx to route traffic to a different IP address?

I have one domain and 2 different servers where I want to serve up content. Main website is running on http://example.com and currently works. I have a forum I want to run with the URL: http://www.example.com/forum, but the forum code/files are on a different server, different hosting company than where the main website is running.

How can I use nginx to force it so when a user types in http://example.com/forum it loads the content from the other server, but to the end user it would be transparent.

Is this possible, if so what steps would I take to set this up?

Answer

For that you need to config nginx to serve from two different servers. location / will serve from app server, and location /forum should server from the forum server.

Digital Ocean Guide

http{
    server {
        listen 80;
        server_name example.com ;

        location /forum {
          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_set_header X-NginX-Proxy true;

          proxy_pass http://forum-server:port;
          proxy_redirect off;
        }

        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_set_header X-NginX-Proxy true;

          proxy_pass http://app-server:port;
          proxy_redirect off;
        }
    }   
}
Comments