weatherbox:database:reverseproxy

Creating a Reverse Proxy with Nginx for Web API Access

The REST API is normally accessed via localhost on the local server. To allow web access to the REST API, you will need to setup Nginx as a reverse proxy.

In order to use a url instead of the server address, you will need to generate a new DNS record on the scel-hawaii.org domain. The DNS nameservers were changed from the original registration service (NameSilo) to CloudFlare so you will need to modify the DNS in CloudFlare. Access the DNS tab and generate a new domain name. Use the A record setting and add the new subdomain name in the name field (ex. “api” for “api.scel-hawaii.org”). The IPv4 address should be the origin server address, TTL should be left on automatic, and the cloud icon should be changed to the gray icon with the arrow going around the cloud. CloudFlare's CDN services may cause problems with the API and should be avoided until tested further. The DNS may take some time to propagate through the internet, especially if the domain already existed.

Once the subdomain is pointing to the server, you will need to create a reverse proxy to access the REST API over localhost. To do this, you will need to create a new server configuration file in /etc/nginx/sites-available/.

server {
  listen 80; #Specifies the port Nginx will listen on for requests
  server_name api.scel-hawaii.org www.api.scel-hawaii.org; #Specifies the addresses Nginx will serve
  
  location / {
    proxy_set_header X-Real-IP http; #Client IP address for Nginx
    proxy_set_header X-Forwarded-For $remote_addr; #Client IP address for PHP
    proxy_set_header Host $host; #Enables proxy to same server/machine
    proxy_pass http://127.0.0.1:3000; #Enables the passing of header fields
  }
}

Once you write the configuration file, you will need to enable the new web server. To enable, run this command “sudo ln -s /etc/nginx/sites-available/api /etc/nginx/sites-enabled/api” (configuration file named api in this example). To apply these changes to the webserver, run this command “sudo systemctl reload nginx”. Next, check to ensure the nginx server is still running with this command “systemctl status nginx”.

The reverse proxy should now be setup successfully. When you access the subdomain, you should receive a JSON response from the REST API. If you receive an error 502, that means the REST API on the server is not started or not functioning correctly.

Authors

Contributing authors:

rmukai

Created by rmukai on 2017/09/27 02:33.

  • weatherbox/database/reverseproxy.txt
  • Last modified: 2021/09/19 21:59
  • (external edit)