Перенаправление не http-ссылки на HTTPS-ссылку с использованием LetsEncrypt в nginx

#ssl #nginx #ssl-certificate #lets-encrypt #nginx-config

#ssl #nginx #ssl-сертификат #позволяет зашифровать #nginx-config

Вопрос:

Я использую Nginx и LetsEncrypt для SSL-сертификата.

У меня есть этот файл nginx.conf, на который не перенаправляются non-http ссылки https . как я могу перенаправить все ссылки на https?

 user root;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

events {
    worker_connections 1024;
}

http {
    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;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

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

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    #include /etc/nginx/conf.d/*.conf;

    server {
        listen       80;
        listen       [::]:80;
        server_name  api.example.com www.api.example.com;
        proxy_read_timeout 600s;
        location /apis/media/images {
           #add_header Access-Control-Allow-Origin *;
           alias /home/example_PROD/Images/;
        }

        location / {
                proxy_pass http://127.0.0.1:10000/;
        }

    }


    server {
        server_name  example.com;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
            try_files $uri /index.html;
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }

    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}
    server {
    server_name www.example.com; # managed by Certbot
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
            try_files $uri /index.html;
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }

    listen [::]:443 ssl ; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot



}


    server {
    if ($host = example.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  example.com;
    return 404; # managed by Certbot


}


    server {
    if ($host = www.example.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


        listen       80 ;
        listen       [::]:80 ;
    server_name www.example.com;
    return 404; # managed by Certbot


}}
  

Ответ №1:

Удалите эту часть:

 server {
    listen       80;
    listen       [::]:80;
    server_name  api.example.com www.api.example.com;
    proxy_read_timeout 600s;
    location /apis/media/images {
       #add_header Access-Control-Allow-Origin *;
       alias /home/example_PROD/Images/;
    }

    location / {
            proxy_pass http://127.0.0.1:10000/;
    }

}