Nginx works using configuration files. You define a configuration for a single site or all the sites in a machine and Nginx serves the content of that defined site. In the past, folks defined new sites in the /etc/nginx/sites-available
and /etc/nginx/sites-enabled
directories. These days, Nginx suggests that you use the conf.d
directory. The default Nginx config serves the default Nginx page, i.e. welcome to Nginx. But you can easily replace it with something simple that reverse proxies your requests. Something like this:
http {
server {
listen 80;
location / {
proxy_pass http://127.0.0.1:5000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
}
The config above serves the machine assigned interfaces on port 80 but proxies the services running on port 5000 to it. This is by far the cleanest way to serve your custom apps on port 80, rather than using sudo
to force them to bind to port 80.
If you say, have a subdomain that you want to proxy requests through, you can define this config in the conf.d
directory. You simply have to define the server_name which will be a subdomain that points to the IP address assigned to your machine via an A record
. You should configure this subdomain both for the standard subdomain and www. This way, you handle all cases of people trying to reach your website. The proxy_pass
in the location block is the same as you saw earlier. It proxies requests sent to these subdomains to the service running on port 8089.
server {
listen 80;
server_name ubuntuserver.osinachi.me www.ubuntuserver.osinachi.me;
location / {
proxy_pass http://localhost:8089;
}
}
After saving your file, say ubuntuserver.conf
in the conf.d
directory, you simply need to reload Nginx. This can be achieved using the command sudo nginx -s reload
. Nginx can now handle serving your app content.