Reputation: 21
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.
Upvotes: 2
Views: 13092
Reputation: 10556
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/etc/nginx/conf.d
and /etc/nginx/sites-enabled
/etc/nginx/conf.d/<some-file>
is where you put additions to the main config.
/etc/nginx/conf.d/<packagename>
(think packages like gitweb, or some php-thing). /etc/nginx/conf.d/local
or /etc/nginx/conf.d/local_something
in there with any additions you need./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:
rewrite
as above, the url shown in the browser will be changed to the rewritten url automatically. Upvotes: 8