我使用 printf 来设置我的 Nginx 设置,它工作正常,但读取和更改很尴尬;有没有更好的方法来提高可读性?
config.vm.provision "shell", inline: <<-SHELL
sudo rm /etc/nginx/sites-enabled/default
sudo printf '%s\n' 'server {' 'root /home/vagrant/web;' 'index index.php index.html index.htm;' 'location / {' 'try_files $uri $uri/ /index.php?$args ;' '}' 'location ~ \.php$ {' 'fastcgi_split_path_info ^(.+\.php)(/.+)$;' 'fastcgi_pass unix:/var/run/php5-fpm.sock;' 'fastcgi_index index.php;' 'include fastcgi_params;' '}' '}' >> /etc/nginx/sites-enabled/default
sudo service nginx start
SHELL
最佳答案
假设您的访客计算机上有 Bash,您可以尝试使用嵌套 bash here documents 。不能保证它可以与 Vagrantfile 一起使用,但无论如何值得一试。
config.vm.provision "shell", inline: <<-SHELL
sudo /bin/bash << 'SCRIPT'
rm /etc/nginx/sites-enabled/default;
cat << 'EOF' >> /etc/nginx/sites-enabled/default
%s\n
server {
root /home/vagrant/web;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php?$args ;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
EOF
service nginx start
SCRIPT
SHELL
请注意,EOF、SCRIPT 和 SHELL 必须放在行的最开头。这些单词前面不能有任何制表符或空格。
关于shell - 在 shell 中打印多行 - 提高配置 Vagrantfile 时的可读性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29904491/