Skip to content

Instantly share code, notes, and snippets.

@ewalk153
Last active February 2, 2023 02:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ewalk153/42b5ce7ede5b9a52aef61bf0331e8f46 to your computer and use it in GitHub Desktop.
Save ewalk153/42b5ce7ede5b9a52aef61bf0331e8f46 to your computer and use it in GitHub Desktop.
Protect static assets with a rails app
upstream backend {
server localhost:3000;
}
server {
listen 9000;
server_name localhost;
root /path_of_rails_app/public;
try_files $uri/index.html $uri @app;
location @app {
proxy_pass http://app;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
}
location /private/ {
root /path_to_protected_resources;
index index.html;
auth_request /auth;
auth_request_set $auth_status $upstream_status;
expires -1;
add_header 'Cache-Control' 'no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0';
}
}
class AuthController < ApplicationController
before_action :authenticate_user!, only: :index
# Setup a rails app with devise using a model named User.
def index
# add empty view file
end
def auth
status = user_signed_in? ? 200 : 403
render(status: status, plain: "")
end
end
Rails.application.routes.draw do
root "auth#index"
get "/auth", to: "auth#auth"
devise_for :users
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment