[root@ahao tec]# service nginx restart
重启之后测试:(本地物理地址是192.168.1.105)
所以在我们物理机上是可以访问的。
这样就达到目的,物理机192.168.1.105可以访问服务,但是在虚拟机中192.168.1.199就不能访问服务器。
3、加密访问
加密访问使用的模块是:ssl
参考网站:http://nginx.org/en/docs/http/ngx_http_ssl_module.html
配置:
CA服务器配置:
[root@ahao~]# vim /etc/pki/tls/openssl.cnf
首先我们要简单的配置ssl模块文件:
根据文件中需要一个数据文件来记录证书颁发的情况。所以我们就建立这样的 文件,
[root@ahao~]# cd /etc/pki/CA
[root@ahao CA]# touch index.txt
[root@ahaoCA]# touch serial //这是记录证书序号的文件,也需要手动建立
[root@ahaoCA]# echo "01">serial //加入序号,以“01”开始
在openssl.cnf文件中还需要更改证书颁发所允许的地区,要给成不受限的。
countryName = optional
stateOrProvinceName =optional
organizationName = optional
还可以根据自己的需要更改默认颁发证书的国家,省份,城市。
[root@ahaoCA]# openssl genrsa 1024 >private/cakey.pem //产生私钥文件
[root@ahaoCA]# chmod 600 private/cakey.pem //更改权限
//产生公钥文件
[root@ahaoCA]# openssl req -new -key private/cakey.pem -x509 -out cacert.pem
You are about to be asked to enter information that will beincorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Nameor a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [CN]:
State or Province Name (full name) [BEIJING]:
Locality Name (eg, city) [BEIJING]:
Organization Name (eg, company) [Default Company Ltd]:CA
Organizational Unit Name (eg, section) []:tec
Common Name (eg, your name or your server's hostname) []:rootca.org
Email Address []:
Web服务器现在需要向CA服务器获取证书(证书的获取是需要个过程,证书上要有web服务 的公钥,公钥从哪里来呢,公钥是从私钥中产生的,所以在请求的过程中,必须要先 有WEB服务的私钥,再发送请求,获得证书。)
//产生web服务的私钥文件
[root@ahao CA]# mkdir -pv /etc/nginx/certs
mkdir: created directory `/etc/nginx/certs'
[root@ahao CA]# cd /etc/nginx/certs/
[root@ahao certs]# openssl genrsa 1024 >nginx.key
[root@ahaocerts]# chmod 600 nginx.key //更改权限
[root@ahaocerts]# openssl req -new -key nginx.key-out nginx.crq // 公钥文件的建立
You are about to be asked to enter information that will beincorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Nameor a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [CN]:
State or Province Name (full name) [BEIJING]:HENAN
Locality Name (eg, city) [BEIJING]:ZHENGZHOU
Organization Name (eg, company) [Default CompanyLtd]:TUOYUANEDU
Organizational Unit Name (eg, section) []:tec
Common Name (eg, your name or your server's hostname) []:tec.abc.com
Email Address []:
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
[root@ahaocerts]# openssl ca -in nginx.crq -out nginx.cert //建立请求 文件
Usingconfiguration from /etc/pki/tls/openssl.cnf
Check that the request matches the signature
Signature ok
Certificate Details:
Serial Number: 1 (0x1)
Validity
Not Before: Nov 30 00:17:54 2013 GMT
Not After : Nov 30 00:17:54 2014 GMT
Subject:
countryName = CN
stateOrProvinceName = HENAN
organizationName =TUOYUANEDU
organizationalUnitName = tec
commonName =tec.abc.com
X509v3 extensions:
X509v3 Basic Constraints:
CA:FALSE
Netscape Comment:
OpenSSL Generated Certificate
X509v3 Authority Key> keyid:3E:A6:CE:D8:37:0C:F8:2E:5B:6C:59:2A:DE:39:EF:F9:AF:F3:34:45
Certificate is to be certified until Nov 30 00:17:54 2014 GMT (365days)
Sign the certificate? [y/n]:y
1 out of 1 certificate requests certified, commit? [y/n]y
Write out database with 1 new entries
Data Base Updated
[root@ahao certs]# cat /etc/pki/CA/index.txt //查看是否有证书文件
V 141130001754Z 01 unknown /C=CN/ST=HENAN/O=TUOYUANEDU/OU=tec/CN=tec.abc.com
我们还需要将证书与web服务器绑定,
[root@ahao nginx]# vim /etc/nginx/nginx.conf //在主配置文档中添加绑定项: