Sails JS Reverse Proxy with Apache2 and Nginx

Author - Bipin

While working with Sails js or Node js, it’s really important to hide port 1337 and not directly exposing Sails js server publicly. Instead, we should use the concept of a reverse proxy. We can do it whether we are using Apache2 or Nginx. Another benefit with this is we can serve static content directly via apache or Nginx. Here I will cover different ways for Sails js reverse proxy with Apache and Nginx.

Sails js Reverse Proxy with Apache2

First, to enable reverse proxy we need to enable some apache2 modes as below

sudo a2enmod headers proxy_http xml2enc proxy ssl proxy_wstunnel rewrite proxy_ajp deflate proxy_balancer proxy_connect proxy_html

We can do apache2 reverse proxy in two ways.

Apache2 Reverse Proxy – Way 1

Changing in Particular VirtualHost but without using <Location> and <LocationMatch>.
Below is full VirtualHost config

<virtualhost *:80="">
	
    ServerName logisticinfotech
    ServerAlias logisticinfotech.com

    ServerAdmin webmaster@localhost
    DocumentRoot /home/ubuntu/www

    ErrorLog ${APACHE_LOG_DIR}/li_error.log
    CustomLog ${APACHE_LOG_DIR}/li_access.log combined

    ProxyRequests           Off
    ProxyPreserveHost       On
    <proxy *>
        Order deny,allow
        Allow from all
    </proxy>

    ProxyPass           /mysite     !  
    # Exclude some path from reverse proxy
    # http://localhost/mysite/
	
    ProxyPass    /phpmyadmin	!
    Alias        /phpmyadmin  	/usr/share/phpmyadmin
    
    # Necessary part to handle socket request with reverse proxy
    RewriteEngine On
    RewriteCond %{REQUEST_URI}  ^/socket.io            [NC]
    RewriteCond %{QUERY_STRING} transport=websocket    [NC]
    RewriteRule /(.*)           ws://localhost:1337/$1 [P,L]

    ProxyPass 		/ 	http://localhost:1337/
    ProxyPassReverse 	/ 	http://localhost:1337/
    
</virtualhost>

If you are using socket.io then don’t forget to add below,
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/socket.io [NC]
RewriteCond %{QUERY_STRING} transport=websocket [NC]
RewriteRule /(.*) ws://localhost:1337/$1 [P,L]

Apache2 Reverse Proxy – Way 2

This one is my favorite way of setup. As you can see below, it gives us more easy and understandable flexibility with LocationMatch and Location.

<VirtualHost *:80>
    ServerName logisticinfotech.com

    ServerAdmin webmaster@localhost
    DocumentRoot /home/ubuntu/www

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    <LocationMatch  "^/(?!js|css|media)" >
            ProxyPass   http://127.0.0.1:1337
            ProxyPassReverse   http://127.0.0.1:1337
    </LocationMatch>

    <Location "/dist/">
            ProxyPass !
    </Location>

    RewriteEngine On
    RewriteCond %{REQUEST_URI}  ^/socket.io            [NC]
    RewriteCond %{QUERY_STRING} transport=websocket    [NC]
    RewriteRule /(.*)           ws://localhost:1337/$1 [P,L]
</VirtualHost>

Sails js Reverse Proxy with Nginx

Nginx is another web server best choice of community.
For Nginx also I am adding two ways but mostly both are same, just change is in defining them
Way1

map $http_upgrade $connection_upgrade {
    default upgrade;
    '' close;
}

upstream sails_server {
    server 127.0.0.1:1337;
    keepalive 64;
}

server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    root /var/www/html;
    index index.html index.htm;

    server_name logisticinfotech.com;

    location / {        

        proxy_pass                          http://sails_server;
        proxy_redirect     off;
        proxy_http_version                  1.1;
        proxy_set_header  Connection        "";
        proxy_set_header  Host              $host;
        proxy_set_header  X-Forwarded-For   $proxy_add_x_forwarded_for;
        proxy_set_header  X-Real-IP         $remote_addr;

        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;

    }

    error_page 404 /404.html;
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
        root /usr/share/nginx/html;
    }

    location /mongoWebAdmin/ {
        root /var/www/html/mongoWebAdmin;
        try_files   $uri  $uri/ /mongoWebAdmin/index.php?$query_string;
    }

    location ~ \.php$ {
        #try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

Way2

server {

    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    root /var/www/html;

    index index.html index.htm index.nginx-debian.html;

    server_name logisticinfotech.com;

    location / {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_set_header X-NginX-Proxy true;

        proxy_pass http://18.205.44.77:8888/;

        proxy_redirect off;

        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }

}

Conclusion

I don’t think there is more to say while we are setting up servers ;).
Post me issues you are getting, or things you would like to add/update in this post.

 

You might be interested!!

Free SEO Checker |
Test your website for free with OnAirSEO.com

Get Your SEO report!

Don’t miss the next post!

Loading

Related Posts