Find your nginx config

nginx 的設定檔位置如果沒有變動的話, 通常會放在這兩個地方

  • /etc/nginx/nginx.conf
  • /etc/nginx/site-enabled/*.conf

Set allow or deny IP in configuration

server {
  listen 443 ssl http2;
  server_name blog.jasper.com;
  # 允許特定 ip
  allow x.x.x.x;

  # 也可以透過網路遮罩的方式來設定 ip
  allow x.x.x.x/24;

  # 拒絕特定 ip
  deny x.x.x.x;

  # 拒絕所有連線
  deny all;
  ...
}

假設如果你有很多 IP 要設定的話, 可以試試這個方法

# 新增一個 conf file
vim /etc/nginx/conf/ip.conf

# 放在你要處理的 ip rule
allow x.x.x.x;
deny x.x.x.x;

# 開啟剛剛的設定檔然後 include 新建的這份 conf file
vim /etc/nginx/nginx.conf

# 把這段指令放進去 nginx 的設定檔裡面
include conf/ip.conf;

nginx base command

# 重新加載 (recommend)
nginx -s reload

# 驗證 nginx config 是否正確
nginx -t

注意事項

  • nginx 的設定檔在撰寫時要注意 ; 一定要加, 否則 nginx 會無法正常啟動