nginx用户认证
① nginx服务器怎么配置浏览器访问需要证书
一直在用Nginx做反向代理,但是其SSL的配置只用过普通的服务端单向证书。在Google,网络狂搜一通之后,一无所获,依旧是那老三样,只有单向认证的示例。浏览器端双向认证的配置好像从没人写过。
因为要来回的设置所有直接使用域名操作比如:
chaodiquan.com 解析到 IP上面(IP要用你自己的,如果使用CDN另算)
这个是主要在最后的实际应用的测试的使用会用到
因为是自己实际应用,只好从OpenSSL的客户端证书开始学起,一点一点啃,大段大段的E文让我这半瓶子醋看的头晕眼晕。
的提示下终于把这个证书搞定,来秀一个。
这需要一下几个步骤:
1) 安装openssl用来做证书认证
2) 创建一个CA根证书
3) 创建一个自签名的服务器证书
4) 设置Nginx
5) 创建客户端证书
6) 安装客户端证书到浏览器
7) Profit.
1)
这一步我是在ubuntu下直接apt-get装的openssl, 配置文件安装在/etc/ssl/openssl.cnf
修改openssl.cnf的以下几段
[ ca ]
default_ca = foo
Openssl将会寻找名称为foo的配置段
[ foo ]
dir = /etc/ssl/private
database = $dir/index.txt
serial = $dir/serial
private_key = $dir/ca.key
certificate = $dir/ca.crt
default_days = 3650
default_md = md5
new_certs_dir = $dir
policy = policy_match
policy_match 我保持默认值没有改
[ policy_match ]
countryName = match
stateOrProvinceName = match
organizationName = match
organizationalUnitName = match
commonName = supplied
emailAddress = optional
默认签发有效期为10年,你可以自己设置一个合适的值
2)
创建一个新的CA根证书
下面的几个脚本我都放在/etc/ssl目录下
new_ca.sh:
#!/bin/sh
# Generate the key. genrsa意思是生成一个私钥
openssl genrsa -out private/ca.key
# Generate a certificate request. req表示生成证书,还能生成ca证书,-new表示产生一个新csr,需要输入一些信息,-key表示私钥,
openssl req -new -key private/ca.key -out private/ca.csr
#
Self signing key is bad... this could work with a third party signed
key... registeryfly has them on for $16 but I'm too cheap lazy to get
one on a lark.
# I'm also not 100% sure if any old certificate will
work or if you have to buy a special one that you can sign with. I could
investigate further but since this
# service will never see the light of an unencrypted Internet see the cheap and lazy remark.
# So self sign our root key.
x509是一个证书生成工具,显示证书内容,转换格式,给CSR签名等
-signkey用来处理CSR和给证书签名,就像CA。使用时得同时提供私钥,把输入文件变成自签名的证书,如果输入CSR文件,则生成自签名文件
-days证书有效时间
-in输入文件 -out输出文件
openssl x509 -req -days 3650 -in private/ca.csr -signkey private/ca.key -out private/ca.crt
#
Setup the first serial number for our keys... can be any 4 digit hex
string... not sure if there are broader bounds but everything I've seen
uses 4 digits.
echo FACE > private/serial
# Create the CA's key database.
touch private/index.txt
# Create a Certificate Revocation list for removing 'user certificates.'
gencrl在index文件中生成一个CRL相关的信息
-crldays是crl过期的时间
openssl ca -gencrl -out /etc/ssl/private/ca.crl -crldays 7
执行 sh new_ca.sh 生成新的CA证书
3)
生成服务器证书的脚本
new_server.sh:
#
Create us a key. Don't bother putting a password on it since you will
need it to start apache. If you have a better work around I'd love to
hear it.
openssl genrsa -out private/server.key
# Take our key and create a Certificate Signing Request for it.
openssl req -new -key private/server.key -out private/server.csr
# Sign this bastard key with our bastard CA key.
-cert CA本身的证书名
-keyfile CA本身的私钥
这句就是相当于CA用他的证书和私钥,根据服务器的证书,来给出一个CA认证的证书
openssl ca -in private/server.csr -cert private/ca.crt -keyfile private/ca.key -out private/server.crt
执行 sh new_server.sh 生成新服务器的证书
4)
最要命的一步,尝试多次后终于搞明白。
配置 nginx 的ssl支持
我的配置如下:
# HTTPS server
#
server {
listen 443;
server_name localhost;
# 打开ssl
ssl on;
# 上一步生成的服务器证书
ssl_certificate /etc/ssl/private/server.crt;
# 服务器证书公钥
ssl_certificate_key /etc/ssl/private/server.key;
# 客户端证书签名 也就是第二步生成的CA签名证书
ssl_client_certificate /etc/ssl/private/ca.crt;
# ssl session 超时
ssl_session_timeout 5m;
# 打开SSL客户端校验 (双向证书检测)
ssl_verify_client on;
#ssl_protocols SSLv2 SSLv3 TLSv1;
#ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
ssl_prefer_server_ciphers on;
location / {
root /var/www/nginx-default;
index index.html index.htm;
}
启动你的nginx ,等待客户连接
5)
现在来生成客户端证书
new_user.sh:
#!/bin/sh
# The base of where our SSL stuff lives.
base="/etc/ssl/private"
# Were we would like to store keys... in this case we take the username given to us and store everything there.
mkdir -p $base/users/$1/
# Let's create us a key for this user... yeah not sure why people want to use DES3 but at least let's make us a nice big key.
生成用户私钥
openssl genrsa -des3 -out $base/users/$1/$1.key 1024
# Create a Certificate Signing Request for said key.
根据用户私钥生成他的证书
openssl req -new -key $base/users/$1/$1.key -out $base/users/$1/$1.csr
# Sign the key with our CA's key and cert and create the user's certificate out of it.
模拟CA来给出CA认证过的证书
openssl ca -in $base/users/$1/$1.csr -cert $base/ca.crt -keyfile $base/ca.key -out $base/users/$1/$1.crt
# This is the tricky bit... convert the certificate into a form that most browsers will understand PKCS12 to be specific.
#
The export password is the password used for the browser to extract the
bits it needs and insert the key into the user's keychain.
# Take the same precaution with the export password that would take with any other password based authentication scheme.
pkcs12 处理pkcs12文件
根据CA认证过的证书和用户的私钥来生成p12验证证书,可用服务器导入。
openssl pkcs12 -export -clcerts -in $base/users/$1/$1.crt -inkey $base/users/$1/$1.key -out $base/users/$1/$1.p12
执行 sh new_user.sh yourname 来生成一个 yourname 的client证书
按照提示一步一步来,这里要注意的是客户证书的几个项目要和根证书匹配
也就是第一步时配置的:
countryName = match
stateOrProvinceName = match
organizationName = match
organizationalUnitName = match
不一致的话无法生成最后的客户证书
6)
发送上一步生成的 yourname.p12 到客户端。
IE下双击安装就可以导入。
FireFox安装 :
Go into preferences.
Advanced.
View Certificates.
Import.
Enter master password for FireFox (if you don't have one set one here otherwise stolen laptop = easy access).
Enter in the export password given to you by the de who created your cert.
Hit OK like a mad man.
打开第一步进行设置的域名解析会弹出对话框来要求你选择使用哪个证书,选择刚才安装的证书。选择接受服务器证书。现在你可以正常访问服务器拉。如果没弄对的话就会出现400 Bad request certification的错误
7)没啥拉,有问题多试几次,其实都是很简单的事。就是中文的资料太少了。
希望可以帮助到你哈
② 如何在Ubuntu上通过Nginx设置HTTP认证
需要htpassword来创建和生成加密的用户用于基础认证(Basic Authentication)。通过以下命令安装apache2-utils。
sudo apt-get install apache2-utils
创建用户名和密码
在Nginx托管的网站目录下生成一个.htpasswd文件。如下的命令可以创建文件同步增加用户和加密的密码到文件中
sudo htpasswd -c /etc/nginx/.htpasswd exampleuser
命令行为提示你输入密码
New password:
Re-type new password:
Adding password for user exampleuser
htpaswd的文件格式如下:
login:password
注意:htpasswd需要对nginx运行用户可访问
更新Nginx配置
在你的网站的Nginx配置文件增加如下两行:
auth_basic "Restricted";auth_basic_user_file /etc/nginx/.htpasswd;
第二行是你的htpasswd文件位置。
举个例子,假如你的文件是/etc/nginx/sites-available/website_nginx.conf,通过vi或者其它编辑器打开该文件
sudo vi /etc/nginx/sites-available/website_nginx.conf
增加代码:
server { listen portnumber; server_name ip_address; location / { root /var/www/mywebsite.com; index index.html index.htm; auth_basic "Restricted"; #For Basic Auth
auth_basic_user_file /etc/nginx/.htpasswd; #For Basic Auth
}
}
刷新Nginx
为了使配置生效,需要刷新nginx配置,然后再访问
$ sudo /etc/init.d/nginx reload* Reloading nginx configuration...
③ 如何在Ubuntu上通过Nginx设置HTTP认证
命令行为提示你抄输入密码
New password:
Re-type new password:
Adding password for user exampleuser
htpaswd的文件格式如下:
login:password
注意:htpasswd需要对nginx运行用户可访问
④ 怎样用LDAP来认证Nginx的proxy
让nginx做auth有啥意义呢,这里明显是需要多个系统集成,nginx作为一个固化系统,是很难做到版灵活的权,你的web应用都是rail,wsgi前端套个auth中间件来做认证就是了。
HTTP Basic Auth仅仅是浏览器每次都带了HTTP头,写着用户名和base64了的密码,这种操作没必要折腾nginx来完成。
⑤ 如何在Ubuntu上通过Nginx设置HTTP认证
命令行为提来示你输自入密码 New password: Re-type new password: Adding password for user exampleuser htpaswd的文件格式如下: login:password 注意:htpasswd需要对nginx运行用户可访问
⑥ nginx https单向认证是什么意思
nginx https单向认证,就是传输的数据加密过了,但是不会校验客户端的来源
nginx实现https单向认证:
1、安装
nginx要安装http_ssl_mole模块,需要OpenSSL库和相关的开发包,因此在安装前,必须安装这些支持
在centos系统下,直接用yum安装即可:# yum install openssl openssl-devel
编译nginx
# tar -zxvf pcre-8.12.tar.gz
# cd pcre-8.12
# ./configure –prefix=/usr/local
# make
# make install
# tar -zxvf nginx-1.0.0.tar.gz
# cd nginx-1.0.0
# ./configure –prefix=/usr/local/nginx –user=www –group=www –with-http_ssl_mole –with-pcre
# make
# make install
2、制作密匙(单项认证)
# mkdir /usr/local/nginx/ssl
# cd /usr/local/nginx/ssl
# openssl genrsa -des3 -out server.key 1024 (建立服务器私钥,在这个过程中需要输入密码短语,需要记住这个密码)
# openssl req -new -key server.key -out server.csr
输入命令以后,需要填写如下内容:
Country Name(国家:中国填写CN)
State or Province Name(区域或是省份:CHONGQING)
Locality Name(地区局部名字:CHONGQING)
Organization Name(机构名称:填写公司名)
Organizational Unit Name(组织单位名称:部门名称)
Common Name(网站域名)
Email Address(邮箱地址)
A challenge password(输入一个密码)
An optional company name(一个可选的公司名称)
输入完这些内容,就会在当前目录生成server.csr文件
# cp server.key server.key.org
# openssl rsa -in server.key.org -out server.key (对于使用上面的私钥启动具有SSL功能的NGINX)
# openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt (使用上面的密钥和CSR对证书进行签
名)
3、配置NGINX
编辑需要使用HTTPS的域名的NGINX配置文件(上面填写的Common Name网站域名)
server {
listen 443;
server_name www_xxx_com;
ssl on;
ssl_certificate /usr/local/nginx/ssl/server.crt;
ssl_certificate_key /usr/local/nginx/ssl/server.key;
}
保存,重启NGINX,这样就搭建了一个简单的https服务的网站(单项认证)
⑦ nginx https 双向认证配置方法
SSL 的双向认证就是,客户端要获取服务端的证书,检查下服务端是不是我可回以信任的主机,否答则我就认为那个站点的内容不可信任,不应该去访问你(浏览器会告诉你),同时服务端也要检查客户端的证书,客户端如果不是服务端所信任的,那服务端也会认为,你不是我的合法用户,我拒绝给你提供服务。所以,要让 HTTPS 的双向认证顺利完成,就要在服务端给定一个证书,这个证书是浏览器可信任的,同时客户端(浏览器)也要发送给服务端一个证书,服务器端也要信任这个证书。
要想让浏览器纯自然地就去信任服务端的证书,那服务端所使用的证书就得是那几大已经被浏览器所信任的证书机构比如沃通CA等签发的证书!
⑧ 403 Forbidden nginx的网站认证
403 错误是由两种原因引起的
1、 网站没有设置默认页
2、权限不足
主要针对第二个原因分析解决办法
1、如果你是通过浏览器正常访问 当出现403时 尝试清空浏览器记录(cookie) 再次访问如果还是出现403 在尝试切换ip(代理或重拨宽带) 如果还是不能访问 那没办法 堆糖网在服务器端 根据你的mac地址或者其他的唯一标识限制了你的访问
有些网站为了防止其他人采集他们会限制单个用户在一定时间内访问的次数
2、如果你是通过程序进行采集 可以尝试伪造Referer、Host、User-Agent
⑨ 我想用nginx服务器代替浏览器来验证服务端的证书,请问怎么做
这个可以配置NGINX的SSL证书验证来实现。
server{
listen12345;#监听端口
server_namelocalhost;
charsetutf-8;#编码识别
sslon;#开启ssl
ssl_certificate/ls/app/nginx/conf/server.crt;#服务的证书
ssl_certificate_key/ls/app/nginx/conf/server.key;#服务端key
ssl_client_certificate/ls/app/nginx/conf/ca.crt;#客户端证书
ssl_session_timeout5m;#session超时时间
ssl_verify_clienton;#开户客户端证书验证
ssl_protocolsSSLv2SSLv3TLSv1;#允许SSL协议
access_log/logs/nginx/access.logaccess;#日志格式及日志存放路径
error_log/logs/nginx/error.log;#错误日志存放路径
}
⑩ 如何配置Nginx,Apache服务器的alias和密码认证
从年前电脑换成linux系统后就没写东西,最近有点懒,在这里讲述下nginx alias 功能,不是server alias .
首先看下看下apache 别名 怎么配置的:
<VirtualHost *:80>
DocumentRoot /www/hou.net/www 这是虚拟主机的根目录吧,但是phpMYadmin 不在这个目录下,想访问。
ServerName www.hou.net
ServerAlias hou.net
Alias /sdb "/www/public/phpMyAdmin/" 就需要 别名功能,:http://www.hou.com/sdb 这样就安全多了。
<Directory "/www/public/phpMyAdmin/">
Options Indexes FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
一 .Apache认证
认证的类型:Basic
Digest摘要
认证方法:A、容器认证: ……
B、隐藏文件认证创建.htaccess文件
方法一、容器认证
A、 进入配置文件 vi /etc/httpd/conf/httpd.conf
B、 配置:大约在531行附近 配置如下:
AllowOverride None ##不允许通过隐藏认证,即通过容器认证
AuthType Basic ##认证类型为Basic
AuthName “ajian” ##认证名字为Ajian
AuthUserFile /var/www/passwd/pass ##pass 为认证密码文件,指定密码文件存放的位置。
Require valid-user ##有效用户(注意大小写,因为Word的原因有些大小写有变化)
C、 创建目录 mkdir -p /var/www/passwd
进入目录 cd /var/www/passwd
D、创建Apache用户 htpasswd -c pass ajian ##pass 为密码文件Ajian为用户
更改 把Pass文件的使用权给Apache: chown apache.apache pass
附:再在Pass文件中添加一个用户:htpasswd pass tt ##添加一个TT的用户到Pass文件中
E、重启服务并测试
方法二、通过隐藏认证
和上面差不多 不过配置不一样
Httpd主配置文件
AllowOverride AuthConfig
创建隐藏文件并放到要通过认证的目录
Eg: vi /var/www/html/mrtg
AuthType Basic
AuthName “Ajian”
AuthUserFile /var/www/passwd/pass
Require valid-user
下面是例子
二、Nginx 登录认证
nginx 的 http auth basic 的密码是用 crypt(3) 加密的。用 apache 的 htpasswd 可以生成密码文件。
没有 apache 自行安装。我安装的是 apache2,/usr/local/apach2。
cd /usr/local/nginx/conf
/usr/local/apache2/bin/htpasswd -c -d pass_file user_name
#回车输入密码,-c 表示生成文件,-d 是以 crypt 加密。
vi nginx.conf
cd /usr/local/nginx/conf /usr/local/apache2/bin/htpasswd -c -d pass_file user_name #回车输入密码,-c 表示生成文件,-d 是以 crypt 加密。 vi nginx.conf
在 nginx.conf 文件中加入授权声明。这里要注意 nginx 0.6.7 开始,auth_basic_user_file 的相对目录是 nginx_home/conf,以前版本的相对目录是 nginx_home。
server {
listen 80;
server_name tuan.xywy.com;
root /www/tuangou;
index index.html index.htm index.php;
autoindex on;
auth_basic "input you user name and password";
auth_basic_user_file htpasswd.file;
location ~ .php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /www/tuangou$fastcgi_script_name;
include fastcgi_params;
}
error_page 404 /404.php;
error_page 403 /404.php;
access_log /logs/tuan_access.log main;
}
针对目录的认证,在一个单独的location中,并且在该location中嵌套一个解释php的location,否则php文件不会执行并且会被下载。auth_basic在嵌套的location之后。
server {
listen 80;
server_name tuan.xywy.com;
root /www/tuangou;
index index.html index.htm index.php;
autoindex on;
location ~ ^/admin/.* {
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /www/tuangou$fastcgi_script_name;
include fastcgi_params;
}
root /www/tuangou/ ;
auth_basic "auth";
auth_basic_user_file htpasswd.file;
}
location ~ .php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
}
access_log /logs/tuan_access.log main;
}
三.nginx alias功能配置自动列目录
server {
listen www.hou.com:88;
server_name www.hou.com;
autoindex on; //开启列目录功能。
# charset gbk;
location /club { 访问的名字http://www.hou.com:88/club
alias /www/clublog/club.xywy.com/; 这是服务器上存放日志的地方
} 这段意思 访问www.hou.com:88/club 就看到club目录的东东了。
location /{
root /www/access;
这段location 也可以没有 www.hou.com:88 出来的是默认nxing 页面
# index index.html index.htm index.php;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
上面nginx配置意思就是: 访问http://hou.xywy.com/:88认证进去是默认访问服务器上/www/access/里面的目录,认证进去后url=http://hou.xywy.com:88/club 就出来 /www/clublog/club.xywy.com/ 里面的目录的内容了。,可能很绕,仔细分析就好了。
root 和 alias 的区别。
最基本的区别:alias指定的目录是准确的,root是指定目录的上级目录,并且该上级目录要含有location指定名称的同名目录。另外,根据前文所述,使用alias标签的目录块中不能使用rewrite的break。
这样在看这段就很清晰了,
location /abc/ {
alias /home/html/abc/;
}
在这段配置下,http://test/abc/a.html就指定的是/home/html/abc/a.html。这段配置亦可改成
location /abc/ {
root /home/html/;
}
这样,nginx就会去找/home/html/目录下的abc目录了,得到的结果是相同的。
但是,如果我把alias的配置改成:
location /abc/ {
alias /home/html/def/;
}
那么nginx将会从/home/html/def/取数据,这段配置还不能直接使用root配置,如果非要配置,只有在/home/html/下建立一个 def->abc的软link(快捷方式)了。
一般情况下,在location /中配置root,在location /other中配置alias是一个好习惯。
至于alias和root的区别,我估计还没有说完全,如果在配置时发现奇异问题,不妨把这两者换换试试。
刚开始我也搞来高去搞了很久包括认证单独一个目录 CGI 问题,希望大家成功。出现问题可以向我咨询大家共同进步!