Nginx로 Shiny Sever에 Reverse Proxy를 설정한 후 Clinet IP를 얻는 방법입니다.
* Reverse Proxy : 외부에서 User(Client)가 서비스 요청을 하면 Proxy server가 중개해주는 것
* Forward Proxy : 내부에서 Client가 외부로 서비스 요청을 하면 Proxy server가 중개해주는 것
Reverse Proxy : Client - Internet - Reverse Proxy - Server Forward Proxy : Client - Foward Proxy - Internet - Server 출처 : https://velog.io/@moonyoung/Nginx%EC%99%80-Apache |
Shiny Server Free 버전의 경우 server의 seesion$request에 x_forwarded_for header(이하 XFF헤더)가 전달되지 않아
User의 실제 IP를 얻으려면 직접 파싱 해야 했습니다.
(Pro 버전의 경우 whitelist 설정으로 server의 seesion$request에 XFF헤더 정보를 가져올 수 있음)
아래에 파싱 한 결과와 코드 남기겠습니다.
library(shiny)
ui_req <- NULL
ui_xfwd <- NULL
ui <- function(req) {
if ("HTTP_X_FORWARDED_FOR" %in% ls(req))
ui_xfwd <<- req[["HTTP_X_FORWARDED_FOR"]]
ui_req <<- paste(ls(req), collapse = "\n")
fluidPage( h3("UI req(request) 변수 리스트"),
verbatimTextOutput("req_list"),
h3("HTTP_X_FORWARDED_FOR 출력"),
verbatimTextOutput("xff") )
}
server <- function(input, output, session) {
output$req_list <- renderPrint({ cat(ui_req) })
output$xff <- renderPrint({ cat("Clinet IP :", ui_xfwd) })
}
shinyApp(ui, server)
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 4096;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
server {
listen 443 ssl default_server;
listen [::]:443 ssl default_server;
server_name _;
ssl on;
ssl_certificate /etc/letsencrypt/live/shiny.menthamin.shop/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/shiny.menthamin.shop/privkey.pem;
ssl_session_timeout 5m;
ssl_protocols TLSv1.2 SSLv2 SSLv3 TLSv1;
ssl_ciphers "ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA256:ECDHE-RSA-AES256-SHA:ECDHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA256:DHE-RSA-AES128-SHA256:DHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA:AES256-GCM-SHA384:AES128-GCM-SHA256:AES256-SHA256:AES128-SHA256:AES256-SHA:AES128-SHA";
ssl_prefer_server_ciphers on;
root /usr/share/nginx/html;
location ^~ /.well-known/acme-challenge/ {
default_type "text/plain";
root /usr/share/nginx/html;
allow all;
}
location / {
proxy_pass http://localhost:3838;
proxy_http_version 1.1;
proxy_redirect / $scheme://$http_host/;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_read_timeout 20d;
proxy_buffering off;
}
}
}
기타 궁금하신 사항은 댓글 남겨주세요.
감사합니다.
참고자료
1. [Pass the header HTTP_X_FORWARDED through to session$request], https://github.com/rstudio/rsconnect/issues/214
2. [Nginx - Shiny reverse proxy 설정 참고자료 1], https://support.rstudio.com/hc/en-us/articles/213733868-Running-Shiny-Server-with-a-Proxy
3. [Nginx - Shiny reverse proxy 설정 참고자료 2], https://onesixx.com/nginx-rstudio-shiny-server/
4. 문영님 블로그, [Nginx와 Apache, Proxy개념이 잘 정리되어있음], https://velog.io/@moonyoung/Nginx%EC%99%80-Apache
[GO gRPC 에러 해결] protoc-gen-go: program not found or is not executable (0) | 2023.01.02 |
---|---|
[.rpm .deb 변환] Centos 패키지 → Ubuntu 패키지로 변경하기 (0) | 2021.09.14 |
[Bamboo] Ubuntu에 Bamboo 설치하기 (0) | 2021.08.02 |
[Rstudio] Error: Unauthorized user, Rstudio-server 특정 계정 로그인 안되는 문제 해결 (0) | 2021.06.13 |
[VScode] Visual Studio Code 내부망에서 사용하기 (0) | 2021.06.13 |