Can't adjust redirect in Nginx, for message with code 401 “Auth Required”

﹥>﹥吖頭↗ 提交于 2019-12-11 06:32:14

问题


I can't adjust redirect in Nginx virtual hosts config-file, specifically for "401 error message" (401 Authorization Required).

My target: When I open address in web-browser, for example it'll be my.domain.com, I want adjust basic authentication and I wanna get custom html page with original design and original login form instead of simple browser window.

screen shot

So, for this realisation need adjust redirect on 401 message to my custom page. I did try it like this, but it doesn't gave to me any results.

error_page 401 /401.html;
        location ~ (401.html)$ {
            alias /var/www/user/data/my.domain.com/www/index.php;
        }

I still see window with login form, not own custom page screen shot

Full code of Virtual Host:

server {
listen       80;
server_name  my.domain.com;

root   /var/www/user/data/my.domain.com/www;
index  index.html;
location / {
    auth_basic "Restricted Content";
    auth_basic_user_file /etc/nginx/.htpasswd;

    error_page 401 /401.html;
        location ~ (401.html)$ {
            alias /var/www/user/data/my.domain.com/www/index.php;
        }

    location ~ \.php$ {
            include       /etc/nginx/fastcgi_params;
            fastcgi_index index.php;
            fastcgi_split_path_info ^(.+\.php)(.*)$;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_pass unix:/run/php/php7.3-fpm.sock;
    }
}

I need help with code syntax according to my case, specifically with right redirection code for custom 401 html page with login form placed in this path:

/var/www/user/data/my.domain.com/www/index.php;

Maybe somebody, could share templates for this redirect in nginx? I don't know yet how to adjust it correctly. Maybe here need to use another nginx module, for example auth_request instead of auth_basic?

Will be appreciate to workable solutions and hints according this case.

来源:https://stackoverflow.com/questions/57747535/cant-adjust-redirect-in-nginx-for-message-with-code-401-auth-required

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!