Cara membuat Subdomain di Nginx

By | September 25, 2015

Panduan cara membangun subdomain di Nginx, hakekatnya konfigurasinya ini sama seperti setup server block atau apabila di Apache dikenal dengan virtual host.

Menambahkan A record

Tambahkan A record di DNS

blog.idnetter.com. IN A 192.168.0.1

Membangun konfigurasi Nginx server block

Bikin file konfigurasi bagi 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 bikin di direktori sites-available tadi ke sites-enabled

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

Bikin folder bagi menyimpan data-data bagi subdomain yang kamu bikin, selaku contoh disini subdomain blog

mkdir /var/www/html/blog

Restart Nginx

service nginx restart

Selamat mencoba


Sumber https://idnetter.com