linux中Nginx配置https
现在都流行给网站或者管理系统加上https,以加强网站安全,今天给大分享下如何在linux环境中,给nginx配置https:
1.查看nginx安装情况,nginx -V看看是否有装有http_ssl_module模块
nginx -V:
nginx version: nginx/1.10.3
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-17) (GCC)
built with OpenSSL 1.0.1e-fips 11 Feb 2013
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-file-aio --with-threads --with-ipv6 --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_ssl_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'
一般默认rpm安装都会带有http_ssl_module
2.防火墙开启443端口
3.确保机器上安装了openssl和openssl-devel,可以执行以下命令更新:
yum -y install gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel
4.创建https证书,我创建在/etc/nginx/目录下,等下好配置时好找
打开nginx目录:
cd /etc/nginx/
创建服务器私钥:
openssl genrsa -des3 -out server.key 1024 #根据提示输入证书口令,记住口令,下面要用
创建签名请求的证书(CSR):
openssl req -new -key server.key -out server.csr
#输入上面设置的口令
#根据提示输入相应的信息
Country Name (2 letter code) [XX]:cn #国家
State or Province Name (full name) []:zhejiang #省份
Locality Name (eg, city) [Default City]:hangzhou #城市
Organization Name (eg, company) [Default Company Ltd]:osyunwei #公司
Organizational Unit Name (eg, section) []:sys #部门
Common Name (eg, your name or your server's hostname) []:osyunwei #主机名称
...
对key进行解密:
openssl rsa -in server.key -out server_nopassword.key #对key进行解密
标记证书使用上述私钥和CSR:
openssl x509 -req -days 365 -in server.csr -signkey server_nopassword.key -out server.crt
5.配置nginx
upstream ad_server {
server 127.0.0.1 weight=1 max_fails=2;
keepalive 100;
}
server {
listen 443;
server_name 192.168.1.15;
ssl on;
ssl_certificate /etc/nginx/server.crt;
ssl_certificate_key /etc/nginx/server_nopassword.key;
location /{
proxy_pass http://ad_server;
proxy_connect_timeout 900;
proxy_send_timeout 900;
proxy_read_timeout 900;
}
}
重启nginx就可以用https://192.168.1.15访问nginx了
当然你如果是在正式环境中,并且是网站的话,就要使用商用的https证书了,并不是上面自己生成的ssl证书了