The way I finally solved it was by adding another role for the same server.
role :app, 'theappuser@myserver'
role :install, 'root@myserver', :no_release => true
And then doing something like this
desc 'Install nginx'
task :install do
on roles(:install) do
require 'erb'
template = ERB.new(File.new('deploy/templates/nginx.conf.erb').read).result(binding)
upload! StringIO.new(template), "/etc/nginx/conf.d/#{fetch(:domain)}.conf"
end
on roles(:app) do
invoke 'nginx:restart'
end
end