S
S
shur1k2014-01-27 18:05:14
PHP
shur1k, 2014-01-27 18:05:14

How to attach simple authorization to the site (nginx rewrite + htpasswd)?

Good afternoon!
It is not possible to fasten simple authorization to the site in any way.
What we have: nginx + php + fpm and a simple site in php.
Host config:

server {
        listen   80;
        server_name test.site.ru;
        root /var/www/test.site.ru/web;
        error_log /var/log/ispconfig/httpd/test.site.ru/error.log;
        access_log /var/log/ispconfig/httpd/test.site.ru/access.log combined;



        location / {
        index  index.php;
        auth_basic "Members Only";
        auth_basic_user_file /etc/nginx/htpasswd;
        #правила перенес из htaccess 
        rewrite ^/image /image.php last;               
        rewrite ^/web/(.*) /web/$1 last;
        rewrite ^/receiver.html /receiver.html last;
        rewrite ^/ /index.php last;
        }

        Images and static content is treated different
        location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|xml)$ {
            access_log        off;
            expires           360d;
        }
        location ~ /\.ht {
             deny  all;
         }
          location ~ \.php$ {
            include /etc/nginx/fastcgi_params;
             fastcgi_pass 127.0.0.1:9000;
             fastcgi_index index.php;
             fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
         }
  }

rewrite ^/ /index.php last;Authorization works without the directive , but further on all links 404 error. Authorization does not work with the rule.
Please tell me which way to dig, probably someone has already come across.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Vit, 2014-01-27
@shur1k

Have you tried adding auth_basic to location ~ \.php$?

S
shur1k, 2014-01-27
@shur1k

I tried it, it worked. Thank you :)
If it's not difficult, in a nutshell, why didn't it work before? You can link to the documentation.

S
ShamblerR, 2015-02-24
@ShamblerR

enter in server

error_page 555 = @pass;
        location @pass {
        auth_basic "Unauthorized";
        auth_basic_user_file /var/www/dev_htpasswd;
        proxy_pass http://127.0.0.1:82;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Real-IP $remote_addr;

and in location /
if ($http_host ~* "^(dev|old|www.old|www.dev)\..*\..{2,8}$"){

                return 555;
                }
                if ($http_host ~* "^.*\.ks03\.ru$"){
                return 555;
                }

}

I added the config for the proxy, for the usual nginx you have already done it yourself.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question