0

Hi I would like to redirect all pages when a user try to access it from http to https. I already setup the ssl but if the url just typed in the http it doesn't redirect them to https. I need to redirect them to https for all pages. How do I achieve this?

server {
    listen 80;
    listen 443 ssl;
    server_name *.website.local;
    ssl_certificate /etc/nginx/ssl/server.crt;
    ssl_certificate_key /etc/nginx/ssl/server.key;
    root /var/www/website/public;

    index index.html index.htm index.php;

    charset utf-8;

    location / {
        try_files $uri $uri/ /index.php$is_args$args;
    }

    client_max_body_size 100m;

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_intercept_errors off;
        fastcgi_buffer_size 16k;
        fastcgi_buffers 4 16k;
    }

    location ~ /\.ht {
        deny all;
    }
}
1

1 Answer 1

1

Create a separate config for http 80 to redirect to https 443.

server {
    listen                  80;
    server_name             *.website.local;

    return          301 https://$host$request_uri;
}
Sign up to request clarification or add additional context in comments.

2 Comments

You don't need a separate config if you don't want one, you can add if ($scheme != "https") { return 301 https://$host$request_uri; } to your existing server config.
What config if server_name is IP(not domain name)?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.