Skip to content

Instantly share code, notes, and snippets.

@fire9
Created December 27, 2017 14:58
Show Gist options
  • Save fire9/e19770b4a8609658374ffa8ffed63216 to your computer and use it in GitHub Desktop.
Save fire9/e19770b4a8609658374ffa8ffed63216 to your computer and use it in GitHub Desktop.
给代理服务器增加认证访问

给代理服务器增加认证访问

在做代理服务器的时候如果没有认证功能,会导致很多人来是使用这个代理服务器,为了防止滥用,所以增加个认证功能。

创建Password文件

sh -c "echo -n 'proxy:' >> /etc/nginx/.htpasswd"

设置密码

sh -c "openssl passwd -apr1 >> /etc/nginx/.htpasswd"

nginx

修改ngin配置文件,增加auth_basicauth_basic_user_file参数

server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    root /usr/share/nginx/html;
    index index.html index.htm;

    server_name localhost;

    location / {
        try_files $uri $uri/ =404;
        auth_basic "Restricted Content";
        auth_basic_user_file /etc/nginx/.htpasswd;
    }
}

重启nginx

sudo service nginx restart
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment