如何在Nginx不绑定域名下使用SSL/TLS证书?
<h1 id="前提">前提</h1><p>该文主要记录如何在没有购买域名的情况下使用SSL/TLS协议,即地址前面的<strong>http</strong>变成了<strong>https</strong>。但是这样的SSL协议是会被浏览器认为是不安全的。在开发或者测试环境可以这样搞,生产环境下还是乖乖的买个域名吧。</p>
<h1 id="ssl证书">SSL证书</h1>
<h2 id="第一步">第一步</h2>
<p>首先到https://csr.chinassl.net/generator-csr.html这里生成SSL秘钥(私钥)和等会拿去生成SSL证书的CSR文件。里面内容可以随便填,域名啥的随便填都没关系。保存好这两个文件。</p>
<h2 id="第二步">第二步</h2>
<p>拿刚才的CSR文件到https://csr.chinassl.net/free-ssl.html这里生成SSL证书。</p>
<p>到这里为止,我们只需要记住<strong>秘钥</strong>和<strong>SSL证书</strong>的存储路径,在nginx配置文件当中需要使用到。<br>
假设存到这里吧。</p>
<pre><code class="language-bash">/etc/ssl/my_domain/my_domain.ssl
/etc/ssl/my_domain/my_domain.private
</code></pre>
<p>我这里只是改了文件的后缀而已,并不影响使用。文件的后缀名你们自行决定也可以。</p>
<h1 id="nginx添加ssl模块">Nginx添加SSL模块</h1>
<p>先查看Nginx以前安装过的模块,避免编译后覆盖了之前添加的模块。进入到你的nginx安装包目录。执行以下命令</p>
<pre><code class="language-bash"># ./objs/nginx -V
nginx version: nginx/1.16.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)
built with OpenSSL 1.0.2k-fips26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --with-http_realip_module
</code></pre>
<p>主要看<code>configure arguments</code>这一行,那么我之前的预编译命令就是如下,而如果没有自定义添加过任何模块那么这里应该为空的</p>
<pre><code class="language-bash">./configure --prefix=/usr/local/nginx --with-http_realip_module
</code></pre>
<p>现在需要添加SSL模块,那么命令如下:</p>
<pre><code class="language-bash">./configure --prefix=/usr/local/nginx --with-http_realip_module \
--with-http_ssl_module
</code></pre>
<p>然后执行<code>make</code>命令,已经安装过安装过nginx的(即执行过<code>make install</code>),就不要执行 <code>make install</code>,不然把你之前安装好的nginx文件覆盖掉。<br>
当然,未安装Nginx的就可以执行<code>make install</code>命令了。</p>
<h1 id="更新nginx启动文件">更新Nginx启动文件</h1>
<h2 id="方式一停止nginx服务更新">方式一:停止Nginx服务更新</h2>
<pre><code class="language-bash">cd /usr/local/nginx/sbin/
./nginx -s stop
mv ./nginx ./nginx.old
cp nginx安装包目录/objs/nginx ./nginx
</code></pre>
<h2 id="方式二热部署更新">方式二:热部署更新</h2>
<p>可以参考我公众号的文章:https://mp.weixin.qq.com/s/o7rkczakPNiys1KM7Z87EA</p>
<h1 id="配置文件">配置文件</h1>
<pre><code class="language-bash">vim /usr/local/nginx/conf/nginx.conf
</code></pre>
<p>配置文件我只摘取了server模块,如下:</p>
<pre><code class="language-nginx"> server {
listen 80;
server_name 127.0.0.1;
location / {
# 重定向到https
rewrite^/(.*)https://$host$1 permanent;
}
}
server {
listen 443 ssl;
server_name127.0.0.1;
ssl_certificate /etc/ssl/my_domain/my_domain.ssl; # ssl证书存储路径
ssl_certificate_key /etc/ssl/my_domain/my_domain.private; # 秘钥存储路径
# ssl的一些配置
ssl_session_cache shared:SSL:1m;
ssl_session_timeout5m;
ssl_prefer_server_cipherson;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #开启TLS协议
location / {
root html;
indexindex.html index.htm;
}
}
</code></pre>
<p>此时输入ip地址,你就能看到<code>https</code>了。</p>
<h1 id="扩展知识">扩展知识</h1>
<h2 id="多个ssl模块">多个SSL模块</h2>
<p>当nginx的多个模块都需要使用SSL协议时,如PC端的前端项目使用了80端口转发,手机端使用了81端口转发。那么可以改成如下:</p>
<pre><code class="language-nginx"> server {
# PC端
listen 80;
server_name 127.0.0.1;
location / {
# 重定向到https,https默认端口是443
rewrite^/(.*)https://$host$1 permanent;
}
}
server {
# 手机端
listen 81;
server_name 127.0.0.1;
location / {
# 重定向到https,指定跳转到8443端口
rewrite^/(.*)https://$host:8443$1 permanent;
}
}
server {
listen 443 ssl;
server_name127.0.0.1;
ssl_certificate /etc/ssl/my_domain/my_domain.ssl; # ssl证书存储路径
ssl_certificate_key /etc/ssl/my_domain/my_domain.private; # 秘钥存储路径
ssl_session_cache shared:SSL:1m;
ssl_session_timeout5m;
ssl_prefer_server_cipherson;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #开启TLS协议
location / {
root html;
indexindex.html index.htm;
}
}
server {
listen 8443 ssl;
server_name127.0.0.1;
ssl_certificate /etc/ssl/my_domain/my_domain.ssl; # ssl证书存储路径
ssl_certificate_key /etc/ssl/my_domain/my_domain.private; # 秘钥存储路径
ssl_session_cache shared:SSL:1m;
ssl_session_timeout5m;
ssl_prefer_server_cipherson;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #开启TLS协议
location / {
root html;
indexindex.html index.htm;
}
}
</code></pre>
<h2 id="443端口转发">443端口转发</h2>
<p>https的默认端口是443,而没有root权限的用户启动时,nginx会提示没有权限使用443端口,此时则需要使用端口转发规则,把443转发到其它端口,如8443。那么需要root用户执行以下命令</p>
<pre><code class="language-bash">iptables -t nat -A PREROUTING -p tcp --dport 443 -j REDIRECT --to-port 8443
iptables -t nat -nL --line
service iptables save
</code></pre>
<p>然后把nginx配置文件的监听端口改成<code>8443 ssl</code></p>
<pre><code class="language-nginx"> server {
listen 80;
server_name 127.0.0.1;
location / {
# 重定向到https,https默认端口是443,因为端口转发规则,转发到8443
rewrite^/(.*)https://$host$1 permanent;
}
}
server {
listen 8443 ssl;
server_name127.0.0.1;
...
}
</code></pre>
<h1 id="总结">总结</h1>
<p>OK,这就是最近工作上需要完成的一个功能,还是自己太菜了。总结一下,希望也能帮到别人。~Thanks♪(・ω・)ノ</p>
<blockquote>
<p>个人博客网址: https://colablog.cn/</p>
</blockquote>
<p>如果我的文章帮助到您,可以关注我的微信公众号,第一时间分享文章给您<br>
<img src="http://qiniuyun.colablog.cn/%E4%BA%8C%E7%BB%B4%E7%A0%81.jpg" alt="微信公众号" loading="lazy"></p><br><br>
来源:https://www.cnblogs.com/Johnson-lin/p/14227869.html
頁:
[1]