我有一个 rails 应用程序,想要在特定位置使用基本的 Nginx Basic Auth,现在我正在这样做
location / {
passenger_enabled on;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-FORWARDED_PROTO https;
proxy_redirect off;
}
location /somelocation {
auth_basic "Restricted"; #For Basic Auth
auth_basic_user_file /usr/local/etc/nginx/.htpasswd; #For Basic Auth
passenger_enabled on;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-FORWARDED_PROTO https;
proxy_redirect off;
}
有没有更好的方法呢?
答案1
是的,Rails 可以在控制器内部执行此操作,请参阅文档详情请见:
class PostsController < ApplicationController
http_basic_authenticate_with name: "dhh", password: "secret", except: :index
def index
render plain: "Everyone can see me!"
end
def edit
render plain: "I'm only accessible if you know the password"
end
end