Contoh Kasus

  1. Client mau menampilkan 1 file saja dengan domain yang mereka miliki untuk kebutuhan tertentu.

    Contoh, client punya domain client.com dan hanya ingin menampilkan file robots.txt saja http://client.com/robots.txt

    Semua akses ke selain client.com/robots.txt akan di redirect ke http://client.com/robots.txt

  2. Client mau menampilkan 1 file saja dengan domain yang mereka miliki untuk kebutuhan tertentu.

    Bedanya, kali ini mereka ingin ekstensi file nya disembunyikan.

    Contoh, client punya domain client.com dan hanya ingin menampilkan file config.json, tapi kali ini di redirect tanpa ekstensi (.json) jadi http://client.com/config

    Semua akses ke selain http://client.com/config akan di redirect ke http://client.com/config


Solusi Kasus Pertama

  1. Siapkan 1 file robots.txt
User-agent: *
  1. Buat satu file virtual host nginx
server {
    listen       88;
    server_name  localhost;
    #server_name client.com

    location /robots.txt {
        #change root folder to your app folder
        root   /home/ipang/embersharing/pribados/blog-tutorials/onefile;
        index  robots.txt;
        #always redirect to robots.txt even if
        #user try to access subfolder
        rewrite robots.txt/ /$1 permanent;
    }
    location / {
       return http://localhost:88/robots.txt;
       #return https://client.com/robots.txt
    }

}
Bakal ke Robots.txt Terus

Bakal ke Robots.txt Terus

Solusi Kasus Kedua

  1. Siapkan 1 file, config.json
{
    "nama": "ipang",
    "website": "ipang.my.id"
}
  1. Buat satu file virtual host nginx
server {
    listen       89;
    server_name  localhost;

    location /nama {
        root   /home/ipang/embersharing/pribados/blog-tutorials/onefile;
        index  config.json;
        #redirect all to config.json
        rewrite ^/(.*)\.json /$1 permanent;
        try_files $uri/config.json $uri.json $uri/ $uri /config.json;
    }
    location / {
       return http://localhost:89/nama;
    }

}
Ketik Apapun Balik Lagi ke /nama

Ketik Apapun Balik Lagi ke /nama


File Docker Serve One File Only

  1. Pastikan ada file berikut di dalam satu folder

    Pastikan Semua File Ada di 1 Folder (nama.json harusnya config.json)

    Pastikan Semua File Ada di 1 Folder (nama.json harusnya config.json)

  2. Untuk default.conf nya bisa di copy dari contoh di atas. Di sini saya copy yang kasus pertama

server {
    listen       88;
    server_name  localhost;
    #server_name client.com

    location /robots.txt {
        #change root folder to your app folder
        root   /home/ipang/embersharing/pribados/blog-tutorials/onefile;
        index  robots.txt;
        #always redirect to robots.txt even if
        #user try to access subfolder
        rewrite robots.txt/ /$1 permanent;
    }
    location / {
       return http://localhost:88/robots.txt;
       #return https://client.com/robots.txt
    }

}
  1. Buat file docker-compose.yml
version: "3.3"
services:
  web:
    image: nginx
    container_name: nginx-ctr
    ports:
      - 88:88
    volumes:
      - ./default.conf:/etc/nginx/conf.d/default.conf
      - ./config.json:/usr/share/nginx/html/config.json

Jika kamu merasa tulisan ini bermanfaat & membantu kamu, kamu bisa berdonasi lewat saweria

If you feel this website help you, you can donate at saweria