Implementation of nginx Proxy from Port 80 to Port 443

  • 2021-09-05 01:16:15
  • OfStack

The nginx. conf configuration file is as follows


user nginx;
worker_processes 1;

error_log /var/log/nginx/error.log warn;
pid    /var/run/nginx.pid;


events {
  worker_connections 1024;
}


http {
  include    /etc/nginx/mime.types;
  default_type application/octet-stream;

  log_format main '$remote_addr - $remote_user [$time_local] "$request" '
           '$status $body_bytes_sent "$http_referer" '
           '"$http_user_agent" "$http_x_forwarded_for"';

  access_log /var/log/nginx/access.log main;

  sendfile    on;
  #tcp_nopush   on;

  keepalive_timeout 65;

  #gzip on;

  include /etc/nginx/conf.d/*.conf;
 
 #  In the following attributes, ssl The attribute at the beginning is related to certificate configuration, and other attributes should be configured according to your own needs. 
 server {
 listen 443 ssl;  #SSL The protocol access port number is 443 . If not added here ssl , may cause Nginx Unable to start. 
 server_name localhost; # Will localhost Modify the domain name bound to your certificate, for example: www.example.com . 
 root html;
 index index.html index.htm;
 ssl_certificate /etc/nginx/huashengshu.top.pem;  # Replace with the file name of your certificate. 
 ssl_certificate_key /etc/nginx/huashengshu.top.key;  # Replace with the key file name of your certificate. 
 ssl_session_timeout 5m;
 ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4; # Use this encryption kit. 
 ssl_protocols TLSv1 TLSv1.1 TLSv1.2;  # Use this protocol for configuration. 
 ssl_prefer_server_ciphers on;  
 location / {
  root /etc/nginx/hss;  # Site directory. 
  index index.html index.htm;  
 }
 }
 

 server {
 listen 80;
 server_name huashengshu.top;
 rewrite ^(.*)$ https://${server_name}$1 permanent; 
 }

}

What works is that


 server {
 listen 80;
 server_name huashengshu.top;
 rewrite ^(.*)$ https://${server_name}$1 permanent; 
 }

Related articles: