The access to specific HTTP requests can be restricted in many ways. I think "the best" way is the way that fits your needs/preferences best. I will list only the options I find the most useful.
Web-based Authentication System
With a custom authentication system you can restrict access only for authorized users with sufficient privileges (in terms of this system). For example, requests to /vhost.php
could be processed only for the users belonging to "Web Server Admins" group.
Firewall
You can maintain a white list of source IP addresses that are allowed to send requests to the Web server's host, e.g.:
iptables -A INPUT -p tcp --dport 80 -s AllowedIP -j ACCEPT
iptables -A INPUT -p tcp --dport 80 -j DROP
See answers to this question, for instance.
HTTPS
I would recommend configuring HTTPS in order to protect the traffic against interception. Note, the firewall should be adjusted accordingly. For example, you can drop all traffic on port 80, and allow requests on port 443 for specific IP addresses.
Filesystem Permissions
I think it is sufficient to allow the Web server's user read/write permissions for /etc/nginx/sites-available
.
Alternatively, you can
1) open permissions to the directory only for for root
, 2) create a script executable only for root
, and 3) allow execution of the script via "passwordless" sudo
only for the Web server's user/group via /etc/sudoers
, e.g.:
Cmnd_Alias WWW_HOST_CONFIG = /path/to/script
%www-data ALL=(ALL) NOPASSWD: WWW_HOST_CONFIG
Note, if you are using a proxy (such as PHP-FPM, or Apache2), you need to give the appropriate permissions for the user of the proxy process. By the way, PHP-FPM allows to set the process user/group via configuration file, e.g.:
[my_project]
listen = /tmp/php-fpm-my_project.sock
listen.mode = 0660
listen.owner = username
listen.group = www
user = username
group = www