Hi Everyone,
We are new users of Traefik and currently are stuck on how to redirect our request.
We are trying to access to Rstudio on port 8787 with a pathprefix /rstudio.
When we were using Nginx we had this trick :
server {
listen 80;
location /rstudio/ {
rewrite ^/rstudio/(.*)$ /$1 break;
proxy_pass http://localhost:8787;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_read_timeout 20d;
# Use preferably
proxy_set_header X-RStudio-Request $scheme://$host:$server_port$request_uri;
proxy_set_header X-RStudio-Root-Path /rstudio
# OR let the proxy rewrite the root path
proxy_redirect http://localhost:8787/ $scheme://$host/rstudio/;
proxy_cookie_path / /rstudio;
# OR existing X-Forwarded headers
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Proto $scheme;
# OR alternatively the Forwarded header (just an example)
proxy_set_header Forwarded "host=$host:$server_port;proto=$scheme;";
}
Now, we would like to tranfer it to Traefik. We have tried several configuration but with have no success on it.
Here's our Traefik Docker-compose line:
app1-rstudio:
container_name: app1-rstudio
image: rstudio
restart: always
privileged: true
networks:
- app1-network
ports:
- "6311:6311"
- "8787:8787"
environment:
- USER=rstudio
- PASSWORD=rstudio
volumes:
- ${APPDIR}filesapp1/scripting/rserver/:/home/rstudio/
- ${APPDIR}filesapp1/log/:/var/log/
- ${APPDIR}filesapp1/log/rserver/:/var/log/rserver/
labels:
- "traefik.http.routers.app1-rstudio.rule=Host(`localhost`) && PathPrefix(`/rstudio`)"
- "traefik.http.services.app1-rstudio.loadbalancer.server.port=8787"
- "traefik.http.routers.app1-rstudio.middlewares=app1-rstudio-replacepathregex"
- "traefik.http.middlewares.app1-rstudio-replacepathregex.replacepathregex.regex=^/rstudio/(.*)$$"
- "traefik.http.middlewares.app1-rstudio-replacepathregex.replacepathregex.replacement=/$$1"
- "traefik.enable=true"
Do you have any idea ?
Thank you for your support on that community.
Nooby Devops Team
1 Like
Bump, I am also struggling to get this working.
mrtn
June 20, 2023, 10:57pm
3
To anyone reading this in 2023:
Refer to this Github issue, set the custom header and be happy:
opened 10:41PM - 14 Mar 22 UTC
closed 05:46PM - 21 Dec 22 UTC
enhancement
help wanted
### What is this issue about?
Some users have reported difficulties to setup … RStudio server with Traefik reverse proxy using a path prefix (e.g., www.example.com/rstudio). The reference discussions are linked below:
- https://community.traefik.io/t/traefik-and-rstudio/9003
- https://community.traefik.io/t/traefik-and-rstudio-reverse-proxy/10304
The problem occurs because not only Traefik, but also Rstudio server must be configured accordingly.
### How to test it?
#### 1. Basic example: does NOT work
_docker-compose.yml_
````yml
version: "3.3"
services:
traefik:
image: "traefik:v2.6"
container_name: "traefik"
command:
#- "--log.level=DEBUG"
- "--api.insecure=true"
- "--providers.docker=true"
- "--providers.docker.exposedbydefault=false"
- "--entrypoints.web.address=:80"
ports:
- "80:80"
- "8080:8080"
volumes:
- "/var/run/docker.sock:/var/run/docker.sock:ro"
# RStudio server
rstudio:
image: "rocker/rstudio:latest"
# IMPORTANT: this name MUST be used in traefik's routers service
container_name: "rstudio-server"
restart: "unless-stopped"
environment:
- PASSWORD=rstudio
labels:
- "traefik.enable=true"
- "traefik.http.routers.rstudio-server.rule=Host(`localhost`) && PathPrefix(`/rstudio`)"
- "traefik.http.routers.rstudio-server.entrypoints=web"
- "traefik.http.services.rstudio-server.loadbalancer.server.port=8787"
````
1. Open a Terminal window (e.g., Bash) and run the following command:
`docker-compose up -d`
1. Open a Web browser window and visit the URL http://localhost/rstudio. The login screen will not be rendered as expected.
<img width="689" alt="Screen Shot 2022-03-14 at 09 29 14" src="https://user-images.githubusercontent.com/4059310/158252984-8a5830e0-5152-41eb-b5c1-ba8b54dc296d.png">
1. To stop it:
`docker-compose down`
#### 2. Stripping prefix (Traefik): still does NOT work
_docker-compose.yml_
````yml
version: "3.3"
services:
traefik:
image: "traefik:v2.6"
container_name: "traefik"
command:
#- "--log.level=DEBUG"
- "--api.insecure=true"
- "--providers.docker=true"
- "--providers.docker.exposedbydefault=false"
- "--entrypoints.web.address=:80"
ports:
- "80:80"
- "8080:8080"
volumes:
- "/var/run/docker.sock:/var/run/docker.sock:ro"
# RStudio server
rstudio:
image: "rocker/rstudio:latest"
# IMPORTANT: this name MUST be used in traefik's routers service
container_name: "rstudio-server"
restart: "unless-stopped"
environment:
- PASSWORD=rstudio
labels:
- "traefik.enable=true"
- "traefik.http.routers.rstudio-server.rule=Host(`localhost`) && PathPrefix(`/rstudio`)"
- "traefik.http.routers.rstudio-server.entrypoints=web"
- "traefik.http.middlewares.rstudio-server-path-strip.stripprefix.prefixes=/rstudio"
- "traefik.http.middlewares.rstudio-server-path-strip.stripprefix.forceSlash=false"
- "traefik.http.routers.rstudio-server.middlewares=rstudio-server-path-strip"
- "traefik.http.services.rstudio-server.loadbalancer.server.port=8787"
````
1. Open a Terminal window and run the following command:
`docker-compose up -d`
1. Open a Web browser window and visit the URL http://localhost/rstudio. Although Rstudio server manages to find the entry path, the login screen is not rendered as expected (it cannot find resources).
<img width="689" alt="Screen Shot 2022-03-14 at 09 42 12" src="https://user-images.githubusercontent.com/4059310/158253079-abae6514-9531-42c3-91c3-8d02baeda674.png">
1. To stop it:
`docker-compose down`
### How to fix it?
The problem occurs because RStudio server does not know what path it is being served from. One can find all RStudio Server Configuration [here](https://docs.rstudio.com/ide/server-pro/latest/rstudio-server-configuration.html). In order to fix this issue it is necessary to set the www Setting `www-root-path` in _`/etc/rstudio/rserver.conf`_ to match the proxy prefix (e.g., /rstudio).
_wwwsettings.sh_
````bash
#!/usr/bin/with-contenv bash
## RStudio Server Configuration
## https://docs.rstudio.com/ide/server-pro/latest/rstudio-server-configuration.html
## www Settings
# www-root-path: The path prefix added by a proxy to the incoming RStudio URL.
# This setting is used so RStudio Server knows what path it is being served
# from. If running RStudio Server behind a path-modifying proxy, this should
# be changed to match the base RStudio Server URL.
if [ -n "$WWW_ROOT_PATH" ]; then
# echo "www-root-path=$WWW_ROOT_PATH"
echo "www-root-path=$WWW_ROOT_PATH" >> /etc/rstudio/rserver.conf
fi
````
_docker-compose.yml_ (**NOTE: /home/dev/wwwsettings.sh is an absolute path on the host machine and must be modified accordingly**)
````yml
version: "3.3"
services:
traefik:
image: "traefik:v2.6"
container_name: "traefik"
command:
#- "--log.level=DEBUG"
- "--api.insecure=true"
- "--providers.docker=true"
- "--providers.docker.exposedbydefault=false"
- "--entrypoints.web.address=:80"
ports:
- "80:80"
- "8080:8080"
volumes:
- "/var/run/docker.sock:/var/run/docker.sock:ro"
# RStudio server
rstudio:
image: "rocker/rstudio:latest"
# IMPORTANT: this name MUST be used in traefik's routers service
container_name: "rstudio-server"
restart: "unless-stopped"
environment:
- PASSWORD=rstudio
# Set the path prefix added by Traefik to the incoming RStudio URL.
# https://docs.rstudio.com/ide/server-pro/latest/rstudio-server-configuration.html
- WWW_ROOT_PATH=/rstudio
volumes:
- "/home/dev/wwwsettings.sh:/etc/cont-init.d/wwwsettings"
labels:
- "traefik.enable=true"
- "traefik.http.routers.rstudio-server.rule=Host(`localhost`) && PathPrefix(`/rstudio`)"
- "traefik.http.routers.rstudio-server.entrypoints=web"
- "traefik.http.middlewares.rstudio-server-path-strip.stripprefix.prefixes=/rstudio"
- "traefik.http.middlewares.rstudio-server-path-strip.stripprefix.forceSlash=false"
- "traefik.http.routers.rstudio-server.middlewares=rstudio-server-path-strip"
- "traefik.http.services.rstudio-server.loadbalancer.server.port=8787"
````
1. Open a Terminal window and run the following command:
`docker-compose up -d`
1. Open a Web browser window, visit the URL http://localhost/rstudio and voilà!
<img width="689" alt="Screen Shot 2022-03-14 at 09 49 35" src="https://user-images.githubusercontent.com/4059310/158257556-68080e20-e5d7-4e67-9683-939c9dda2fe6.png">
1. To stop it:
`docker-compose down`
To allow the deployment of RStudio Server on a self-service container service delivery platform (e.g., Portainer) with no need to mount a volume I'd suggest to place the content of _`wwwsettings.sh`_ in _`rocker-versioned2/scripts/init_set_env.sh`_ (or in _`rocker-versioned2/scripts/init_userconf.sh`_).
https://github.com/rocker-org/rocker-versioned2/blob/83651a7f3471d0f56f73bfaff462191e42eeeb1d/scripts/init_set_env.sh#L1-L14
In the future, it may be relevant to expose other RStudio server configuration settings/properties. Just let me know in case I should send the proposed solution as a pull request.
Cheers!