首页 > 编程语言 > Nginx同时支持Http和Https的配置详解
2020
10-09

Nginx同时支持Http和Https的配置详解

现在的网站支持Https几乎是标配功能,Nginx能很好的支持Https功能。下面列举一个配置同时支持Http和Https的功能。

需要注意的是:既然选择使用Https,就是为了保证通信安全,那么就没必要再用Http进行通信了。在URL中还支持Http的方式,主要是为了用户不知道网站支持Https,还是使用Http的方式进行访问。这时Nginx后台需要自动将Http请求转成Https的方式,这样就又能支持Http,又能保证通信安全了。

废话不多说,下面直接贴一个Nginx支持Http和Https的配置,是我的wordpres网站支持Https的配置,大家何以参考。

server
{
  # 开启Https
  listen 443 ssl;
  # 配置证书,免费证书怎么申请这边就不多说了。在晚上搜索腾讯云或者阿里云免费证书申请即可
  ssl_certificate /etc/nginx/conf.d/cert/4351595_www.xxx.pem;
  ssl_certificate_key /etc/nginx/conf.d/cert/4351595_www.xxx.key;
  ssl_session_timeout 5m;
  ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
  ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  ssl_prefer_server_ciphers on;
  
  server_name xxx;
  index index.html index.htm index.php;
  root /data/wwwroot/wordpress;
  error_log /var/log/nginx/wordpress-error.log crit;
  access_log /var/log/nginx/wordpress-access.log;

  # 这边用于包含其他配置
  include extra/*.conf;
  include conf.d/rewrite/wordpress.conf;

}

# 将Http请求转化成Https请求
server {
  listen 80;
  server_name xxx;
  rewrite ^/(.*) https://$server_name$request_uri? permanent;
}

到此这篇关于Nginx同时支持Http和Https的配置详解的文章就介绍到这了,更多相关Nginx同时支持Http和Https配置内容请搜索自学编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持自学编程网!

编程技巧