How to configure apache to forward https requests to a Strapi/node application running on port 1337? - node.js

I have a Strapi/node js application running on port 1337, and I can reach it with:
http://my-ip:1337
I also have a domain associated in the same server and a working ssl certificate in apache2, so that in the browser I can use https:
https://my-domain.com
Now, when I try to use https to reach port 1337 like this:
https://my-domain.com:1337
I get an "This site can’t provide a secure connection" error.
I tried to edit my my-domain.ssl.conf in apache 2 with the following code:
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerName www.my-domain
ServerAlias my-domain
DocumentRoot /var/www/my-domain/public_html
ErrorLog /var/www/my-domain/log/error.log
CustomLog /var/www/my-domain/log/requests.log combined
SSLProxyEngine On
ProxyPreserveHost On
ProxyPass / http://localhost:1337/
ProxyPassReverse / http://localhost:1337/
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/my-domain/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/my-domain/privkey.pem
</VirtualHost>
</IfModule>
But unluckily when I introduce the ProxyPass directive the server stops working without any meaningful error.
Both mod_proxy and mod_proxy_http are loaded in the apache2 configuration..
What could be the problem here? Thanks to anyone who can help with it!!

Strapi/node js application running on port 1337 to https
1st Changes
Edit /etc/apache2/sites-available/000-default.conf
ServerName yourdomian-name.com
ProxyPreserveHost On
SSLProxyEngine On
ProxyPass / http://localhost:1337/
ProxyPassReverse / http://localhost:1337/
ProxyPreserveHost On
SSLCertificateFile /etc/letsencrypt/live/yourdomian-name.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/yourdomian-name.com/privkey.pem
2nd Changes
Edit this /etc/apache2/sites-available/000-default-le-ssl.conf
SSLCertificateFile /etc/letsencrypt/live/yourdomian-name.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/yourdomian-name.com/privkey.pem
ServerName yourdomian-name.com
ProxyPreserveHost On
SSLProxyEngine On
ProxyPass / http://localhost:1337/
ProxyPassReverse / http://localhost:1337/
ProxyPreserveHost On
then restart apache2
sudo /etc/init.d/apache2 restart
Check https://yourdomian-name.com

Related

Apache2 Proxy Websocket connections

i followed many tutorials on how to add reverse proxy on Nodejs applications, i installed one on my VPS that uses websocket, this is my apache2 config.
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerName tracker.local.dev
ServerAdmin webmaster#localhost
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
ProxyPreserveHost On
ProxyRequests off
ProxyPass / http://127.0.0.1:8085/
ProxyPassReverse / http://127.0.0.1:8085/
SSLProxyEngine On
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/tracker.local.dev/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/tracker.local.dev/privkey.pem
</VirtualHost>
# vim: syntax=apache ts=4</VirtualHost>
</IfModule>
I can connect to the NodeJS App but it won't connect the Websocket, i already enabled every module and checked everything on my config, but i can't make it work.

Using wordpress for /blog route in nodejs website not working

I created a vhost like this in my apache2 server configuration:
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerName test.co
ServerAdmin webmaster#localhost
#wordpress
Alias /blog "/var/www/test_wp/public_html"
<Directory "/var/www/test_wp/public_html">
Options None
AllowOverride None
Order allow,deny
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
ProxyPreserveHost On
ProxyPass / http://localhost:8080/
ProxyPassReverse / http://localhost:8080/
SSLCertificateFile /etc/letsencrypt/live/test.co/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/test.co/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
</IfModule>
I'm using alias to redirect /blog url (test.co/blog) to wordpress folder in my server.
i'm using proxypass for the nodejs website.
But when i hit test.co/blog , it shows 404 not found, in my nodejs website
Follow this: Exclude an alias from virtualhost proxypass
Add this line :
ProxyPassMatch ^/blog !
ProxyPass / http://localhost:8080/
ProxyPassReverse / http://localhost:8080/

Serving a NodeJS app via apache

