Lewati ke isi

setup Nginx untuk membatasi akses halaman web

Pertama, pastikan bahwa Nginx sudah terinstal pada server Anda dengan menjalankan perintah berikut pada terminal:

sudo apt update sudo apt install nginx

Buat file .htpasswd pada direktori /etc/nginx/ dengan menggunakan perintah berikut:

sudo htpasswd -c /etc/nginx/.htpasswd username

Ganti "username" dengan nama pengguna yang ingin Anda gunakan untuk autentikasi. Setelah menjalankan perintah ini, Anda akan diminta untuk memasukkan dan mengkonfirmasi kata sandi untuk pengguna tersebut.

Selanjutnya, buat konfigurasi untuk autentikasi di dalam blok server pada konfigurasi Nginx. Buka berkas konfigurasi server pada direktori /etc/nginx/sites-available/ dengan perintah seperti berikut:

sudo nano /etc/nginx/sites-available/default

Pada blok server, tambahkan konfigurasi auth_basic untuk mengaktifkan autentikasi, seperti berikut:

auth_basic_user_file /etc/nginx/.htpasswd;

Ganti "Restricted Access" dengan pesan yang ingin Anda tampilkan pada pengguna ketika diminta untuk masuk. auth_basic_user_file menunjukkan lokasi file .htpasswd yang baru saja dibuat pada langkah kedua.

Simpan dan keluar dari berkas konfigurasi dengan menekan Ctrl+X, kemudian Y, dan Enter.

Verifikasi konfigurasi Nginx dengan menjalankan perintah berikut:

sudo nginx -t

Jika tidak ada kesalahan yang terdeteksi, jalankan perintah berikut untuk memuat ulang konfigurasi Nginx:

sudo systemctl reload nginx

Sekarang halaman web Anda akan terlindungi oleh autentikasi. Ketika pengguna mengunjungi halaman web, mereka akan diminta untuk memasukkan nama pengguna dan kata sandi yang baru saja Anda buat pada langkah kedua sebelum mereka dapat mengakses halaman web.

example full config nginx

##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# https://www.nginx.com/resources/wiki/start/
# https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/
# https://wiki.debian.org/Nginx/DirectoryStructure
#
# In most cases, administrators will remove this file from sites-enabled/ and
# leave it as reference inside of sites-available where it will continue to be
# updated by the nginx packaging team.
#
# This file will automatically load configuration files provided by other
# applications, such as Drupal or Wordpress. These applications will be made
# available underneath a path with that package name, such as /drupal8.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##

# Default server configuration
#

server {
    listen 80;
    server_name prometheus.agungsurya.my.id;
    return 301 https://prometheus.agungsurya.my.id$request_uri;
}

server {
        listen 443 ssl;
        listen [::]:443 ssl;

        # SSL configuration
        #
        # listen 443 ssl default_server;
        # listen [::]:443 ssl default_server;
        #
        # Note: You should disable gzip for SSL traffic.
        # See: https://bugs.debian.org/773332
        #
        # Read up on ssl_ciphers to ensure a secure configuration.
        # See: https://bugs.debian.org/765782
        #
        # Self signed certs generated by the ssl-cert package
        # Don't use them in a production server!
        #
        # include snippets/snakeoil.conf;

        root /var/www/html/;

        # Add index.php to the list if you are using PHP
        index index.html index.htm index.nginx-debian.html;

        ssl_protocols TLSv1.3;
        ssl_certificate /etc/ssl/private/apache-selfsigned.crt;
        ssl_certificate_key /etc/ssl/private/apache-selfsigned.key;

        server_name prometheus.agungsurya.my.id;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                #try_files $uri $uri/ =404;
                auth_basic "Prometheus server authentication";
                auth_basic_user_file /etc/nginx/.htpasswd;
                proxy_pass http://localhost:9090;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection 'upgrade';
                proxy_set_header Host $host;
                proxy_cache_bypass $http_upgrade;
        }

        # pass PHP scripts to FastCGI server
        #
        #location ~ \.php$ {
        #       include snippets/fastcgi-php.conf;
        #
        #       # With php-fpm (or other unix sockets):
        #       fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
        #       # With php-cgi (or other tcp sockets):
        #       fastcgi_pass 127.0.0.1:9000;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #       deny all;
        #}
}


# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
#server {
#       listen 80;
#       listen [::]:80;
#
#       server_name example.com;
#
#       root /var/www/example.com;
#       index index.html;
#
#       location / {
#               try_files $uri $uri/ =404;
#       }
#}