无法让Nginx Basic Auth工作

时间:2017-08-11 06:12:58

标签: php authentication ssl nginx basic-authentication

我正在尝试在Nginx子域上设置基本身份验证,但它无效。

server {

listen 80 default;
listen 443 default ssl;

#ssl_certificate /etc/pki/tls/certs/localhost.crt;
#ssl_certificate_key /etc/pki/tls/private/localhost.key;
ssl_certificate /etc/pki/tls/certs/2019-wildcard.somserver.com.crt;
ssl_certificate_key /etc/pki/tls/private/2019-wildcard.somserver.com.key;

server_name www.somserver.com;
root /var/www/vhosts/somserver.com/httpdocs;
access_log /var/log/nginx/somserver.com-access.log main;
error_log /var/log/nginx/somserver.com-error.log warn;
index index.php index.html;

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

    try_files $uri $uri/ @handler;
    expires 30d;
}

location ~ ^/(app|includes|lib|media/customer|media/downloadable|pkginfo|var)/ { deny all; }
location ~ ^/RELEASE_NOTES.txt  { return 404; }
location ~ ^/errors/.*\.(xml|phtml)$ { return 404; }
location ~ ^/media/.*\.(cfg|ini|xml)$ { return 404; }
location ~ ^/media/.*\.(php|pl|py|jsp|asp|htm|shtml|sh|cgi) { return 404; }
location ~ /\. { return 404; }

location /media/ {
location ~ /\. { return 404; }

location /media/ {
    try_files $uri uri/ /get.php;
    expires 30d;
}

location @handler {
            auth_basic "Restricted Content";
    auth_basic_user_file /etc/nginx/.htpasswd;
    rewrite / /index.php;
}

location ~ .php/ {
            auth_basic "Restricted Content";
    auth_basic_user_file /etc/nginx/.htpasswd;
    rewrite ^(.*.php)/ $1 last;
}

location ~ .php$ {
    auth_basic "Restricted Content";
    auth_basic_user_file /etc/nginx/.htpasswd;
    try_files $uri /index.php;
    expires off;
    fastcgi_pass unix:/var/run/php-fpm/somserver.com.sock;
    fastcgi_buffers 256 4k;
    fastcgi_buffer_size 32k;
    fastcgi_busy_buffers_size 256k;
    fastcgi_read_timeout 3600s;
    fastcgi_param HTTPS $fastcgi_https;
    fastcgi_param SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    fastcgi_param MAGE_RUN_CODE default;
    fastcgi_param MAGE_RUN_TYPE store;
    include fastcgi_params;
}

location ~ ^/(php-status|ping)$ {
    access_log off;
    allow 127.0.0.1;
    allow 172.24.16.85;
    deny all;
    include fastcgi_params;
    fastcgi_pass unix:/var/run/php-fpm/somserver.com.sock;
    include fastcgi_params;
    fastcgi_param PATH_TRANSLATED $document_root$fastcgi_script_name;
}

}

最初我假设我只需要将它添加到location / {,但是在尝试将其添加到所有与PHP相关的位置后,它似乎仍然没有任何影响。我可以尝试进一步解决这个问题吗?

它是一个运行Nginx和FasstCGI的Cent OS盒子。应用程序是Magento,所以我需要在php文件上添加基本身份验证。

0 个答案:

没有答案