I am running a nodejs application on my apache2 web server. On trying to access the domain page example.com it shows the default apache page instead of the apps page. But on going to example.com:8090 it show the node application
This is the config
<VirtualHost *80>
ServerName example.com
ServerAlias www.example.com
ProxyRequests off
ProxyPreserveHost On
ProxyVia Full
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
<Location />
ProxyPass http://serverip:8090
ProxyPassReverse http://serverip:8090
</Location>
</VirtualHost>
This will work, just know it's not ideologically the most elegant solution.
# Main site proxy
<VirtualHost *:80>
ServerAlias www.example.com
ServerName example.com
ProxyPreserveHost On
<Proxy *>
Order allow,deny
Allow from all
</Proxy>
ProxyPass / http://nodejsIP:8090/
ProxyPassReverse / http://nodejsIP:8090/
</VirtualHost>
# Subdomain proxies
<VirtualHost *:80>
ServerAlias www.api.example.com
ServerName api.example.com
ProxyPreserveHost On
<Proxy *>
Order allow,deny
Allow from all
</Proxy>
ProxyPass / http://nodejsIP:8090/api/
ProxyPassReverse / http://nodejsIP:8090/api/
</VirtualHost>
<VirtualHost *:80>
ServerAlias www.apps.example.com
ServerName apps.example.com
ProxyPreserveHost On
<Proxy *>
Order allow,deny
Allow from all
</Proxy>
ProxyPass / http://nodejsIP:8090/apps/
ProxyPassReverse / http://nodejsIP:8090/apps/
</VirtualHost>
Note: if your apache server and node server are both running off the same box and hence have the same IP, use http://localhost for the nodejsIP

Apache2 redirect subdirectory to port

If I redirect from the root directory to a different port everything works fine with this configuration:
<VirtualHost *:80>
ProxyPreserveHost On
ProxyRequests Off
ServerName www.example.com
ServerAlias example.com
ProxyPass / http://localhost:8080/
ProxyPassReverse / http://localhost:8080/
</VirtualHost>
http://www.example.com/ -> http://www.example.com:8080/
But if i try the followig configuration to redirect from a subdirectory to a different port it redirects to port 80.
<VirtualHost *:80>
ProxyPreserveHost On
ProxyRequests Off
ServerName www.example.com
ServerAlias example.com
ProxyPass /example http://localhost:8080/example/
ProxyPassReverse /example http://localhost:8080/example/
</VirtualHost>
http://www.example.com/example -> http://www.example.com:80/example/
What's wrong with this configuration?

AWS EC2 port opened, but still not accessible

I have port TCP 3000 enabled, it even shows enabled under the EC2 security settings.
Although I can not telnet into my server via that port, I'm getting Could not open connection to the host, on port 3000: Connect failed.
I'm running an Ubuntu 12.04 AWS EC2 Micro instance in Oregon.
I'm running apache on the server to host http(s) (80/443) traffic and am trying to host an additional app through port 3000 via nodejs express.
Here is my Apache sites-enabled file:
<VirtualHost *:80>
ServerAdmin name#domain.com
ServerName www.domain.com
ServerAlias www.domain.com
DocumentRoot /var/www/
ProxyRequests off
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
<Location />
ProxyPass http://localhost:80/
ProxyPassReverse http://localhost:80/
</Location>
</VirtualHost>
<VirtualHost *:80>
ServerAdmin name#domain.com
ServerName lobby.domain.com
ServerAlias lobby.domain.com
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
<Location />
ProxyPass http://localhost:3000/
ProxyPassReverse http://lobby.domain.com/
</Location>
</VirtualHost>
This is what you want on your reverse proxy configs:
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin name#domain.com
ServerName www.domain.com
ServerAlias www.domain.com
DocumentRoot /var/www/
</VirtualHost>
<VirtualHost *:80>
ServerAdmin name#domain.com
ServerName lobby.domain.com
ServerAlias lobby.domain.com
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
<Location />
ProxyPass http://127.0.0.1:3000/
ProxyPassReverse http://127.0.0.1:3000/
</Location>
</VirtualHost>
Make sure you enable the apache proxy modules, from the command line:
a2enmod proxy
a2enmod proxy_connect
a2enmod proxy_http
a2enmod proxy_balancer
service apache2 restart
Make sure you are binding your application on 0.0.0.0 instead of 127.0.0.1 or localhost. It sounds like your application is not listening to IP addresses outside of the server.
(Also make sure your node.js application is running too)
To listed on 0.0.0.0 you need to so something like this in your application:
var app = connect().use(connect.static('public')).listen(3000, "0.0.0.0");

Resources