首页 后端开发 php教程 给站点加上https支持

给站点加上https支持

Aug 08, 2016 am 09:29 AM
nginx sudo

本文引用自http://blog.linjunhalida.com/blog/using-https-for-rails/
https是针对http的加密协议,它可以保证用户访问网站的过程中,通讯的数据是加密的,这样可以防止第三方监听,保护用户隐私。这里总结一下如何给Rails加上https的支持。首先,假设你的rails已经跑起来了,在http://yourserver.com,服务器是ubuntu,本地的访问方式是127.0.0.1:8787,那么你需要利用nginx来提供https的服务。首先安装nginx和openssl:
<codeandale mono console new font-style:inherit font-variant:inherit font-weight:inherit line-height:inherit vertical-align:baseline>sudo apt-get install nginx openssl
</codeandale>
登录后复制
生成服务器的秘钥公钥:
<codeandale mono console new font-style:inherit font-variant:inherit font-weight:inherit line-height:inherit vertical-align:baseline>openssl req -new -nodes -keyout server.key -out server.csr
openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
</codeandale>
登录后复制
生成的几个文件解释:
  • server.key 服务器的私钥。
  • server.csr (certificate signing request) https证书签名请求。
  • server.crt 生成的服务器证书。
  • 然后有这些文件,我们可以配置nginx服务了。

    生成nginx的配置文件:

    <codeandale mono console new font-style:inherit font-variant:inherit font-weight:inherit line-height:inherit vertical-align:baseline>sudo touch /etc/nginx/sites-available/yourserver
    sudo ln -s /etc/nginx/sites-available/yourserver /etc/nginx/sites-enabled
    sudo vi /etc/nginx/sites-available/yourserver
    </codeandale>
    登录后复制

    里面的内容:

    <span>1</span><span>2</span><span>3</span><span>4</span><span>5</span><span>6</span><span>7</span><span>8</span><span>9</span><span>10</span><span>11</span><span>12</span><span>13</span><span>14</span><span>15</span><span>16</span><span>17</span><span>18</span><span>19</span><span>20</span><span>21</span><span>22</span><span>23</span><span>24</span><span>25</span>
    登录后复制
    <codeandale mono console new background:rgb><span><span>upstream unicorn {
    </span><span>  server 127.0.0.1:8787 fail_timeout=0;
    </span><span>}
    </span><span>server {
    </span><span>  listen       443;
    </span><span>  server_name  yourserver.com;
    </span><span></span><span>  ssl                  on;
    </span><span>  ssl_certificate      yourpath/server.crt;
    </span><span>  ssl_certificate_key  yourpath/server.key;
    </span><span></span><span>  ssl_session_timeout  5m;
    </span><span></span><span>  ssl_protocols  SSLv2 SSLv3 TLSv1;
    </span><span>  ssl_ciphers  HIGH:!aNULL:!MD5;
    </span><span>  ssl_prefer_server_ciphers   on;
    </span><span></span><span>  location / {
    </span><span>      proxy_set_header Host $host;
    </span><span>      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    </span><span>      proxy_set_header X-Forwarded-Proto https;
    </span><span>      proxy_redirect off;
    </span><span>      proxy_pass http://localserver;
    </span><span>  }
    </span><span>}</span></span></codeandale>
    登录后复制

    需要修改里面的server_name,yourpath。

    然后重新启动nginx:

    <codeandale mono console new font-style:inherit font-variant:inherit font-weight:inherit line-height:inherit vertical-align:baseline>sudo service nginx restart
    </codeandale>
    登录后复制

    如果没有报错,那么你就可以通过https://yourserver.com来访问你的网站了。

    不过,浏览器会阻止你继续访问,或者需要你的确认。 浏览器会保存一份可信网站的列表,你的服务器加密是自己生成的,不在里面。 如果你的网站是商用的,最好去注册一下。这里有一个指引。

    引用资料:

    • railscast

    Posted by 机械唯物主义 Mar 24th, 2013  rails


    以上就介绍了给站点加上https支持,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

    本站声明
    本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

    热AI工具

    Undresser.AI Undress

    Undresser.AI Undress

    人工智能驱动的应用程序,用于创建逼真的裸体照片

    AI Clothes Remover

    AI Clothes Remover

    用于从照片中去除衣服的在线人工智能工具。

    Undress AI Tool

    Undress AI Tool

    免费脱衣服图片

    Clothoff.io

    Clothoff.io

    AI脱衣机

    AI Hentai Generator

    AI Hentai Generator

    免费生成ai无尽的。

    热门文章

    R.E.P.O.能量晶体解释及其做什么(黄色晶体)
    1 个月前 By 尊渡假赌尊渡假赌尊渡假赌
    R.E.P.O.最佳图形设置
    1 个月前 By 尊渡假赌尊渡假赌尊渡假赌
    威尔R.E.P.O.有交叉游戏吗?
    1 个月前 By 尊渡假赌尊渡假赌尊渡假赌

    热工具

    记事本++7.3.1

    记事本++7.3.1

    好用且免费的代码编辑器

    SublimeText3汉化版

    SublimeText3汉化版

    中文版,非常好用

    禅工作室 13.0.1

    禅工作室 13.0.1

    功能强大的PHP集成开发环境

    Dreamweaver CS6

    Dreamweaver CS6

    视觉化网页开发工具

    SublimeText3 Mac版

    SublimeText3 Mac版

    神级代码编辑软件(SublimeText3)

    怎么查看nginx是否启动 怎么查看nginx是否启动 Apr 14, 2025 pm 01:03 PM

    确认 Nginx 是否启动的方法:1. 使用命令行:systemctl status nginx(Linux/Unix)、netstat -ano | findstr 80(Windows);2. 检查端口 80 是否开放;3. 查看系统日志中 Nginx 启动消息;4. 使用第三方工具,如 Nagios、Zabbix、Icinga。

    nginx在windows中怎么配置 nginx在windows中怎么配置 Apr 14, 2025 pm 12:57 PM

    如何在 Windows 中配置 Nginx?安装 Nginx 并创建虚拟主机配置。修改主配置文件并包含虚拟主机配置。启动或重新加载 Nginx。测试配置并查看网站。选择性启用 SSL 并配置 SSL 证书。选择性设置防火墙允许 80 和 443 端口流量。

    linux怎么查看nginx是否启动 linux怎么查看nginx是否启动 Apr 14, 2025 pm 12:48 PM

    在 Linux 中,使用以下命令检查 Nginx 是否已启动:systemctl status nginx根据命令输出进行判断:如果显示 "Active: active (running)",则 Nginx 已启动。如果显示 "Active: inactive (dead)",则 Nginx 已停止。

    nginx怎么配置云服务器域名 nginx怎么配置云服务器域名 Apr 14, 2025 pm 12:18 PM

    在云服务器上配置 Nginx 域名的方法:创建 A 记录,指向云服务器的公共 IP 地址。在 Nginx 配置文件中添加虚拟主机块,指定侦听端口、域名和网站根目录。重启 Nginx 以应用更改。访问域名测试配置。其他注意事项:安装 SSL 证书启用 HTTPS、确保防火墙允许 80 端口流量、等待 DNS 解析生效。

    nginx304错误怎么解决 nginx304错误怎么解决 Apr 14, 2025 pm 12:45 PM

    问题的答案:304 Not Modified 错误表示浏览器已缓存客户端请求的最新资源版本。解决方案:1. 清除浏览器缓存;2. 禁用浏览器缓存;3. 配置 Nginx 允许客户端缓存;4. 检查文件权限;5. 检查文件哈希;6. 禁用 CDN 或反向代理缓存;7. 重启 Nginx。

    linux怎么启动nginx linux怎么启动nginx Apr 14, 2025 pm 12:51 PM

    在 Linux 中启动 Nginx 的步骤:检查 Nginx 是否已安装。使用 systemctl start nginx 启动 Nginx 服务。使用 systemctl enable nginx 启用在系统启动时自动启动 Nginx。使用 systemctl status nginx 验证启动是否成功。在 Web 浏览器中访问 http://localhost 查看默认欢迎页面。

    nginx403怎么解决 nginx403怎么解决 Apr 14, 2025 am 10:33 AM

    如何解决 Nginx 403 Forbidden 错误?检查文件或目录权限;2. 检查 .htaccess 文件;3. 检查 Nginx 配置文件;4. 重启 Nginx。其他可能原因还包括防火墙规则、SELinux 设置或应用程序问题。

    怎么启动nginx服务器 怎么启动nginx服务器 Apr 14, 2025 pm 12:27 PM

    启动 Nginx 服务器需要按照不同操作系统采取不同的步骤:Linux/Unix 系统:安装 Nginx 软件包(例如使用 apt-get 或 yum)。使用 systemctl 启动 Nginx 服务(例如 sudo systemctl start nginx)。Windows 系统:下载并安装 Windows 二进制文件。使用 nginx.exe 可执行文件启动 Nginx(例如 nginx.exe -c conf\nginx.conf)。无论使用哪种操作系统,您都可以通过访问服务器 IP

    See all articles