Redirect HTTP Ke HTTPS Di Nginx

Cara redirect Nginx HTTP ke HTTPS sebenarnya sangat mudah kalo sudah tau cara konfigurasinya namun kalo belum tau itu lumayan pusing nyarinya di internet sampai Pas. Langsung saja saya menggunakan linux centos dan untuk settingan nginx nya ada di '/etx/nginx/nginx.conf' untuk settingan redirectnya.

        server {
                server_name www.example.com;
                listen 202.74.239.121:80;
                rewrite ^/(.*) https://www.example.com/$1 permanent;
        }

Lakukan test konfigurasi nginx sebelum di lakukan restart nginx caranya menjalankan syntak

# nginx -t

Output setelah menjalankan syntak ini maka akan keluar output

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Status settingan sudah Oke, baru lakukan restart service nginx pada linux centos

systemctl restart nginx

Terakhir untuk memastikan lihat status nginx running atau tidaknya dengan menjalankan syntak

# systemctl status nginx

Output dari syntak tersebut akan menghasilkan output seperti berikut ini :

● nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
   Active: active (running) since Sun 2019-10-20 16:25:08 WIB; 17s ago
  Process: 15859 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
  Process: 15857 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
  Process: 15855 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
 Main PID: 15861 (nginx)
   CGroup: /system.slice/nginx.service
           ├─15861 nginx: master process /usr/sbin/nginx
           └─15863 nginx: worker process

Oct 20 16:25:08 server.asepwarkir.com systemd[1]: Stopped The nginx HTTP and reverse proxy server.
Oct 20 16:25:08 server.asepwarkir.com systemd[1]: Starting The nginx HTTP and reverse proxy server...
Oct 20 16:25:08 server.asepwarkir.com nginx[15857]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
Oct 20 16:25:08 server.asepwarkir.com nginx[15857]: nginx: configuration file /etc/nginx/nginx.conf test is successful
Oct 20 16:25:08 server.asepwarkir.com systemd[1]: Failed to parse PID from file /run/nginx.pid: Invalid argument
Oct 20 16:25:08 server.asepwarkir.com systemd[1]: Started The nginx HTTP and reverse proxy server.

Finish, silahkan lakukan clear cache pada browser untuk memastikan settingan berhasil lalu baru lakukan test akses tanpa menggunakan https.

 

Related Articles

Comments