Eu tenho o seguinte arquivo .conf para nginx. Estou configurando a autenticação junto com um cookie.
Quando insiro a URL da página de login, ela não mostra o prompt para inserir credenciais e entra em um loop de redirecionamento infinito.
Achei que poderia separar a lógica de redirecionamento para outro bloco, mas não consigo encontrar uma maneira de pular para o bloco /login/redirection.
Tentei usar try_files, mas não funcionou.
O que posso fazer para garantir que o prompt de login sempre apareça?
http {
include mime.types;
default_type application/octet-stream;
#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 logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
location / {
# Check if the user is authenticated by looking for the cookie
if ($http_cookie !~ 'openhab_cookie=loggedin') {
# Redirect to the login page if not authenticated
return 302 http://localhost:81/login;
}
# If the user is authenticated, proceed to the application
proxy_pass http://localhost:8080;
proxy_set_header Host $host;
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;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 81;
server_name localhost;
location /login {
# authentication (issue in here, it is skipped)
auth_basic "Login Required";
auth_basic_user_file /etc/nginx/.htpasswd;
# Only set cookie if authenticated
if ($http_cookie !~* "openhab_cookie=loggedin") {
add_header Set-Cookie "openhab_cookie=loggedin; Max-Age=300; Path=/; HttpOnly";
}
return 302 http://localhost:80/;
}
}
Você poderia usar
try_files
"hack" para fazer um redirecionamento interno que será executado após a verificação da autorização.