正确的让Nginx的HTTP和HTTPS站点在一个Server配置内共存

From:http://nginx.org/en/docs/http/configuring_https_servers.html#single_http_https_server

It is good practice to configure separate servers for HTTP and HTTPS protocols from the very start. Although their functionalities currently seem equal, this may change significantly in the future and using a consolidated server may become problematic. However, if HTTP and HTTPS servers are equal, and you prefer not to think about the future, you may configure a single server that handles both HTTP and HTTPS requests by deleting the directive “ssl on” and adding the “ssl” parameter for *:443 port:

如果两个虚拟主机内容一样 只是端口不一样 一个http 一个https 何必分开写呢?

server {
listen 80;
listen 443 ssl;
server_name www.nginx.com;
ssl_certificate www.nginx.com.crt;
ssl_certificate_key www.nginx.com.key;
...
}

要让https和http并存,不能在配置文件中使用ssl on,必须在端口上制定ssl选项
(nginx 0.8.21)之前ssl选项只能和default一起使用

之前我写过一篇文章:我的Nginx分布式配置文件管理方案 里面就使用了https和http共用一个配置文件 但是我里面使用了ssl on 却没什么问题 我在测试机上怎么也不能重现这个。一台生产环境的服务器已经那样工作了一段时间里 暂时不方便停机测试,先不管他 记录下来 这个才是官方正宗的方法。之前的问题到后面再去排查。

话说这样Nginx添加多端口虚拟主机很方便了
想起apache, 好像不能只在一个虚拟主机配置节里面同时指定N个端口吧
必须重复的写 ,改动一个配置起来也很麻烦,生怕漏掉了哪个主机配置节
当然如果是我无知 欢迎apache党指正。

Author Info :
  • From:正确的让Nginx的HTTP和HTTPS站点在一个Server配置内共存
  • URL:https://blog.ihipop.com/2011/05/2386.html
  • Please Reserve This Link,Thanks!
  • 发表回复

    您的电子邮箱地址不会被公开。 必填项已用 * 标注