huashengdun / webssh

:seedling: Web based ssh client
https://webssh.huashengdun.org/
MIT License
4.57k stars 1.31k forks source link

deployment within apache as reverse proxy #52

Open konstantix opened 5 years ago

konstantix commented 5 years ago

hey man, just a quick information, maybe it's useful for someone else. I run an Apache https webserver as reverseproxy and had some difficulties with the connection upgrade to websockets - but now it's running correct with the following config:

activate needed apache modules: a2enmod proxy proxy_http proxy_html proxy_wstunnel filter rewrite substitute

# webssh.conf
#
# https://mydomain.org/ssh 
# Config for WebSSH Portal
##

<Location /ssh >
    ProxyPass  http://127.0.0.1:8888
        ProxyPassReverse http://127.0.0.1:8888

# for websocket stuff
    RewriteEngine On
    RewriteCond %{HTTP:Upgrade}  ^WebSocket$ [NC, OR]
        RewriteCond %{HTTP:CONNECTION} Upgrade$ [NC, OR]
        RewriteRule .*  ws://localhost:8888/ws$1 [P,L]

# rewrite response html to fix paths
        AddOutputFilterByType SUBSTITUTE text/html
        Substitute "s|static/js/|/ssh/static/js/|i"
        Substitute "s|static/css/|/ssh/static/css/|i"
        Substitute "s|static/img/|/ssh/static/img/|i"
</Location>

Also i created a systemd service to ensure webssh is running vi /etc/systemd/system/webssh.service with following content:

[Unit]
Description=Python WebSSH 
After=syslog.target network.target
StartLimitBurst=5, StartLimitIntervalSec=10

[Service]
ExecStart=/path/to/webssh_env/bin/wssh --address=127.0.0.1 --origin=https://mydomain.org  --port=8888 --log-file-prefix=/var/log/webssh.log 
ExecReload=/bin/kill -HUP $MAINPID
Type=simple
PIDFile=/var/run/webssh.pid
Restart=on-failure
RestartSec=1

[Install]
WantedBy=multi-user.target

systemctl daemon-reload && systemctl enable webssh.service && systemctl start webssh.service

regards!

huashengdun commented 5 years ago

Hi konstantix,

Thanks for your useful information, especially for Apache users.

varneyc commented 5 years ago

Placing this in my server virtualhost file gives the configuration error RewriteCond: bad flag delimiters Any idea what I've done wrong occurs?

My server configuration (/etc/apache2/sites-enabled/example.com-le-ssl.conf):

<IfModule mod_ssl.c>
<VirtualHost *:443>
        # The ServerName directive sets the request scheme, hostname and port that
        # the server uses to identify itself. This is used when creating
        # redirection URLs. In the context of virtual hosts, the ServerName
        # specifies what hostname must appear in the request's Host: header to
        # match this virtual host. For the default virtual host (this file) this
        # value is not decisive as it is used as a last resort host regardless.
        # However, you must set it for any further virtual host explicitly.

        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html

        # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
        # error, crit, alert, emerg.
        # It is also possible to configure the loglevel for particular
        # modules, e.g.
        #LogLevel info ssl:warn

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

        # For most configuration files from conf-available/, which are
        # enabled or disabled at a global level, it is possible to
        # include a line for only one particular virtual host. For example the
        # following line enables the CGI configuration for this host only
        # after it has been globally disabled with "a2disconf".
        #Include conf-available/serve-cgi-bin.conf

        <Location /ssh >
                ProxyPass  http://127.0.0.1:8888
                ProxyPassReverse http://127.0.0.1:8888

# for websocket stuff
                RewriteEngine On
                RewriteCond %{HTTP:Upgrade} ^WebSocket$ [NC, OR]
                RewriteCond %{HTTP:CONNECTION} Upgrade$ [NC, OR]
                RewriteRule .* ws://localhost:8888/ws$1 [P,L]

# rewrite response html to fix paths
                AddOutputFilterByType SUBSTITUTE text/html
                Substitute "s|static/js/|/ssh/static/js/|i"
                Substitute "s|static/css/|/ssh/static/css/|i"
                Substitute "s|static/img/|/ssh/static/img/|i"
        </Location>

ServerName example.com
Include /etc/letsencrypt/options-ssl-apache.conf
ServerAlias www.example.com
SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
</VirtualHost>
</IfModule>
varneyc commented 5 years ago

Problem was the spaces in the flags are no longer allowed - [NC,OR] rather than [NC, OR] if anyone else has the same problem.

Cheers.

tevkar commented 4 years ago

For more recent versions of Apache: Replace AddOutputFilterByType SUBSTITUTE text/html

By

FilterDeclare MYFILTER
FilterProvider MYFILTER SUBSTITUTE "%{Content_Type} = 'text/html'"
FilterChain MYFILTER
tevkar commented 4 years ago

Yet another working example as a virtual host:

<IfModule mod_ssl.c>
<VirtualHost *:443>
        ServerName  <hostname>

        ProxyPass / http://<ip-address>:8888/
        ProxyPassReverse / http://<ip-address>:8888/

        RewriteEngine On
        RewriteCond %{HTTP:Upgrade} websocket [NC]
        RewriteCond %{HTTP:Connection} upgrade [NC]
        RewriteRule ^/?(.*) "ws://<ip-address>:8888/$1" [P,L]

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

SSLCertificateFile /etc/letsencrypt/live/<hostname>/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/<hostname>/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
</IfModule>
Jamstah commented 3 years ago

If you get an error about Cross origin not being allowed, add ProxyPreserveHost On to your config.