Cara membuat Subdomain di Nginx

Tutorial cara membuat subdomain di Nginx, sebenarnya konfigurasinya ini sama seperti setup server block atau jika di Apache dikenal dengan virtual host.

Menambahkan A record

Tambahkan A record pada DNS

blog.idnetter.com. IN A 192.168.0.1

Membuat konfigurasi Nginx server block

Buat file konfigurasi untuk subdomain baru

vi /etc/nginx/sites-available/blog.idnetter.com.conf

Silahkan disesuaikan

server {
    listen 80;
    server_name www.blog.idnetter.com;
    rewrite ^(.*) http://blog.idnetter.com$1 permanent;
}

server {
    listen 80;
    server_name blog.idnetter.com;

    client_max_body_size 5m;
    client_body_timeout 60;

    access_log /var/log/nginx/access.idnetter.blog;
    error_log /var/log/nginx/error.idnetter.blog.log error;

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

    ### 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; }

    ### 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;
        fastcgi_pass unix:/var/run/php-fpm.socket;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_intercept_errors on;
        fastcgi_split_path_info ^(.+\.php)(.*)$;
        fastcgi_hide_header X-Powered-By;
        }
}

Link konfig file yang kita buat di direktori sites-available tadi ke sites-enabled

ln -s /etc/nginx/sites-available/blog.idnetter.com.conf /etc/nginx/sites-enabled/

Buat folder untuk menyimpan data-data untuk subdomain yang anda buat, sebagai contoh disini subdomain blog

mkdir /var/www/html/blog

Restart Nginx

service nginx restart

Selamat mencoba

Add a comment