Saturday, July 11, 2020

Konfigurasi Virtual Host Nginx - Centos 7

Bagi teman-teman yang masih belajar bagaimana cara membuat virtual host nginx di centos maka saya mau berbagi pengalaman, cara install nginx bisa lihat disini. jika teman-teman belum tau apa itu virtual host maka coba baca-baca di artikel yang lagi apa itu virtual host dan fungsi nya. cara membuat nya bisa lihat di bawah ini.
Step 1. lihat pada direktori " /etc/nginx/ " disana ada file nginx.conf dan ubah file tersebut.
            Copy / backup file yang aslinya.
            [root@nginx ~]# mv /etc/nginx/nginx.conf mv /etc/nginx/nginx.conf.backup
            Lalu buat kembali file yang baru.
            [root@nginx ~]# vi /etc/nginx/nginx.conf

Step 2. jika teman-teman ingin meletakan vhost di folder " sites-available " maka buat folder terlebih dahulu. untuk di centos tidak langsung terbuat tetapi jika teman-teman membuat nya di ubuntu maka langsung terbuat folder tersebuat dan akan saya bahas di artikel selanjut nya disini.

Step 3. teman-teman bisa copi paste file berikut ini, atau bisa cari di artikel yang lain nya.
         [root@nginx ~]# vi /etc/nginx/nginx.conf

         user  nginx;
worker_processes  auto;

worker_rlimit_nofile 200000;
error_log  /var/log/nginx/error.log crit;
pid        /var/run/nginx.pid;

events {
    worker_connections  4000;
    use epoll;
    multi_accept on;
}


http {
        server_tokens off;
        add_header X-Frame-Options SAMEORIGIN;
        add_header X-Content-Type-Options nosniff;
        add_header X-XSS-Protection "1; mode=block";

        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 3600;
        fastcgi_read_timeout 3600;
        types_hash_max_size 2048;
        client_max_body_size 300M;
        include       /etc/nginx/mime.types;
        default_type  application/octet-stream;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_prefer_server_ciphers on;
        access_log  /var/log/nginx/access.log;
        gzip on;
        gzip_disable "msie6";
        gzip_vary on;
        gzip_proxied any;
        gzip_comp_level 6;
        gzip_buffers 16 8k;
        gzip_http_version 1.1;
    gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript application/vnd.ms-fontobject application/x-font-ttf font/opentype image/svg+xml image/x-icon application/x-javascript;

    include /etc/nginx/conf.d/*.conf;

Setelah selesai jangan lupa di save " esc " lalu " shift+: " ketik " wq! " dan enter

Step 4. buat virtualhost di bagian " conf.d "
         [root@nginx ~]# vi /etc/nginx/conf.d/vhost.conf
         atau temen-temen bisa jg copi paste file berikut ini
         
         upstream nginx {
   ip_hash;
   server 127.0.0.1:80 max_fails=3 fail_timeout=10s;
   server 127.0.0.2:80 max_fails=3 fail_timeout=10s;
}


server {
           listen 80;
           server_name nama_domain.com;
           return 301 https://$server_name$request_uri;
           location / {
                proxy_pass      http://nginx;
                }
        }


server {
           listen 443 ssl;
           server_name nama_domain.com;
           ssl_certificate         /etc/nginx/ssl/scertificate.crt;
           ssl_certificate_key     /etc/nginx/ssl/scertificate_key.key;
           ssl_dhparam             /etc/nginx/ssl/dhparam.pem;
           ssl_session_cache       shared:SSL:50m;
           ssl_session_timeout     1d;
           ssl_session_tickets     off;
           ssl_ciphers          ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA:ECDHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA256:DHE-RSA-AES256-SHA:ECDHE-ECDSA-DES-CBC3-SHA:ECDHE-RSA-DES-CBC3-SHA:EDH-RSA-DES-CBC3-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:DES-CBC3-SHA:!DSS;

           error_log /var/log/nginx/vhost-error.log;
           access_log /var/log/nginx/vhost-access.log;

           location / {
                        proxy_pass http://nginx;
                        proxy_set_header Host $host;
                        proxy_set_header X-Real-IP $remote_addr;
                        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                        proxy_set_header X-Forwarded-Proto $scheme;
                        client_max_body_size   64M;
                      }
           location /mocap {
                        proxy_pass http://nginx;
                        proxy_set_header Host $host;
                        proxy_set_header X-Real-IP $remote_addr;
                        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                        proxy_set_header X-Forwarded-Proto $scheme;
                        client_max_body_size   64M;
                      }
        }
Jangan lupa save file tersebut.

Step 5. restart nginx temen-temen dan setiap ada perubahan pada vhost selalu restart nginx
          [root@nginx ~]# systemctl restart nginx
          [root@nginx ~]# systemctl restart ngin.services
         setelah itu cek domain yang telah dibuat diatas pada blowser atau lakukan
         [root@nginx ~]# curl -I nama_domain.com

" Semoga artiket ini dapat bermanfaat buat temen-temen semua "