nginx的https协议需要ssl模块的支持,我们在编译nginx时使用--with-http_ssl_module参数加入SSL模块。还需要服务器私钥,服务器证书,如果是公司对外环境,这个证书需要购买第三方的权威证书,否则用户体验得不到保障。
[root@WZKX-WEB01~]# /application/nginx/sbin/nginx -V
nginx version:nginx/1.9.13
built by gcc4.4.7 20120313 (Red Hat 4.4.7-16) (GCC)
built withOpenSSL 1.0.1e-fips 11 Feb 2013
TLS SNI supportenabled
configure arguments: --prefix=/application/nginx-1.9.13--user=nginx --group=nginx --with-http_ssl_module--with-http_stub_status_module
[root@WZKX-WEB01~]# cd /application/nginx/conf/
[root@WZKX-WEB01conf]# mkdir key 《====创建要生成证书的目录
[root@WZKX-WEB01conf]# cd key 《====进入要生成证书的目录
[root@WZKX-WEB01key]# openssl genrsa -des3 -out server.key 1024 《====使用openssl创建服务器私钥
..++++++
…++++++
e is 65537 (0x10001)
Enter pass phrase for server.key:
Verifying – Enter pass phrase forserver.key:
[root@WZKX-WEB01 key]# openssl req -new -key server.key -out server.csr
说明:这是用步骤1的密钥生成证书请求文件server.csr, 这一步提很多问题,一一输入
Enter pass phrase for server.key:
You are about to be asked to enter information thatwill be incorporated
into your certificate request.
What you are about to enter is what is called aDistinguished Name or a DN.
There are quite a few fields but you can leave someblank
For some fields there will be a default value,
If you enter ‘.’, the field will be left blank.
—–
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:BJ
Locality Name (eg, city) [Default City]:BJ
Organization Name (eg, company) [Default CompanyLtd]:SDU
Organizational Unit Name (eg, section)[]:SA
Common Name (eg, your name or your server’shostname) []:TANK
Email Address []:254702202@qq.com
Please enter the following ‘extra’ attributes
to be sent with your certificate request
A challenge password[]:
An optional company name []:
[root@WZKX-WEB01key]# cp server.key server.key.ori
[root@WZKX-WEB01key]# openssl rsa -in server.key.ori -out server.key 《==创建要生成证书的目录
Enter pass phrase for server.key.ori:
writing RSA key
[root@WZKX-WEB01key]# openssl x509 -req -days 365 -in server.csr-signkey server.key -out server.crt
Signature ok
subject=/C=CN/ST=BJ/L=BJ/O=SDU/OU=SA/CN=TANK/emailAddress=254702202@qq.com
Getting Private key
说明:这是用步骤1,2的的密钥和证书请求生成证书server.crt。 【-days参数指明证书有效期,单位为天】
[root@WZKX-WEB01key]# pwd
/application/nginx/conf/key
[root@WZKX-WEB01key]# ls
server.crt server.csr server.key server.key.ori
[root@WZKX-WEB01 key]# cat /application/nginx/conf/nginx.conf
server {
ssl on;
ssl_certificate /application/nginx/conf/key/server.crt;
ssl_certificate_key /application/nginx/conf/key/server.key;
listen 443;
server_name www.miaomiao.com;
location /{
root /opt/web/www.miaomiao.com;
index index.html index.htm;
}
}
[root@WZKX-WEB01 key]# /application/nginx/sbin/nginx -s reload
[root@WZKX-WEB01 key]# netstat -lntup|grep 443
tcp 0 0 0.0.0.0:443 0.0.0.0:* LISTEN 11404/nginx
由于该证书非第三方权威机构颁发,而是我们自己签发的,所以浏览器会警告,如果是对外的业务需要加密,必须使用商用第三方签名证书
以上配置有个不好的地方,如果用户使用时忘了使用https或者443端口,那么将会报错,所以我们需要80端口的访问转到443端口并使用ssl加密访问。【生产环境中,用户也基本不可能用https去访问一个网站,所以实现跳转很有必要。】
只需要增加一个server段,使用301永久重定向。
[root@WZKX-WEB01 key]# cat /application/nginx/conf/nginx.conf
user nginx;
worker_processes 1;
error_log /opt/web/logs/error.log;
worker_rlimit_nofile 65535;
pid logs/nginx.pid;
events {
use epoll;
worker_connections 65535;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
log_format commonlog ‘$remote_addr - $remote_user [$time_local] "$request" ‘
‘$status $body_bytes_sent "$http_referer" ‘
‘"$http_user_agent""$http_x_forwarded_for"‘;
access_log /opt/web/logs/access.log commonlog;
server {
listen 80;
server_name www.miaomiao.com;
rewrite ^/(.*) https://www.miaomiao.com/$1 permanent;
location /{
root /opt/web/www.miaomiao.com;
index index.html index.htm;
}
}
server {
ssl on;
ssl_certificate /application/nginx/conf/key/server.crt;
ssl_certificate_key /application/nginx/conf/key/server.key;
listen 443;
server_name www.miaomiao.com;
location /{
root /opt/web/www.miaomiao.com;
index index.html index.htm;
}
}
}
[root@WZKX-WEB01 key]# /application/nginx/sbin/nginx -t
nginx: the configuration file/application/nginx-1.9.13/conf/nginx.conf syntax is ok
nginx: configuration file/application/nginx-1.9.13/conf/nginx.conf test is successful
[root@WZKX-WEB01 key]# /application/nginx/sbin/nginx-s reload
本文出自 “学习之旅” 博客,谢绝转载!
原文地址:http://blogtank.blog.51cto.com/10511324/1840052