Nginx supports Http and Https

  • 2021-08-28 21:31:48
  • OfStack

Now the website supports Https is almost standard function, Nginx can support Https function very well. Here is a configuration that supports both Http and Https.

It should be noted that since Https is chosen to ensure communication security, it is no longer necessary to use Http for communication. In URL also support Http, mainly for users do not know the website support Https, or use Http access. At this time, Nginx background needs to automatically convert Http request to Https, so that Http can be supported again and communication security can be ensured.

Needless to say, the following directly post a configuration of Nginx supporting Http and Https, which is the configuration of my wordpres website supporting Https. Why do you refer to it?


server
{
  #  Open Https
  listen 443 ssl;
  #  Configuration certificate, how to apply for free certificate here will not be said much. Search Tencent Cloud or Alibaba Cloud for free certificate application at night 
  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;

  #  This side is used to include other configurations 
  include extra/*.conf;
  include conf.d/rewrite/wordpress.conf;

}

#  Will Http Request conversion to Https Request 
server {
  listen 80;
  server_name xxx;
  rewrite ^/(.*) https://$server_name$request_uri? permanent;
}


Related articles: