Skip to content

Instantly share code, notes, and snippets.

@amitkhare
Last active May 9, 2020 19:56
Show Gist options
  • Save amitkhare/8d98d91321654e2739d58f47c5ad5dcf to your computer and use it in GitHub Desktop.
Save amitkhare/8d98d91321654e2739d58f47c5ad5dcf to your computer and use it in GitHub Desktop.
Apache2 Reverse Proxy with web-sockets and SSL
<VirtualHost *:80>
ServerName sub.example.com
ProxyPreserveHost On
SSLProxyEngine On
RewriteEngine on
RewriteCond %{HTTP:Upgrade} websocket [NC]
RewriteCond %{HTTP:Connection} upgrade [NC]
RewriteRule .* "ws://localhost:8080%{REQUEST_URI}" [P]
ProxyPass / http://localhost:8080/
ProxyPassReverse / http://localhost:8080/
ProxyRequests off
<Location "/">
AuthType Basic
AuthUserFile /etc/apache2/.htpasswd
AuthName "Secured Access"
Require valid-user
</Location>
<IfModule mod_ssl.c>
RewriteCond %{SERVER_NAME} =sub.example.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</IfModule>
</VirtualHost>
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerName sub.example.com
ProxyPreserveHost On
SSLProxyEngine On
RewriteEngine on
RewriteCond %{HTTP:Upgrade} websocket [NC]
RewriteCond %{HTTP:Connection} upgrade [NC]
RewriteRule .* "wss://localhost:8080%{REQUEST_URI}" [P]
ProxyPass / https://127.0.0.1:8080/
ProxyPassReverse / https://127.0.0.1:8080/
ProxyRequests off
<Location "/">
AuthType Basic
AuthUserFile /etc/apache2/.htpasswd
AuthName "Secured Access"
Require valid-user
</Location>
SSLCertificateFile /etc/letsencrypt/live/sub.example.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/sub.example.com/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
</IfModule>
@amitkhare
Copy link
Author

replace

sub.example.com

:8080

@amitkhare
Copy link
Author

./code-server --bind-addr localhost:8080--cert /etc/letsencrypt/live/sub.example.com/fullchain.pem --cert-key /etc/letsencrypt/live/sub.example.com/privkey.pem                      

@amitkhare
Copy link
Author

Setup Apache Basic Auth

$ sudo apt-get update
$ sudo apt-get install apache2-utils

Step 2

sudo htpasswd -c /etc/apache2/.htpasswd <USER>
# for securing Directory
<Directory "/var/www/html">
        AuthType Basic
        AuthUserFile /etc/apache2/.htpasswd
        AuthName "Secured Access"
        Require valid-user
</Directory>

# for securing url path
    <Location "/">
        AuthType Basic
        AuthUserFile /etc/apache2/.htpasswd
        AuthName "Secured Access"
        Require valid-user
    </Location>

@amitkhare
Copy link
Author

vscode.sh detached

#!/bin/bash
cat /code-server/last | while read line
do
  kill $line
  echo "previous instance of vscode pid: $line was killed"
done

/code-server/code-server \
--bind-addr localhost:8080 \
--auth none \
--cert /etc/letsencrypt/live/sub.example.com/fullchain.pem \
--cert-key /etc/letsencrypt/live/sub.example.com/privkey.pem \
</dev/null &>/dev/null & \

echo "creating new instance with pid: $!"

echo $! > /code-server/last

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment