I have an application using angular and nodejs for the frontend.
I have integrated a chat app using socket.io for nodejs, in my localhost it works fine.
For the deployment, I have configured two subdomains one for the backend and the other for the angular app to communicate safely.
I have added an SSL Certificate for the backend subdomain “backendapi.app.com” and the subdomain for the app “backoffice.app.com” .
I have doing that to configure cors and access api via frontend with safely mode and all works fine.
When I deployed my new version with the chat app, an error showed on the console :
GET https://backendapi.app.com/socket.io/?EIO=4&transport=polling&t=P7SX7Ir
My apache file for the backendapi.app.conf is :
<VirtualHost *:80>
ServerName backendapi.app.com
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
<Directory /var/www/html>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
# Configuration du proxy inverse pour les API
ProxyPreserveHost On
ProxyPass /api http://127.0.0.1:3000/api
ProxyPassReverse /api http://127.0.0.1:3000/api
# Configuration du proxy pour les WebSockets
ProxyPass /socket.io/ http://127.0.0.1:3000/socket.io/
ProxyPassReverse /socket.io/ http://127.0.0.1:3000/socket.io/
# En-têtes CORS et gestion des requêtes OPTIONS pour les WebSockets
<Location /socket.io>
Header always set Access-Control-Allow-Origin "https://backoffice.app.com"
Header always set Access-Control-Allow-Methods "GET, POST, OPTIONS"
Header always set Access-Control-Allow-Headers "Origin, Content-Type, Accept, Authorization"
Header always set Access-Control-Allow-Credentials "true"
</Location>
# Logs
ErrorLog ${APACHE_LOG_DIR}/backendapi_app_error.log
CustomLog ${APACHE_LOG_DIR}/backendapi_app_access.log combined
# Redirection HTTP vers HTTPS
RewriteEngine on
RewriteCond %{SERVER_NAME} =backendapi.app.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
and the with the SSL :
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerName backendapi.app.com
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
<Directory /var/www/html>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
# Configuration CORS
Header set Access-Control-Allow-Origin "*"
Header set Access-Control-Allow-Methods "GET, POST, PUT, DELETE, OPTIONS"
Header set Access-Control-Allow-Headers "Content-Type, Authorization"
</Directory>
# Configuration du proxy inverse
ProxyPreserveHost On
ProxyPass /api http://127.0.0.1:3000/api
ProxyPassReverse /api http://127.0.0.1:3000/api
# En-têtes CORS
<Location /api>
Header set Access-Control-Allow-Origin "*"
Header set Access-Control-Allow-Methods "GET, POST, PUT, DELETE, OPTIONS"
Header set Access-Control-Allow-Headers "Origin, Content-Type, Accept, Authorization"
</Location>
# En-têtes CORS pour les WebSockets
<Location /socket.io>
Header always set Access-Control-Allow-Origin "https://backoffice.app.com"
Header always set Access-Control-Allow-Methods "GET, POST, OPTIONS"
Header always set Access-Control-Allow-Headers "Origin, Content-Type, Accept, Authorization"
Header always set Access-Control-Allow-Credentials "true"
</Location>
ErrorLog ${APACHE_LOG_DIR}/backendapi_app_error.log
CustomLog ${APACHE_LOG_DIR}/backendapi_app_access.log combined
SSLCertificateFile /etc/letsencrypt/live/backendapi.app.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/backendapi.app.com/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
</IfModule>