简体   繁体   中英

Redirect HTTP to HTTPS nginx with SSL

After a lot of tries, I succeed to add proper SSL from namecheap, that will secure my domain (with multiple ports).

I have some apps - that run on NGINX. My droplet is of Digital Ocean.

So I have a few blocks with this configuration:

#this is the default 80 port 
server {
#listen 80; # - will cause nginx complain on already in use. 
listen 443;

ssl on;

ssl_certificate /my/folder/forssl/my-domain_com_chain.crt;

ssl_certificate_key /home/projects/ssl-files/my-domain.com.key;

root /var/www/my-domain.com/so-ev;

server_name my-domain.com;

#all those tries didn't help
 #   return 301 https://so-ev-qa.shop$request_uri;
#return 301 https://$server_name$request_uri;
#rewrite ^(.*) https://so-ev-qa.shop$1 permanent;


}



server {

listen 26;

ssl on;

ssl_certificate /my/folder/forssl/my-domain_com_chain.crt;

ssl_certificate_key /home/projects/ssl-files/my-domain.com.key;

root /var/www/my-domain.com/html;

server_name my-domain.com;

}

server {

listen 3000 ;

ssl on;

ssl_certificate /my/folder/forssl/my-domain_com_chain.crt;

ssl_certificate_key /home/projects/ssl-files/my-domain.com.key;

root /var/www/my-domain.com/html2;

server_name my-domain.com;

}


server {
    listen 27;
     ssl on;

               
                ssl_certificate /my/folder/forssl/my-domain_com_chain.crt;

                ssl_certificate_key /home/projects/ssl-files/my-domain.com.key;

                server_name  my-domain.com;
                index index.html index.htm;
                access_log /var/log/nginx/bmiapp.log;
                error_log  /var/log/nginx/bmiapp-error.log error;

                location / {
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header X-Forwarded-Proto $scheme;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header Host $http_host;
                proxy_pass http://127.0.0.1:26;
                proxy_redirect off;
         }
}

Currently the problem is that it's not doing redirect from http to https.

While https://example.com is secured, http://example.com isn't.

I am trying to do the simple redirecting action.

All my tries ended unsuccessfully.

If I'm trying to add block for listen 80, it's complain about“已在使用中”。 All my other tries didn't work.

Help will be appreciated, I have spent on this SSL issues a lot of hours.

I found the problem.

There was an old-client service that was running on the background.

We can use

udo lsof -i:80

To get a clue

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM