Cara Redirect HTTP ke HTTPS Nginx Permanent

Cara redirect http ke https, beberapa waktu lalu idnetter pernah share layanan sertifikat SSL gratis, jika ada yang belum baca silahkan kunjungi : SSL Gratis. Dan salah satunya dari WoSign yang memberikan SSL Gratis valid sampai 2 tahun, cara install sertifikatnya pun cukup mudah walau harus memahami apa itu SSL sertifikat dan apa fungsinya paling tidak tahu sedikit saja, artikel tentang SSL bisa anda temukan di Wikipedia.

UPDATE TERBARU : Cara redirect HTTP ke HTTPS

Setelah melakukan pendaftaran dan sedikit memodifikasi konfigurasi Nginx Server Block,  akhirnya bisa juga mencicipi HTTPS. Berikut ini panduan bagaimana cara redirect HTTP ke HTTPS di Nginx agar visitor secara permanent di redirect ke protokol HTTPS.

 

Cara Redirect HTTP ke HTTPS Nginx

Edit Nginx config pada server block, tambahkan baris berikut ini ke konfigurasi nginx, sesuaikan.

 server {
   listen 80;
   server_name idnetter.com www.idnetter.com;
   return 301 https://$server_name$request_uri;
 }

Perhatikan baris return 301… atau HTTP 301 Moved permanently, merupakan cara redirect paling efisien karena tidak ada regex untuk dievaluasi, pelajari lebih lanjut di pitfalls.

Dan juga tambahkan baris ini, jangan lupa sesuaikan link file sertifikat sslnya

 server {
   listen 443 ssl;
   server_name idnetter.com;

   # link dimana file sertifikat berada
   ssl_certificate /etc/nginx/ssl/idnetter.com_bundle.crt;
   ssl_certificate_key /etc/nginx/ssl/idnetter.com.key;

   ...
   ...
}

dari konfigurasi diatas dapat dikatakan semua pengunjung idnetter.com baik yang memakai WWW atau tidak akan dialihkan ke protokol https. konfigurasi nginx selengkapnya akan tampak seperti dibawah ini:

server {
    listen                  80;
    server_name             idnetter.com www.idnetter.com;
    return                  301 https://$server_name$request_uri;
 }
 
server {
    listen                  443 ssl;
    server_name             idnetter.com;

    client_max_body_size    5m;
    client_body_timeout     60;

    access_log              /var/log/nginx/idnetter.com-access;
    error_log               /var/log/nginx/idnetter.com-error error;

    root                    /var/www/html/idnetter/;
    index                   index.html index.php;

    ssl_certificate         /etc/nginx/ssl/idnetter.com_bundle.crt;
    ssl_certificate_key     /etc/nginx/ssl/idnetter.com.key;

   ### root directory ###
    location / {
        try_files           $uri $uri/ /index.php?$args;
    }

    ### security ###
    error_page              403 =404;
    location                ~ /\. { access_log off; log_not_found off; deny all; }
    location                ~ ~$ { access_log off; log_not_found off; deny all; }
    location                ~* wp-admin/includes { deny all; }
    location                ~* wp-includes/theme-compat/ { deny all; }
    location                ~* wp-includes/js/tinymce/langs/.*\.php { deny all; }
    location                /wp-includes/ { internal; }
    location                ~* wp-config.php { deny all; }
    location                ~* ^/wp-content/uploads/.*.(html|htm|shtml|php)$ {
        types                   { }
        default_type            text/plain;
    }

    ### disable logging ###
    location                = /robots.txt { access_log off; log_not_found off; }
    location                = /favicon.ico { access_log off; log_not_found off; }

    ### caches ###
    location                ~* \.(jpg|jpeg|gif|css|png|js|ico|html)$ { access_log off; expires max; }
    location                ~* \.(woff|svg)$ { access_log off; log_not_found off; expires 30d; }
    location                ~* \.(js)$ { access_log off; log_not_found off; expires 7d; }

    ### php block ###
    location                ~ \.php?$ {
        try_files               $uri =404;
        include                 fastcgi_params;

        # Sesuaikan file socket anda
        fastcgi_pass            unix:/var/run/gateaway.socket;

        fastcgi_param           SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_intercept_errors on;
        fastcgi_split_path_info ^(.+\.php)(.*)$;
        #Prevent version info leakage
        fastcgi_hide_header     X-Powered-By;
    }

 }

Save dan reload nginx

nginx -s reload

 

Setting WordPress

Jika anda memakai WordPress, perlu update setting pada bagian General

WordPress Address (URL) : https://example.com
Site Address (URL) : https://example.com

Dengan cara tersebut semua permalink akan memakai awalan https. Ini merupakan solusi munculnya warning SSL pada address bar (icon padlock dengan tanda silang merah) karena link gambar masih menggunakan http. Saat saya mencoba di Browser Chrome gambar juga tidak bisa tampil, alhasil setelah diset semua memuaskan.

4 thoughts on “Cara Redirect HTTP ke HTTPS Nginx Permanent

  1. aries

    mas ada tutorial buat ssl untuk multiple domain di server yang sama ? ane udah buat tpi pas ane jalankan perintah nginx -t muncul pesan [emerg] a duplicate default server for 0.0.0.0:443 in etc/nginx/sites-enabled/vb.conf:60

  2. Omar

    Tutroial yang lengkap ga ada, sepotong-sepotong contoh: dimulai dari install ssl kemudian tinggal disesuaikan pada konfigurasi server block Nginx.
    Oh error itu, kemungkinan konfigurasinya default_server di file config defaultnya Nginx gan Aries.

  3. Imam Ramadhan

    Mas Bro, apakah ada cara/tutorial untuk windows server 2016, untuk redirect dari http menjadi https via nginx?

    1. Omar

      Coba pakai

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

Add a comment