How to forward http request to another port or service in Nginx reverse proxy?
Nginx can be used to forward HTTP request from one server to another. Requests are typically forwarded from the reverse proxy to the web server using it. It is frequently used to modify URLs if your domain has moved or changed. But occasionally you might need to use nginx to route requests to a different port. The steps below must be followed when redirecting a subdomain or domain to another application using nginx.
How to add/edit a domain configuration in nginx?
The default configuration file for Nginx is /etc/nginx/nginx.conf. You can add domains to this configuration but it will become harder to manage. So, it is recommended to add another configuration file (in /etc/nginx/sites-available
directory) and add a new domain to it.
1. Start by creating a new configuration file in sites available directory. Name the configuration file with the domain name in it for quick identification.
$ sudo touch /etc/nginx/sites-available/example-domain.com.conf
2. Open the settings file in a text editor.
$ sudo vi /etc/nginx/sites-available/example-domain.com.conf
How to Use NGINX to Forward Request to Another Port
1. In the simplest case, Let's say you wish to send requests to http://domain.com:8000/abc from http://domain.com/lite/xyz. you can configure your nginx server to add a location block. You can also change the IP address to another server's IP.
Please remember to add forward slash (/
) after port number 8000
and not after /lite
as shown below. If you want to redirect request http://domain.com/lite/xyz to http://domain.com:8000/lite/xyz then add the following location block. In this case, do not add forward slash after port number as shown below.
location /lite {
proxy_pass http://127.0.0.1:8000/;
}
2. You can also use the setup below, which is more thorough and enables more control. For instance, this one permits hostname preservation while the ones mentioned above do not.
You can also use rewrite with this configuration rewrite ^/route/?(.*)$ /$1 break;
.
location /route/ {
proxy_pass http://127.0.0.1:9000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
3. We can also redirect to multiple (node) services to load balance. Here 5 nodeapp services are running and and we are using nginx as reverse proxy, load balancers and web server to redirect the request to the domain.com
to the nodeapp.
upstream {
nodeapp 127.0.0.1:8000;
nodeapp 127.0.0.1:8081;
nodeapp 127.0.0.1:8082;
nodeapp 127.0.0.1:8083;
nodeapp 127.0.0.1:8084;
}
server {
listen 80;
server_name domain.com;
location /(.*) {
proxy_pass http://nodeapp/$1$is_args$args;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Real-Port $server_port;
proxy_set_header X-Real-Scheme $scheme;
}
}
Finally, You can save the file and exit the editor.
Verify our configuration, then launch Nginx.
It is a recommended practice to test new configurations before loading them to the production server because a single syntax mistake will prevent the Nginx service from running, which will prevent users from accessing your website. You can use the following command to do so.
$ sudo nginx -t -c /etc/nginx/sites-available/domain.com.conf
Enable the site if no issues were discovered. In order to accomplish this, we must build a symbolic link between the site configuration file and the sites-enabled directory using the following command:
$ sudo ln -s /etc/nginx/sites-available/domain.com.conf /etc/nginx/sites-enabled/domain.com.conf
Finally, Start/Restart/Reload the server to load the changes:
$ sudo systemctl restart nginx
or
$ sudo service nginx restart