apache proxy configuration for socket.io (project not in root) - node.js

I know there are several questions like this but i can't find one solving my problem for a project which is not in the server root.
I have the following situation: I have an Apache server which runs several projects like:
0.0.0.0/project1
0.0.0.0/project2
I added a node.js project let's call it nodeproject. With ProxyPass I am passing requests made to 0.0.0.0/nodeproject to 0.0.0.0:8080 where my node server is running. This is all working fine except the websocket configuration for socket.io.
In my /etc/apache2/apache2.conf file i placed this code to do the proxying. mod_proxy_wstunnel is enabeld Which i basically copied from here.
ProxyRequests off
ProxyVia on
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/nodeproject/socket.io [NC]
RewriteCond %{QUERY_STRING} transport=websocket [NC]
RewriteRule /(.*) ws://localhost:8080/$1 [P,L]
ProxyPass /nodeproject/socket.io http://localhost:8080/socket.io
ProxyPassReverse /nodeproject/socket.io http://localhost:8080/socket.io
<Location /nodeproject>
ProxyPass http://localhost:8080
ProxyPassReverse http://localhost:8080
</Location>
The failing request looks like this:
ws://0.0.0.0/nodeproject/socket.io/?EIO=3&transport=websocket&sid=MDuVcHmt3T1sa8ruAAAa
and i get a Bad Request 400 response.
I am not an expert in configuring this kind of stuff where did I go wrong?
Thanks in advance!

You shouldn't put the rewrite conditional in the /etc/apache2/apache2.conf, but in the virtual host directive in /etc/apache2/sites-available/000-project-whatever.conf.
Here's an example from one of my own little projects.
<VirtualHost *:80>
ServerName sockey.api
ServerAdmin webmaster#localhost
DocumentRoot /var/www/sockey.api
<Directory /var/www/sockey.api>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
ErrorDocument 404 /index.html
ErrorLog ${APACHE_LOG_DIR}/sockey.api.error.log
CustomLog ${APACHE_LOG_DIR}/sockey.api.access.log combined
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/socket.io [NC]
RewriteCond %{QUERY_STRING} transport=websocket [NC]
RewriteRule /(.*) ws://localhost:8081/$1 [P,L]
ProxyPass /socket.io http://localhost:8081/socket.io
ProxyPassReverse /socket.io http://localhost:8081/socket.io
</VirtualHost>

I was able to use Apache as a proxy for socket.io using this configuration:
RewriteEngine On
RewriteCond %{QUERY_STRING} transport=polling [OR]
RewriteCond %{REQUEST_URI} /socket.io/socket.io.js
RewriteRule /socket.io/(.*)$ http://localhost:8082/socket.io/$1 [P]
ProxyPass /socket.io/ ws://localhost:8082/socket.io/
ProxyPassReverse /socket.io/ ws://localhost:8082/socket.io/
It takes into account that there are two types of requests going over https: the request for the js file socket.io.js itself, and the polling requests.

Related

Redirecting from Http to Https with AWS's ELB

I have made queries before here. I considered it was working, but actually not yet.
I have load balancer facing to Internet and behind is Ec2 instance.
My load balancer is opened to port 80 and port 443.
They are arranged as 80 -> 80 and 443 -> 80.
Now is I need to redirect http access to https.
What I did is
I made a .htaccess file and located into /var/www/html/. That is the folder where my website's index.php is located.
Inside .htaccess file, I tried a few version what I found in the Internet. None of them worked.
<VirtualHost *:80>
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} =http
RewriteCond %{HTTP_USER_AGENT} !^ELB-HealthChecker
RewriteRule . https://%{HTTP:Host}%{REQUEST_URI} [L,R=permanent]
</VirtualHost>
<VirtualHost *:80>
RewriteEngine on
RewriteCond %{HTTP_HOST} www.(.+) [OR,NC] # Added
RewriteCond %{HTTP:X-Forwarded-Proto} !https [NC]
RewriteRule ^/?(.*) https://domainname.com%{REQUEST_URI} [L,R=301]
</VirtualHost>
Why it doesn't work at my set up?
EDIT:
<If "req('X-Forwarded-Proto') != 'https'>
RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} !^ELB-HealthChecker
RewriteRule . https://%{HTTP:Host}%{REQUEST_URI} [L,R=permanent]
</If>
What I do for HTTPS with apache is upload a custom redirect.conf file via container commands. So redirect.conf looks something like this:
<If "req('X-Forwarded-Proto') != 'https'>
RewriteEngine On
RewriteRule . https://%{SERVER_NAME}%{REQUEST_URI} [L,R]
</If>
And then you'd use a container command to put this file into the apache configuration directory /etc/httpd/conf.d/ like so:
container_commands:
01_update_rewrite_rules:
command: "cp rewrite.conf /etc/httpd/conf.d/"
I have been solving the issue for a long time and now can make it work successfully. I like to share what I have done as follows.
(1)Create .htaccess file inside the folder same as root file index.php exists
(2).htaccess file has
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} =http
RewriteRule . https://%{HTTP:Host}%{REQUEST_URI} [L,R=permanent]
(3)Then enable mod_rewrite, try the command line command:
sudo a2enmod rewrite
(4)To make sure mod_rewrite is enabled, update /etc/apache2/apache2.conf file as
<Directory /var/www/html>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
initially is
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
Then you can setup http to https successfully
You need to restart server as sudo service apache2 restart after update of /etc/apache2/apache2.conf
Your load balancer and its security group need to open to http access.

