nginx配置http跳转https

进阶:HTTPS 跳转 现在,你的服务器同时接受 HTTP 和 HTTPS 请求。如果你希望只受理 HTTPS 请求,可以在 nginx.conf 中添加一个 301 跳转规则,告知浏览器将 HTTP 变成 HTTPS.

http {
    server {
        listen 80; 
        listen [::]:80;
        server_name example.com www.example.com;
        return 301 https://$server_name$request_uri;
    }
    server {
        listen 443 ssl;
        server_name example.com www.example.com;
        ssl_certificate /etc/letsencrypt/live/example.com/cert.pem;
        ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
        location / {
            root /var/www/html;
        }
        ...
    }
    ...
}
文章目录