configure /etc/nginx/nginx.conf to redirect to separate host

12,129

On Debian-based distributions (including Ubuntu) the following considerations are true for nginx (similar layout is used by the apache packages):

  • /etc/nginx/nginx.conf is the main config file
  • the main config file includes all files in /etc/nginx/conf.d and /etc/nginx/sites-enabled
  • /etc/nginx/conf.d/<some-file> is where you put additions to the main config.
    • If any packages other then nginx add configuration those will be in /etc/nginx/conf.d/<packagename> (think packages like gitweb, or some php-thing).
    • Add a file /etc/nginx/conf.d/local or /etc/nginx/conf.d/local_something in there with any additions you need.
    • By sepparating things out like that it makes it easy to determine where any potential problems are.
  • /etc/nginx/sites-enabled/<some-file> is where you put the configuration additions needed for virtual hosts, 1 file per virtual host. By sepparating out the virtual hosts, it becomes easier to know where to look to change/fix things (as you know automatically ignore everyting not relevant to that vhost).
  • /etc/nginx/sites-available/ is where packages put config for vhosts they want to add. These are not automatically active, to enable such a site you'd link it from or copy it to /etc/nginx/sites-enabled/

Since the additions you want to make are specific to the mydomain.com virtual host, you should add them to /etc/nginx/sites-enabled/mydomain.com. The contents of which should be something like this:

server{
  server_name mydomain.com;

  location /page1/ { rewrite ^/page1/(.*)$ $scheme://10.100.0.163/$1 permanent; }
  location /page2/ { rewrite ^/page2/(.*)$ $scheme://10.100.0.164/$1 permanent; }

  #add config needed to server whatever else on 
  #location / {}
}

Notes about that config:

  • If you do it with a rewrite as above, the url shown in the browser will be changed to the rewritten url automatically.
  • If you want the url shown to the user to remain "mydomain.com/page1", you need to use a proxy_pass-directive instead of a rewrite (keyword for this kind of setup is reverse proxy)
  • you use $scheme:// instead of $http:// as it won't break that way if you also have that vhost listen on https:// later on
Share:
12,129
Michael Surkan
Author by

Michael Surkan

Updated on June 17, 2022

Comments

  • Michael Surkan
    Michael Surkan almost 2 years

    Does anyone know how to modify the nginx.conf file to redirect to another host? I would like to direct http://mydomain.com/page1 and http://mydomain.com/page2 to separate hosts on my local network. mydomain.com/page1 should go to 10.100.0.163 and mydomain.com/page2 should go to 10.100.0.164.

    Here is what I added to the HTML section of the nginx.conf file on my Ubuntu server.

    location /page1/ {
    rewrite ^/page1/(.*)$ http://10.100.0.163/$1 permanent;
    }
    
    location /page2/ {
    rewrite ^/page2/(.*)$ http://10.100.0.164/$1 permanent;
    }
    

    Is there anything else I need to add to the nginx.conf file? Are there any other configuration files I also need to modify?

    I just did a standard install of nginx and haven't changed anything else.

  • Michael Surkan
    Michael Surkan about 11 years
    Thanks for the advice. I will create a file called 'mydomain.com' in the sites-enabled folder with the server function you suggest. Is this the only thing you suggest I do? Do I also need to add something to one of the other nginx configuration files so that it knows this virtual host exists?
  • cobaco
    cobaco about 11 years
    if you look in the /etc/nginx/nginx.conf you'll notice it has the line include /etc/nginx/sites-enabled/*; that makes it pick up any file in the sites-enabled folder (note: nginx only notices changes to the configuration on restart or reload, so you do need to do service nginx reload). You don't need anything else for what you described