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 問題,希望大家成功。出現問題可以向我咨詢大家共同進步!