I am trying to force authentication on any user that attempts to access OpenHAB, I basically want to do the following:
access openhab (localhost:8080 or :8080) → redirect to nginx authentication page → redirect back to openhab
so far I have this in conf file for nginx:
server {
listen 81;
server_name localhost
return 301 http://$server_name$request_uri;
}
server {
listen 80;
server_name mydomain_or_myip;
# Cross-Origin Resource Sharing.
add_header 'Access-Control-Allow-Origin' '*' always;
add_header 'Access-Control-Allow_Credentials' 'true' always;
add_header 'Access-Control-Allow-Headers' 'Authorization,Accept,Origin,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range' always;
add_header 'Access-Control-Allow-Methods' 'GET,POST,OPTIONS,PUT,DELETE,PATCH' always;
# openHAB 3 api authentication
add_header Set-Cookie X-OPENHAB-AUTH-HEADER=1;
add_header Strict-Transport-Security "max-age=31536000"; # Remove if using self-signed and are having trouble.
location / {
auth_basic "Username and Password Required";
auth_basic_user_file C:/nginx/conf/.htpasswd;
proxy_pass http://localhost:8080/;
# proxy_set_header Host $http_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;
# proxy_set_header Upgrade $http_upgrade;
add_header Cache-Control no-store;
# proxy_set_header Connection "Upgrade";
# proxy_set_header Authorization "";
# satisfy any;
# allow 192.168.0.0/24;
# allow 127.0.0.1;
# deny all;
}
}
but the only way I can authenticate is if I access localhost:81/authentication, but I can go just directly to localhost:8080 (which is openhab) without authentication. This is why I wanna be able to redirecct to /authentication even if I accessed localhost:8080 directly.