+2 votes
in Web & Google by (15.2k points)
I am using the VestaCP control panel on my VPS. Using the .htaccess file, I was able to redirect HTTP requests to HTTPS on the Apache server, but Nginx does not support it. How can I redirect all HTTP requests to HTTPS on the Nginx web server?

1 Answer

0 votes
by (25.6k points)
edited by

If you have root priviledges, access your VPS using root. If you have installed any certificate (e.g. letsencrypt) for your domain, there should be two .conf files in the folder /home/USER_NAME/conf/web for your domain. Make the following changes in those two files.

File1: your_domain.nginx.conf

Add the line: "return 301 https://$host$request_uri;". This will redirect all HTTP traffics to HTTPS.

server {
    listen    a.b.c.d:80;
    server_name YOUR_SERVER_NAME ;
    return 301 https://$host$request_uri;

File2: your_domain.nginx.ssl.conf

If you want to enable HTTP2 on your server, add 'ssl http2' after 443.

server {
    listen    a.b.c.d:443 ssl http2;

...