Syno NAS: Cấu hình Nginx

Trường hợp thiết lập WordPress qua WebStation (Nginx) thì không thể dùng các kiểu permalink khác nhau ngoài kiểu id=? Lý do là Nginx không dùng các file .htaccess như Apache2 nên không thể chuyển hướng dòng lệnh theo kiểu permalink.

Cách giải quyết là include vào file cấu hình WordPress file permalinks sau

# /etc/nginx/permalinks
cat <<'EOF' > /etc/nginx/permalinks
location / {
  if (!-e $request_filename) {
    rewrite ^.*$ /index.php last;
  }
  try_files  $uri $uri/ /index.php?$args =404;
}
EOF

Tuy nhiên với DSM, mỗi khi nginx khởi động lại thì tất cả các file cấu hình nginx đều được tạo mới nên những thay đổi của user đều bị xóa. Ngoài ra service nginx không gọi trực tiếp app nginx mà thông qua app mà Synology không cung cấp tài liệu là synow3tool. App này trước khi khởi động lại nginx thì tạo lại tất cả các file cấu hình theo ghi nhớ của DSM và WebStation. Điều này có tiện lợi là khi lỗi cấu hình web thì chỉ cần khởi động lại nginx hay NAS là mọi thứ phục hồi như cũ, ngoài bất tiện khi người dùng cần thay đổi thủ công cấu hình website như đã nói ở trên.

Khi cần thay đổi thủ công cấu hình website, chúng ta cần phải chờ synow3tool khởi động xong nginx (quá trình này có thể mất 3-5 phút), rồi sửa file cấu hình, cuối cùng reload lại nginx.

Script include permalinks vào file cấu hình wordpress

# /root/permalinks.sh
cat <<'EOF' > /root/permalinks.sh
#!/bin/bash
KEYWORD='domain.wordpress.me'
ADDTEXT='    include permalinks;'
CONFDIR='/etc/nginx/sites-enabled/webservice_portal_*'
while true; do
  for f in $CONFDIR; do
    if [ -e "$f" ] && grep -q "$KEYWORD" "$f"; then
      grep -q "$ADDTEXT" "$f" || sed -i --follow-symlinks 's#^\}$#'"$ADDTEXT\n}#" "$f"
      break 2
    fi
  done
  sleep 5
done
systemctl reload nginx
EOF

Script trên chờ đến khi tìm thấy file cấu hình tương ứng có chứa KEYWORD để thêm dòng include permalinks, tuy nhiên nếu synow3tool chưa khởi động xong nginx thì file cấu hình sẽ lại bị thay mới.

Chúng ta thử bằng cách sleep 300 trước khi gọi permalinks.sh, nếu thành công thì giảm dần.

Gọi script cập nhật cấu hình nginx qua Task Scheduler

 Control Panel > Task Scheduler > Create > Triggered Task > User-defined script

Gọi script cập nhật cấu hình nginx qua systemd service

Chúng ta cần tạo permalinks.service và permalinks.timer

# permalinks.service
cat <<EOF > /usr/lib/systemd/system/permalinks.service
[Unit]
Description=Add permalinks to wordpress

[Service]
ExecStart=-/root/permalinks.sh
Type=oneshot

[Install]
WantedBy=multi-user.target
EOF
# permalinks.timer
cat <<EOF > /etc/systemd/system/permalinks.timer
[Unit]
Description=Timer for the permalinks service

[Timer]
OnBootSec=4min

[Install]
WantedBy=timers.target
EOF

Chúng ta chỉ cần enable permalinks.timer để timer khởi chạy khi khởi động, sau 4 phút nó sẽ gọi chạy service cùng tên.

systemctl daemon-reload
systemctl enable permalinks.timer

Chú thích

  • Có thể dùng permalinks.service với Type=oneshotAfter=nginx.service như sau
# permalinks.service
cat <<EOF > /usr/lib/systemd/system/permalinks.service
[Unit]
Description=Add permalinks to wordpress
After=nginx.service

[Service]
ExecStart=-/root/permalinks.sh
Type=oneshot

[Install]
WantedBy=multi-user.target
EOF

Khi đó không cần timer, permalinks.service sẽ chờ nginx.service kết thúc mới exec. Tuy nhiên hậu quả là NAS khởi động khá lâu, khoảng 6 phút mới beep. sau đó vẫn tiếp tục nạp các service!

  • Có người góp ý viết lại nginx.service thay synow3tool bằng nginx để đỡ rắc rối. Tuy nhiên vì DSM điều khiển mọi dịch vụ qua Web UI nên nó chăm sóc nginx kỹ như vậy cũng hợp lý!

Comments Off on Syno NAS: Cấu hình Nginx

Filed under Software

Comments are closed.