Cara Redirect HTTP ke HTTPS Nginx Permanent

By | April 14, 2016

Cara redirect http ke https, beberapa waktu lalu idnetter pernah share layanan sertifikat SSL free, jikalau ada yang belum baca silahkan kunjungi : SSL Gratis. Dan salah satunya dari WoSign yang memberikan SSL Free valid hingga 2 tahun, cara install sertifikatnya pun lumayan gampang walau mesti memahami apa itu SSL sertifikat dan apa fungsinya sangat tak tahu sedikit saja, artikel mengenai SSL dapat kamu dapatkan di Wikipedia.

UPDATE TERBARU : Cara redirect HTTP ke HTTPS

Setelah mengerjakan pendaftaran dan sedikit memodifikasi konfigurasi Nginx Server Block,  akhirnya dapat pun 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

Menej Nginx config di 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, adalah sebuah cara redirect sangat efisien karena tak ada regex bagi dievaluasi, pelajari lebih lanjut di pitfalls.

Dan pun 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 dinamakan segala pengunjung idnetter.com bagus yang memakai WWW atau tak 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 kamu
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 informasi leakage
fastcgi_hide_header X-Powered-By;
}

}

Save dan reload nginx

nginx -s reload

 

Pengaturan WordPress

Bila kamu memakai WordPress, perlu update pengaturan di bagian General

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

Dengan cara tersebut segala permalink akan memakai awalan https. Ini adalah sebuah solusi munculnya warning SSL di address bar (icon padlock dengan tanda silang merah) karena link gambar masih menggunakan http. Ketika saya mencoba di Browser Chrome gambar pun tak dapat tampil, alhasil setelah diset segala memuaskan.


Sumber https://idnetter.com