生成证书:
1.首先使用openssl执行如下命令生成一个key:
openssl genrsa -des3 -out nginx.key 1024
然后他会要求你输入这个key文件的密码。不推荐输入。因为以后要给nginx使用。每次reload nginx配置时候都要你验证这个PAM密码的。
由于生成时候必须输入密码。你可以输入后执行如下代码复制一个nginx2.key文件,此key文件无密码。
openssl rsa -in nginx.key -out nginx2.key
2.然后使用openssl 根据这个key文件生成证书请求文件:
openssl req -new -key nginx2.key -out nginx.csr
以上命令生成时候要填很多东西 一个个看着写吧(可以随便,毕竟这是自己生成的证书,但是如果使用java程序访问时,需要将在输入用户名或服务器名时,输入自己的域名,不然会报找不到匹配的域名证书错误)
3.最后根据这2个文件生成crt证书文件:
openssl x509 -req -days 3650 -in nginx.csr -signkey nginx2.key -out nginx.crt
4.最后使用到的文件是key和crt文件。如果需要用pfx 可以用以下命令生成:
openssl pkcs12 -export -inkey nginx2.key -in nginx.crt -out nginx.pfx
配置nginx https:
需要在nginx.conf配置文件中添加:
server { listen 443 ssl; server_name httpfs.test.com; ssl_protocols SSLv2 SSLv3 TLSv1; #ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP; ssl_certificate ssl/nginx.crt; ssl_certificate_key ssl/nginx2.key; ssl_session_cache shared:SSL:1m; ssl_session_timeout 5m; ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP; ssl_prefer_server_ciphers on; location / { proxy_pass http://httpfs/; } }
我的nginx.conf配置文件:
#user nobody; 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; #log_format main ‘$remote_addr - $remote_user [$time_local] "$request" ‘ # ‘$status $body_bytes_sent "$http_referer" ‘ # ‘"$http_user_agent" "$http_x_forwarded_for"‘; #access_log logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; upstream httpfs { server 127.0.0.1:14000; } server { listen 80; server_name httpfs.test.com; #charset koi8-r; #access_log logs/host.access.log main; location / { proxy_pass http://httpfs/; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } # HTTPS server server { listen 443 ssl; server_name httpfs.test.com; ssl_protocols SSLv2 SSLv3 TLSv1; #ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP; ssl_certificate ssl/nginx.crt; ssl_certificate_key ssl/nginx2.key; ssl_session_cache shared:SSL:1m; ssl_session_timeout 5m; ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP; #ssl_ciphers HIGH:!aNULL:!MD5; ssl_prefer_server_ciphers on; location / { proxy_pass http://httpfs/; } } }
重启nginx后,使用https访问就可以了。