Nginx 开启http基本认证

Nginx 开启http基本认证

方式一

# 安装 htpasswd 命令
yum install httpd-tools
htpasswd -cb  /usr/local/nginx/passwd/test.db admin 123456

方式二

echo "admin:$(openssl passwd -crypt 123456)" >> /usr/local/nginx/passwd/test.db

修改 nginx 相应server模块

加入:

auth_basic "User Authentication";
auth_basic_user_file /usr/local/nginx/passwd/test.db;

示例:

 vim /usr/local/nginx/conf/vhost/test.conf
server
    {
        listen 80 default_server reuseport;
        #listen [::]:80 default_server ipv6only=on;
        server_name _;
        index index.html index.htm index.php;
        root  /home/wwwroot/web/test;

        #error_page   404   /404.html;

        auth_basic "User Authentication";
        auth_basic_user_file /usr/local/nginx/conf/htpasswd/default.htpasswd;

        # Deny access to PHP files in specific directory
        #location ~ /(wp-content|uploads|wp-includes|images)/.*\.php$ { deny all; }

        location / {
                if (!-e $request_filename) {
                        rewrite ^(.*)$ /index.php?s=/$1 last;
                }
        }

        include enable-php.conf;

        location /nginx_status
        {
            stub_status on;
            access_log   off;
        }

        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
        {
            expires      30d;
        }

        location ~ .*\.(js|css)?$
        {
            expires      12h;
        }

        location ~ /.well-known {
            allow all;
        }

        location ~ /\.
        {
            deny all;
        }

        access_log  /home/wwwlogs/test_access.log;
    }

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注