Apache redirect from https

I installed an SSL Certificate on my server, and https works fine.
Http redirect works fine for example http://login.example.com -> https://example1.com/index.html
But redirect from https not working, for example https://login.example.com -> https://example1.com/index.html.
its just go to the main page of my server https://redirect.example.com
Any suggestions on how to get the redirect from https to work?
Thanks !
<VirtualHost *:80>
RewriteEngine On
ServerName redirect.example.com
RewriteCond %{HTTP_HOST} ^login\.example\.com(.*)
RewriteRule (.*) https://example1.com/index.html [L]
</VirtualHost>
<VirtualHost *:443>
SSLEngine on
SSLCertificateFile /etc/httpd/my.crt
SSLCertificateKeyFile /etc/httpd/my.pem
ServerName redirect.mydomain.com
RewriteEngine On
ServerName redirect.example.com
RewriteCond %{HTTP_HOST} ^login\.example\.com(.*)
RewriteRule (.*) https://example1.com/index.html [L]
</VirtualHost>
Thanks!
I see that you have a duplicate ServerName directive in your SSL host declaration.
That could be causing trouble.
From the Apache documentation:
If you are using name-based virtual hosts, the ServerName inside a section specifies what hostname must appear in the request's Host: header to match this virtual host.
Use server alias for your Rewrite Rule:
ServerAlias login.example.com
you can use multiple ServerAlias for multiple Rewrite Rule
What exactly do you mean by multiple rules ? Multiple domain names/subdomains ?
A plain redirect of http(s)://login.example.com to https://example1.com/index.html should be achieved this way:
<VirtualHost *:80>
ServerName login.example.com
RewriteEngine On
RewriteCond %{HTTP_HOST} ^login\.example\.com
RewriteRule (.*) https://example1.com/index.html [L]
</VirtualHost>
<VirtualHost *:443>
SSLEngine on
SSLCertificateFile /etc/httpd/my.crt
SSLCertificateKeyFile /etc/httpd/my.pem
ServerName login.example.com
RewriteEngine On
RewriteCond %{HTTP_HOST} ^login\.example\.com
RewriteRule (.*) https://example1.com/index.html [L]
</VirtualHost>
Important: ServerName should match your SSL certificate's Common Name (unless it's a wildcard certificate).
NB: RewriteEngine may be overkill for your purpose since the Apache mod_alias module provides the Redirect directive which may be sufficient here.
From the Apache documentation: When not to use mod_rewrite

Return 404 for the /web directory Symfony 3

All my links are accessibles by mysite.com but also with mysite.com/web/
I would not like to redirect /web to / but, i would like to return a 404 for ^/web. I think it's possible cuz i saw a lot of symfony projects which return 404 error for the /web path
Thanks you all !
You need to modify your apache configuration so the document root points to /web folder.
Your apache config file needs to look something similar to this:
<VirtualHost *:80>
ServerName www.mysite.com
ServerAlias mysite.com
ServerAdmin contact#mysite.com
RewriteEngine On
DocumentRoot your_site_path/web
<Directory your_site_path/web/>
Options FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^app\.php(/(.*)|$) %{ENV:BASE}/$2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .? - [L]
RewriteRule .? %{ENV:BASE}/app.php [L]
</IfModule>
</Directory>
</VirtualHost>

