当前位置:首页 > CN2资讯 > 正文内容

Nginx之Https最佳实践

3天前CN2资讯

   nginx的https协议需要ssl模块的支持,我们在编译nginx时使用–with-http_ssl_module参数加入SSL模块。还需要服务器私钥,服务器证书,如果是公司对外环境,这个证书需要购买第三方的权威证书,否则用户体验得不到保障;这里我仅仅在我的vps上使用并测试。

注意:如果你购买的是第三方服务证书,那么只需要参考1.3-1.4的配置信息即可完整企业ssl配置实践。

此博文参考老男孩23期二麻博客Nginxhttps

笔者QQ572891887

Linux架构交流群:471443208

1.1检查NginxSSL模块是否安装

[root@web-node1~]# /application/nginx/sbin/nginx -V

nginx version: nginx/1.6.3

built by gcc 4.4.7 20120313 (Red Hat 4.4.7-16) (GCC)

TLS SNI support enabled

configure arguments: –prefix=/application/nginx-1.6.3 –user=nginx –group=nginx –with-http_ssl_module –with-http_stub_status_module

1.2准备私钥和证书

1.2.1创建服务器私钥

[root@web-node1~]# cd /application/nginx/conf/

[root@web-node1 conf]# mkdir key

[root@web-node1 conf]# cd key/

[root@web-node1 key]# openssl genrsa -des3 -out server.key 1024

Generating RSA private key, 1024 bit long modulus

..++++++

…++++++

e is 65537 (0x10001)

Enter pass phrase for server.key:

Verifying – Enter pass phrase for server.key:

1.2.2签发证书

[root@web-node1 key]# openssl req -new -key server.key -out server.csr

Enter pass phrase for server.key:

You are about to be asked to enter information that will be incorporated

into your certificate request.

What you are about to enter is what is called a Distinguished Name or 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) [XX]:CN

State or Province Name (full name) []:BJ

Locality Name (eg, city) [Default City]:BJ

Organization Name (eg, company) [Default Company Ltd]:SDU

Organizational Unit Name (eg, section) []:SA         

Common Name (eg, your name or your server’s hostname) []:XuBuSi

Email Address []:[email protected]

 

Please enter the following ‘extra’ attributes

to be sent with your certificate request

A challenge password []:     

An optional company name []:

1.2.3删除服务器私钥口令

[root@web-node1 key]# cp server.key server.key.ori

[root@web-node1 key]# openssl rsa -in server.key.ori -out server.key

Enter pass phrase for server.key.ori:

writing RSA key

1.2.4生成使用签名请求证书和私钥生成自签证书

[root@web-node1 key]# 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=XuBuSi/[email protected]

Getting Private key

1.3开启Nginx SSL

[root@web-node1 ~]# cat /application/nginx/conf/vhosts/www.conf

    server {

     server_nameblog.xuliangwei.com;

       #listen       80;

       listen       443;

       ssl on;

       ssl_certificate key/server.crt;

       ssl_certificate_key key/server.key;

 

location / {

          roothtml/blog;

            index  index.php index.html index.htm;

        access_log /app/logs/blog.xuliangwei.log main;

        }

    }

1.3.1重启nginx生效

[root@web-node1 ~]# /application/nginx/sbin/nginx -s reload

[root@web-node1 ~]# netstat -lntup|grep 443

tcp        0    0 0.0.0.0:443                 0.0.0.0:*                   LISTEN    1711/nginx

1.3.2测试https

由于该证书非第三方权威机构颁发,而是我们自己签发的,所以浏览器会警告,如果是对外的业务需要加密,必须使用商用第三方签名证书。

必须访问https://blog.xuliangwei.com

1.4配置重定向80端口转443端口

以上配置有个不好的地方,如果用户使用时忘了使用https或者443端口,那么将会报错,所以我们需要80端口的访问转到443端口并使用ssl加密访问。

只需要增加一个server段,使用301永久重定向。

[root@web-node1 ~]# tail -5 /application/nginx/conf/vhosts/www.conf

server {

        listen 80;

        server_name blog.xuliangwei.com;

        rewrite ^(.*) https://$server_name$1 permanent;

}

[root@web-node1 ~]# /application/nginx/sbin/nginx -t

nginx: the configuration file /application/nginx-1.6.3/conf/nginx.conf syntax is ok

nginx: configuration file /application/nginx-1.6.3/conf/nginx.conf test is successful

[root@web-node1 ~]# /application/nginx/sbin/nginx -s reload

输入blog.xuliangwei.com自动跳转https




    你可能想看:

    扫描二维码推送至手机访问。

    版权声明:本文由皇冠云发布,如需转载请注明出处。

    本文链接:https://www.idchg.com/info/19987.html

    分享给朋友:

    “Nginx之Https最佳实践” 的相关文章

    VPS Pro - 理想的虚拟专用服务器解决方案

    什么是 VPS Pro VPS Pro 是一种先进的虚拟专用服务器解决方案,提供用户高度可定制的服务器环境。与传统的共享主机或物理服务器相比,VPS Pro 以虚拟化技术为基础,让每位用户享有像独立服务器一样的资源和灵活性。这种技术不仅提升了资源的利用率,还为用户提供了更高的控制权限。 在VPS P...

    Nginx Cache Control: 如何使用 No Cache 精确管理缓存策略

    作为一名开发者,我一直非常欣赏 Nginx 作为高性能 HTTP 和反向代理服务器的能力。Nginx 不仅在稳定性和可扩展性方面表现出色,它的缓存控制功能也相当强大。通过设置响应头,Nginx 能有效地管理客户端和代理服务器的缓存行为,让我在开发和部署时能够更灵活地处理资源的缓存。 使用缓存控制的好...

    如何使用restorecon命令恢复Linux系统文件的安全上下文

    在学习Linux或进行系统管理时,可能会听到“restorecon”这个词。简单来说,restorecon是一个用于恢复文件和目录的安全上下文的命令。它的核心作用在于确保系统文件符合安全政策,帮助保持系统的安全性。想象一下,当我们对系统文件进行更改或更新时,这些文件的安全设置可能会被意外改变,这时r...

    全面解析甲骨文服务器:功能、应用与未来趋势

    甲骨文服务器概述 甲骨文服务器的定义与功能 甲骨文服务器,简单来说,是一种数据库管理系统,非常适合于处理复杂的数据和海量的信息。它不仅能够存储数据,更能够提供高效的数据管理、查询和分析功能。这样的系统通常由多个功能模块构成,能够满足企业在数据处理方面的多种需求。我一直认为,甲骨文服务器最大的优势在于...

    KVM是什么?深入了解Kernel-based Virtual Machine的功能与应用

    在谈论KVM之前,我们有必要了解它的基本定义和起源。KVM,全称为Kernel-based Virtual Machine,这是一种虚拟化技术,可以让我们在一台物理机器上运行多个虚拟机。KVM的设计是基于Linux内核的,因此,它能够充分利用Linux的强大功能。它首次出现在2006年,随着Linu...

    50GB免费存储空间:如何选择最适合你的云端存储服务

    什么是云端存储? 云端存储是一种将数据保存到远程服务器上的服务,而不是依靠本地硬盘或其他存储设备。用户可以通过互联网随时随地访问这些数据,简化了数据管理。记得第一次接触云存储时,我被这个概念深深吸引。没有了对硬盘容量的担忧,随时可以上传和下载文件,这似乎为我的数字生活带来了无限可能。 有了云端存储,...