本人不才,配置了兩天,終于搞出來了,結(jié)合網(wǎng)上諸多博文,特此總結(jié)一下!
配置環(huán)境:
Ubuntu 11.04
PCRE 8.31
Openssl 2.0.2
Nginx 1.2.5
為了確保能在 nginx中使用正則表達(dá)式進(jìn)行更靈活的配置,安裝之前需要確定系統(tǒng)是否安裝有 PCRE(Perl Compatible Regular Expressions)包??梢缘?/span>ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/
下載最新的 PCRE 源碼包,使用下面命令下載編譯和安裝 PCRE 包:
- # wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.31.tar.bz2
- # tar jxvf pcre-8.31.tar.bz2
- # cd pcre-8.31
- # ./configure –enable-utf8
- # make
- # make install
openssl為開源軟件,在Linux(或UNIX/Cygwin)下創(chuàng)建一個簡單的CA。我們可以利用這個CA進(jìn)行PKI、數(shù)字證書相關(guān)的測試。比如,在測試用Tomcat或Apache構(gòu)建HTTPS雙向認(rèn)證時,我們可以利用自己建立的測試CA來為服務(wù)器端頒發(fā)服務(wù)器數(shù)字證書,為客戶端(瀏覽器)生成文件形式的數(shù)字證書(可以同時利用openssl生成客戶端私鑰),安裝方法和上面類似。
下面重點(diǎn)說說nginx的安裝方法:
下載最新穩(wěn)定版本1.2.5,使用命令:
- # tar zxvf nginx-1.2.5.tar.gz
- # cd nginx-1.2.5
- # ./configure
- --prefix=/usr
- --sbin-path=/usr/sbin/nginx
- --conf-path=/etc/nginx/nginx.conf
- --error-log-path=/var/log/nginx/error.log
- --pid-path=/var/run/nginx/nginx.pid
- --lock-path=/var/lock/nginx.lock
- --user=www-nginx
- --group=www
- --with-http_ssl_module
- --with-http_stub_status_module
- --with-http_flv_module
- --with-http_gzip_static_module
- --http-log-path=/var/log/nginx/access.log
- --http-client-body-temp-path=/var/tmp/nginx/client/
- --http-proxy-temp-path=/var/tmp/nginx/proxy/
- --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/
- # 簡單安裝 ./configure --prefix=/opt/nginx --with-http_stub_status_module --with-http_ssl_module
- # make
- # make install
注意:在使用"--prefix"等配置項(xiàng)時,前面是兩橫"--",而不是"-",這里有些博文根本沒注意到,害得我暈了半天。
--with-http_stub_status_module 是為了啟用 nginx 的 NginxStatus 功能,用來監(jiān)控 nginx 的當(dāng)前狀態(tài)。
--with-http_ssl_module 啟用http_ssl模塊
--with-ipv6 支持ipv6
安裝成功后 /opt/nginx 目錄下有四個子目錄分別是:conf、html、logs、sbin 。其中 nginx 的配置文件存放于 conf/nginx.conf,nginx 只有一個程序文件位于 sbin 目錄下。確保系統(tǒng)的 80 端口沒被其他程序占用,運(yùn)行 sbin/./nginx 命令來啟動 Nginx,打開瀏覽器訪問此機(jī)器的 IP,如果瀏覽器出現(xiàn) Welcome to nginx! 則表示 nginx 已經(jīng)安裝并運(yùn)行成功。
注:此處采用sbin/./nginx命令啟動是因?yàn)槲疫@里如果用網(wǎng)上說的sbin/nginx啟動的話,根本啟動不了,而且會出現(xiàn)安裝nginx的提示,很怪!
使用openssl制作證書:
1、服務(wù)器單向驗(yàn)證
創(chuàng)建并進(jìn)入sslkey存放目錄
# mkdir /opt/nginx/sslkey
# cd /opt/nginx/sslkey
①、生成RSA密鑰:
# openssl genrsa -out key.pem 2048
②、生成一個證書請求
# openssl req -new -key key.pem -out cert.csr
# //會提示輸入省份、城市、域名信息等,重要的是,email 一定要是你的域名后綴的你可以拿著這個文件去數(shù)字證書頒發(fā)機(jī)構(gòu)(即CA)申請一個數(shù)字證書。CA會給你一個新的文件cacert.pem,那才是你的數(shù)字證書。
如果是自己做測試,就可以用下面這個命令來生成證書:
# openssl req -new -x509 -nodes -out server.crt -keyout server.key
③、修改 nginx 配置
- # HTTPS server
- #
- server {
- listen 443;
- server_name localhost;
-
- ssl on;
- ssl_certificate /opt/nginx/sslkey/server.crt;
- ssl_certificate_key /opt/nginx/sslkey/server.key;
-
- ssl_session_timeout 5m;
-
- ssl_protocols SSLv2 SSLv3 TLSv1;
- ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
- ssl_prefer_server_ciphers on;
-
- location / {
- root /home/workspace/;
- index index.asp index.aspx;
- }
- }
配置好后,重啟nginx,采用 https打開網(wǎng)站,瀏覽器會提示證書錯誤,點(diǎn)擊繼續(xù)瀏覽即可。
2、服務(wù)器-客戶端雙向驗(yàn)證
在nginx 目錄下建立ca文件夾,進(jìn)入ca。
#
mkdir newcerts private conf server。
其中newcerts子目錄將存放CA簽署(頒發(fā))過的數(shù)字證書(證書備份目錄)。而private目錄用于存放CA的私鑰。目錄conf只是用于存放一些簡化參數(shù)
用的配置文件,server存放服務(wù)器證書文件。
①、在conf目錄創(chuàng)建文件openssl.conf配置文件,內(nèi)容如下:
- [ ca ]
- default_ca = foo # The default ca section
-
- [ foo ]
- dir = /opt/nginx/ca # top dir
- database = /opt/nginx/ca/index.txt # index file.
- new_certs_dir = /opt/nginx/ca/newcerts # new certs dir
-
- certificate = /opt/nginx/ca/private/ca.crt # The CA cert
- serial = /opt/nginx/ca/serial # serial no file
- private_key = /opt/nginx/ca/private/ca.key # CA private key
- RANDFILE =/opt/nginx/ca/private/.rand # random number file
-
- default_days = 365 # how long to certify for
- default_crl_days= 30 # how long before next CRL
- default_md = md5 # message digest method to use
- unique_subject = no # Set to 'no' to allow creation of
- # several ctificates with same subject.
- policy = policy_any # default policy
-
- [ policy_any ]
- countryName = match
- stateOrProvinceName = match
- organizationName = match
- organizationalUnitName = match
- localityName = optional
- commonName = supplied
- emailAddress = optional
注:你也可以直接修改openssl的配置文件,這樣的話后面制作證書的代碼中就不用引用這個配置文件了。
②、使用腳本創(chuàng)建證書
下面的幾個腳本都放在/nginx/ca/目錄下。
創(chuàng)建一個新的CA根證書。
new_ca.sh:
- #!/bin/sh
- # Generate the key.
- openssl genrsa -out private/ca.key
- # Generate a certificate request.
- 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.
- openssl x509 -req -days 365 -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 > serial
- # Create the CA's key database.
- touch index.txt
- # Create a Certificate Revocation list for removing 'user certificates.'
- openssl ca -gencrl -out /opt/nginx/ca/private/ca.crl -crldays 7 -config "/opt/nginx/ca/conf/openssl.conf"
執(zhí)行 sh new_ca.sh生成新的CA證書。
生成服務(wù)器證書的腳本。
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 server/server.key
- # Take our key and create a Certificate Signing Request for it.
- openssl req -new -key server/server.key -out server/server.csr
- # Sign this bastard key with our bastard CA key.
- openssl ca -in server/server.csr -cert private/ca.crt -keyfile private/ca.key -out server/server.crt -config "/opt/nginx/ca/conf/openssl.conf"
執(zhí)行 sh new_server.sh生成新服務(wù)器的證書
配置 nginx的ssl支持:
- #user www-nginx;
- worker_processes 1;
-
- #error_log logs/error.log;
- #error_log logs/error.log notice;
- #error_log logs/error.log info;
-
- #pid logs/nginx.pid;
-
-
- events {
- worker_connections 1024;
- }
- http {
- include mime.types;
- default_type application/octet-stream;
- sendfile on;
- keepalive_timeout 65;
- #gzip on;
-
- # HTTPS server
- #
- server {
- listen 443;
- server_name localhost;
- ssi on;
- ssi_silent_errors on;
- ssi_types text/shtml;
-
- ssl on;
- ssl_certificate /opt/nginx/ca/server/server.crt;
- ssl_certificate_key /opt/nginx/ca/server/server.key;
- ssl_client_certificate /opt/nginx/ca/private/ca.crt;
-
- ssl_session_timeout 5m;
- ssl_verify_client on; #開戶客戶端證書驗(yàn)證
-
- ssl_protocols SSLv2 SSLv3 TLSv1;
- ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
- ssl_prefer_server_ciphers on;
-
- location / {
- root /home/workspace/;
- index index.asp index.aspx;
- }
- }
- }
啟動nginx ,等待客戶連接,如果此時連接服務(wù)器,將提示400 Bad request certification的錯誤,故還需要生成客戶端證書。
new_user.sh:
- #!/bin/sh
- # The base of where our SSL stuff lives.
- base="/opt/nginx/ca"
- # 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/
-
- # 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/client.key 1024
- # Create a Certificate Signing Request for said key.
- openssl req -new -key $base/users/client.key -out $base/users/client.csr
- # Sign the key with our CA's key and cert and create the user's certificate out of it.
- openssl ca -in $base/users/client.csr -cert $base/private/ca.crt -keyfile $base/private/ca.key -out $base/users/client.crt -config "/opt/nginx/ca/conf/openssl.conf"
-
- # 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.
- openssl pkcs12 -export -clcerts -in $base/users/client.crt -inkey $base/users/client.key -out $base/users/client.p12
-
-
執(zhí)行 shnew_user.sh生成一個 client證書。
按照提示一步一步來,這里要注意的是客戶證書的幾個項(xiàng)目要和根證書匹配。
也就是前面配置的:
countryName = match
stateOrProvinceName = match
organizationName = match
organizationalUnitName = match
不一致的話無法生成最后的客戶證書,證書生成后,客戶端導(dǎo)入證書瀏覽器,即可打開網(wǎng)站。
注意事項(xiàng):
1、制作證書時會提示輸入密碼,服務(wù)器證書和客戶端證書密碼可以不相同。
2、服務(wù)器證書和客戶端證書制作時提示輸入省份、城市、域名信息等,需保持一致。
3、Nginx默認(rèn)未開啟SSI,上面配置已開啟。
4、Nginx不能自啟動,需要如下配置:
- cd /etc/init.d
- sudo touch nginx
- sudo chmod +x nginx
nginx內(nèi)容:
- #! /bin/sh
- #
- ### BEGIN INIT INFO
- # Provides: nginx
- # Required-Start: $syslog $local_fs $remote_fs
- # Required-Stop: $syslog $local_fs $remote_fs
- # Should-Start: dbus avahi
- # Should-Stop: dbus avahi
- # Default-Start: 2 3 4 5
- # Default-Stop: 1
- # Short-Description: Nginx Server
- # Description: Nginx
- ### END INIT INFO
-
- PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/opt/nginx/sbin
- DAEMON=/opt/nginx/sbin/nginx
- NAME=nginx
- DESC="Nginx Server"
- PID_FILE=/opt/nginx/logs/nginx.pid
-
- test -x $DAEMON || exit 0
-
- RUN=yes
- #RUN_AS_USER=root
-
- #DAEMON_OPTS="-a $RUN_AS_USER"
-
- set -e
-
- case "$1" in
- start)
- echo -n "Starting $DESC: "
- start-stop-daemon --start --quiet --pidfile $PID_FILE \
- --exec $DAEMON
- echo "$NAME."
- ;;
- stop)
- echo -n "Stopping $DESC: "
- start-stop-daemon --stop --oknodo --quiet --pidfile $PID_FILE \
- --exec $DAEMON
- echo "$NAME."
- ;;
- force-reload)
- # check whether $DAEMON is running. If so, restart
- start-stop-daemon --stop --test --quiet --pidfile \
- $PID_FILE --exec $DAEMON \
- && $0 restart \
- || exit 0
- ;;
- restart)
- echo -n "Restarting $DESC: "
- start-stop-daemon --stop --oknodo --quiet --pidfile \
- $PID_FILE --exec $DAEMON
- sleep 1
- start-stop-daemon --start --quiet --pidfile \
- $PID_FILE --exec $DAEMON
- echo "$NAME."
- ;;
- status)
- if [ -s $PID_FILE ]; then
- RUNNING=$(cat $PID_FILE)
- if [ -d /proc/$RUNNING ]; then
- if [ $(readlink /proc/$RUNNING/exe) = $DAEMON ]; then
- echo "$NAME is running."
- exit 0
- fi
- fi
-
- # No such PID, or executables don't match
- echo "$NAME is not running, but pidfile existed."
- rm $PID_FILE
- exit 1
- else
- rm -f $PID_FILE
- echo "$NAME not running."
- exit 1
- fi
- ;;
- *)
- N=/etc/init.d/$NAME
- echo "Usage: $N {start|stop|restart|force-reload}" >&2
- exit 1
- ;;
- esac
-
- exit 0
設(shè)置自啟動:
- sudo chkconfig --list nginx
- sudo chkconfig nginx on
申明:作者寫博是為了總結(jié)經(jīng)驗(yàn),和交流學(xué)習(xí)之用。
如需轉(zhuǎn)載,請盡量保留此申明,并在文章頁面明顯位置給出原文連接。謝謝!
|