Node-Red: Nginx Reverse Proxy

This article  is a sequel of our Node-Red series of tutorials and focuses on proxying Node-Red instance with Nginx Web Server. There are numerous benefits of using this setup, especially the security features such as login rate limiting to avoid brute force attack. However, in this episode, we’ll just focus on the basic proxy setup for Node-Red using Nginx Web Server.

Nginx Configuration:

  • Navigate to the nginx configuration folder cd /etc/nginx/sites-available.
  • Create a new configuration file e.g. sudo nano nodred.
  • Add server block by copying the server block given below.
server {
  listen 80;
  server_name your_server_name;

  access_log /var/log/nginx/access.log;

  location / {
    proxy_pass http://localhost:1880;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
  }
}
  • Change the server_name to your own domain / sub-domain.
  • Change the port in the Server Block, as desired.
  • If you want to redirect a sub-domain to some nested URL such as /ui of dashboard node, add redirect server block before the actual reverse proxy server block.
server {
  server_name  sub.domain.com;
  location / {
    return 301 http://www.domain.com/ui/;
  }
}

server {
  listen 80;
  server_name domain.com www.domain.com;

  location / {
    proxy_pass http://localhost:1880;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
  }
}
  • For serving static contents such as css or js files with the help of http nodes, add one more location to server block and change admin and static/contents with your own keyword specified in http-in node.
server {
  server_name  sub.domain.com;
  location / {
    return 301 http://www.domain.com/ui/;
  }
}

server {
  listen 80;
  server_name domain.com www.domain.com;

  location / {
    proxy_pass http://localhost:1880;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
  }
  
  location ~* ^/admin/(.+\.(css|js)) {
    rewrite ^/static/contents/$1 break;
    proxy_pass http://localhost:1880;
  }
}
  • Create a symbolic link for the virtual block created above sudo ln -s /etc/nginx/sites-available/nodered /etc/nginx/sites-enabled/.
  • Test nginx if configuration is correct sudo nginx -t. If everything is OK, then it will show the following message.
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
  • Reload the Nginx to apply the changes sudo systemctl reload nginx.
  • Now you can access the Node Red via Nginx Proxy.

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x