force https on subdirectory and redirect non-www traffic to www

So for the past couple hours I've been frustrated with my .htaccess file not working.
I have two subdirectories: secure.mysite.ca and storage.mysite.ca that need to be both ssl encrypted. The sites work when I put https in front of the URL (i.e. https://secure.mysite.ca/ or https://storage.mysite.ca/) but when I put http in front it redirects to my root website (http://mysite.ca/). I want to redirect to the https version of the site when someone tries to go to the unencrypted version.
ALSO I would like to redirect all traffic visiting http://mysite.ca/ to be redirected to http://www.mysite.ca/ (i.e. www in front)
This .htaccess file used to work over a year ago but now it doesn't.
Here is my htaccess file found in the root folder:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^mysite\.ca$ [NC]
RewriteRule ^(.*)$ http://www.mysite.ca%{REQUEST_URI} [R=permanent,L]
RewriteCond %{SERVER_PORT} !^443$
RewriteCond %{HTTP_HOST} ^(secure|storage)\. [NC]
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
Am I doing something wrong? Should I be putting an .htaccess file inside the subdirectory?
EDIT: Here is the virtualhost config file for secure.fixnode.ca, storage.fixnode.ca and www.fixnode.ca. Please let me know what I'm doing wrong
storage.fixnode.ca.conf
<VirtualHost *:443>
DocumentRoot "/var/www/fixnode_website/content/Online Storage"
<Directory "/var/www/fixnode_website/content/Online Storage">
allow from all
Options +Indexes
</Directory>
SSLEngine on
ServerName storage.fixnode.ca
SSLCertificateFile /path/to/cert/storage.crt
SSLCertificateKeyFile /path/to/private key/private.key
</VirtualHost>
secure.fixnode.ca.conf
<VirtualHost *:443>
DocumentRoot "/var/www/fixnode_website/content/Secure Login"
<Directory "/var/www/fixnode_website/content/Secure Login">
allow from all
Options +FollowSymlinks
</Directory>
SSLEngine on
ServerName secure.fixnode.ca
SSLCertificateFile /path/to/cert/cert.crt
SSLCertificateKeyFile /path/to/cert/mykey.key
</VirtualHost>
and finally my virtual host for my root domain www.fixnode.ca.conf
<VirtualHost *:80>
DocumentRoot /var/www/fixnode_website
<Directory "/var/www/fixnode_website">
allow from all
Options +Indexes
</Directory>
ServerAlias fixnode.ca
ServerName www.fixnode.ca
</VirtualHost>
If you have recently made a change to your system, you may have overwritten your apache config file(e.g. httpd.conf or apache2.conf) which may reset the options to default.
You need to place this rule in both the sub-directories of both sub-domains:
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(secure|storage)\. [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Drupal 7 rewrite not using .htaccess

I'm trying to enable the rewrite function in drupal 7 without using the .htaccess file since it should only be used if you do not have root access to the server (accordingly to apache's website)
Without any further adieu, here is my configuration
<VirtualHost *:80>
ServerName www.example.com
ServerAlias example.com
DirectoryIndex index.php
DocumentRoot /var/www/example.com/public
ErrorDocument 404 /index.php
LogLevel warn
ErrorLog /var/www/example.com/log/error.log
CustomLog /var/www/example.com/log/access.log combined
<Directory "/var/www/example.com/public">
Options -Indexes +FollowSymLinks +ExecCGI
AllowOverride None
Order allow,deny
Allow from all
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ /index.php?q=$1 [L,QSA]
</Directory>
</VirtualHost>
The rewrite configuration worked when I had it in the .htaccess file but not when I put it inside the vhost file.
Can you guys see anything wrong here?
I think your rewrite rule should not contain the '/' prefix now that it's not a .htaccess anymore. Soo try with:
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
Removing .htaccess reference is a good idea. You should put the AllowOverride None on a <Directory /> instruction so that this start from the root of the filesystem. If think you do not need the +ExecCGI also. If you want to go deeper in apache config for Drupal without .htaccess check also this detailled resource.

Resources