Cara Membuat SubDomain di Nginx VPS

Tutorial cara membuat subdomain di nginx vps ini sama dengan konfigurasi Server Block (Virtual Host) Nginx.

Konfigurasi Nginx Server Block

Buat file konfigurasi untuk subdomain baru

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

Sesuaikan dengan domain/subdomain anda

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)(.*)$;
        #Prevent version info leakage
        fastcgi_hide_header X-Powered-By;
        }
}

Masuk ke direktori sites-enabled

cd /etc/nginx/sites-enabled

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

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

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

mkdir /var/www/html/idnetter/blog

Restart Nginx

service nginx restart

Selamat mencoba




Share this post:

Leave a Reply

Your email address will not be published. Required fields are marked *