V
V
voker20052019-08-31 15:28:31
PHP
voker2005, 2019-08-31 15:28:31

How to make a custom authorization page in Nginx using the "auth_request" or "auth_basic" function?

Hello colleagues.
The task is to make a custom authorization page on the Nginx web server using the " auth_request " or " auth_basic " function. Is this even possible or am I delusional and wasting my time?
As planned:
I have a domain, for example my.domain.com , in order to open it, you need to log in to this web server, i.e. you need to configure auth_basic , and this is done very simply, no more than 5 minutes. That is, we will create a password file using the htpasswd utility and simply add this to the virtual host config:

auth_basic "Restricted Content";
auth_basic_user_file /etc/nginx/.htpasswd;

When trying to open the my.domain.com domain, we will see the following window:
5d6a603cb48c3006232317.png
But as planned, it looks simple and I want the login and password entry form to be with a corporate design, namely, to open a web page (index.php) with login and password entry fields , captcha and own graphic design.
It is necessary that when trying to open the my.domain.com domain in the browser, the user is redirected to another domain, namely to login.domain.com , then the page https://login.domain.com/index.php opens automatically where the user is in a beautiful form enters a login and password for authorization on the Nginx web server and, as a result, redirects to the my.domain.com domainfrom which the authorization was requested by the web server. It seems to me that auth_basic
is not suitable for this feint , because after reading the documentation I did not see a thoughtful opportunity to make such settings, but I saw the potential in the auth_request function where there is a function of some subrequests that, in response, if they give a 200 code, it turns out that in php you can redirect back to the originally requested link my.domain.com , because the browser session will be authorized and you can work on the web server with this site. ---- For authorization, redirection to another domain is optional, because. a redirect can be done, for example, to a custom path of the same domain, for example, to my.domain.com/super_aut/index.php ----

Closer to the question:
Here is an example virtual host config:
server {
    listen       80;
    server_name  my.domain.com;

# При открытии сайта, запрашивает авторизацию на веб-сервере nginx
location / {
    auth_request /auth;
    ...
}

# Говорим серверу что путь к авторизации ( /auth) находится тут, по ссылке http://login.domain.com:
location = /auth {
    proxy_pass http://login.domain.com;
    proxy_pass_request_body off;
    proxy_set_header Content-Length "";
    proxy_set_header X-Original-URI $request_uri;
}

In this case, the authorization technology is not entirely clear to me, where and how do we administer the access right, on the PHP side? What regulates authentication, we configure the login and password in php, and in nginx we configure the exchange of response codes, or how. Tell me where to specify the login and password for authorization on the server, I will be glad for any useful information about this.
I also wanted to know what should be php. I would be glad for any information on this subject, especially the PHP template in which this configuration is implemented.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dimonchik, 2019-08-31
@dimonchik2013

FSE is easier
https://stackoverflow.com/questions/3079031/login-...
well, only if you are not a shooter

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question