├── .gitignore ├── Gemfile ├── LICENSE.txt ├── README.md ├── Rakefile ├── lib └── mina │ ├── nginx.rb │ ├── nginx │ └── version.rb │ └── templates │ └── nginx.conf.erb └── mina-nginx.gemspec /.gitignore: -------------------------------------------------------------------------------- 1 | *.gem 2 | *.rbc 3 | .bundle 4 | .config 5 | .yardoc 6 | Gemfile.lock 7 | InstalledFiles 8 | _yardoc 9 | coverage 10 | doc/ 11 | lib/bundler/man 12 | pkg 13 | rdoc 14 | spec/reports 15 | test/tmp 16 | test/version_tmp 17 | tmp 18 | TAGS 19 | tags 20 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source 'https://rubygems.org' 2 | 3 | # Specify your gem's dependencies in mina-nginx.gemspec 4 | gemspec 5 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2014 hbin 2 | 3 | MIT License 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining 6 | a copy of this software and associated documentation files (the 7 | "Software"), to deal in the Software without restriction, including 8 | without limitation the rights to use, copy, modify, merge, publish, 9 | distribute, sublicense, and/or sell copies of the Software, and to 10 | permit persons to whom the Software is furnished to do so, subject to 11 | the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 20 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 21 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 22 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Mina Nginx 2 | 3 | [Mina](https://github.com/nadarei/mina) tasks for handle with 4 | [Nginx](http://nginx.com/). 5 | 6 | This gem provides several mina tasks: 7 | 8 | mina nginx:install # Install template config to host repo for easy overrides 9 | mina nginx:setup # Install config file to the server's shared dir + symlink 10 | mina nginx:print # Parse & print the nginx config 11 | 12 | mina nginx:reload # Reload Nginx 13 | mina nginx:restart # Restart Nginx 14 | mina nginx:start # Start Nginx 15 | mina nginx:status # Status Nginx 16 | mina nginx:stop # Stop Nginx 17 | 18 | ## Installation 19 | 20 | Add this line to your application's Gemfile, then `bundle install`: 21 | 22 | gem 'mina-nginx', :require => false 23 | 24 | Once installed, add this to your `config/deploy.rb` file: 25 | 26 | require 'mina/nginx' 27 | 28 | Install the base template to your repo's `lib/mina/templates` directory: 29 | 30 | $ bundle exec mina nginx:install 31 | 32 | Consider variables used by the nginx config, particularly: 33 | 34 | * `application` - application name; defaults to 'application' 35 | * `nginx_socket_path` - path to socket file used in nginx upstream directive 36 | * `server_name` - application's nginx server_name (e.g. example.com); defaults to value for `domain` 37 | * `domain` - fqdn you are deploying to 38 | * `deploy_to` - deployment path 39 | * `current_path` - current revision path 40 | 41 | Edit installed template as required. 42 | 43 | ## Recommended Usage 44 | 45 | 1. Follow install steps above; and 46 | 2. Invoke `nginx:setup` in your main `setup` task 47 | 3. Run `nginx:setup` (or base `setup`) to install config changes 48 | 49 | n.b. if the config template has not been installed locally, `mina-nginx` will 50 | fall back to the default template gracefully. 51 | 52 | ## Contributing 53 | 54 | 1. Fork it ( http://github.com/hbin/mina-nginx/fork ) 55 | 2. Create your feature branch (`git checkout -b my-new-feature`) 56 | 3. Commit your changes (`git commit -am 'Add some feature'`) 57 | 4. Push to the branch (`git push origin my-new-feature`) 58 | 5. Create new Pull Request 59 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | require 'bundler/gem_tasks' 2 | -------------------------------------------------------------------------------- /lib/mina/nginx.rb: -------------------------------------------------------------------------------- 1 | require 'mina/nginx/version' 2 | 3 | namespace :nginx do 4 | application = fetch :application, 'application' 5 | 6 | set :nginx_user, 'www-data' 7 | set :nginx_group, 'www-data' 8 | set :nginx_path, '/etc/nginx' 9 | set :nginx_config, -> { "#{fetch(:shared_path)}/config/nginx.conf" } 10 | set :nginx_config_e, -> { "#{fetch(:nginx_path)}/sites-enabled/#{application}.conf" } 11 | set :nginx_socket_path, -> { "#{fetch(:shared_path)}/tmp/puma.sock" } 12 | 13 | desc 'Install Nginx config to repo' 14 | task :install => :environment do 15 | run :local do 16 | installed_path = path_for_template 17 | 18 | if File.exist? installed_path 19 | error! %(file exists; please rm to continue: #{installed_path}) 20 | else 21 | command %(mkdir -p config/deploy/templates) 22 | command %(cp #{nginx_template} #{installed_path}) 23 | end 24 | end 25 | end 26 | 27 | desc 'Print nginx config in local terminal' 28 | task :print => :environment do 29 | run :local do 30 | command %(echo '#{erb nginx_template}') 31 | end 32 | end 33 | 34 | desc 'Setup Nginx on server' 35 | task :setup => :environment do 36 | nginx_config = fetch :nginx_config 37 | nginx_enabled_config = fetch :nginx_config_e 38 | 39 | comment %(Installing nginx config file to #{nginx_config}) 40 | command %(echo '#{erb nginx_template}' > #{nginx_config}) 41 | 42 | comment %(Symlinking nginx config file to #{nginx_enabled_config}) 43 | command %(sudo ln -nfs #{nginx_config} #{nginx_enabled_config}) 44 | 45 | invoke :'nginx:restart' 46 | end 47 | 48 | %w(stop start restart reload status).each do |action| 49 | desc "#{action.capitalize} Nginx" 50 | task action.to_sym => :environment do 51 | comment %(#{action.capitalize} Nginx) 52 | command "sudo service nginx #{action}" 53 | end 54 | end 55 | 56 | private 57 | 58 | def nginx_template 59 | installed_path = path_for_template 60 | template_path = path_for_template installed: false 61 | 62 | File.exist?(installed_path) ? installed_path : template_path 63 | end 64 | 65 | def path_for_template installed: true 66 | installed ? 67 | File.expand_path('./config/deploy/templates/nginx.conf.erb') : 68 | File.expand_path('../templates/nginx.conf.erb', __FILE__) 69 | end 70 | end 71 | -------------------------------------------------------------------------------- /lib/mina/nginx/version.rb: -------------------------------------------------------------------------------- 1 | module Mina 2 | module Nginx 3 | VERSION = '0.0.3'.freeze 4 | end 5 | end 6 | -------------------------------------------------------------------------------- /lib/mina/templates/nginx.conf.erb: -------------------------------------------------------------------------------- 1 | <% application = fetch :application, 'application' %> 2 | <% socket_path = fetch :nginx_socket_path, '/path/to/web/socket' %> 3 | 4 | upstream <%= application %> { 5 | server unix://<%= socket_path %> fail_timeout=0; 6 | } 7 | 8 | server { 9 | listen 80 default deferred; 10 | server_name <%= fetch :server_name, fetch(:domain) %> 11 | root <%= "#{fetch :deploy_to}/#{fetch :current_path}/public" %>; 12 | 13 | location ^~ /assets/ { 14 | gzip_static on; 15 | expires max; 16 | add_header Cache-Control public; 17 | } 18 | 19 | try_files $uri/index.html $uri @<%= application %>; 20 | location @<%= application %> { 21 | proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 22 | proxy_set_header Host $http_host; 23 | proxy_redirect off; 24 | proxy_pass http://<%= application %>; 25 | } 26 | 27 | error_page 500 502 503 504 /500.html; 28 | client_max_body_size 4G; 29 | keepalive_timeout 10; 30 | } 31 | -------------------------------------------------------------------------------- /mina-nginx.gemspec: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | lib = File.expand_path('../lib', __FILE__) 3 | $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) 4 | require 'mina/nginx/version' 5 | 6 | Gem::Specification.new do |spec| 7 | spec.name = 'mina-nginx' 8 | spec.version = Mina::Nginx::VERSION 9 | spec.authors = ['hbin', 'anulman'] 10 | spec.email = ['huangbin88@foxmail.com'] 11 | spec.summary = %(Mina tasks for handle with Nginx.) 12 | spec.description = %(Configuration and managements Mina tasks for Nginx.) 13 | spec.homepage = 'https://github.com/hbin/mina-nginx.git' 14 | spec.license = 'MIT' 15 | 16 | spec.files = `git ls-files`.split($RS) 17 | spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) } 18 | spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) 19 | spec.require_paths = ['lib'] 20 | 21 | spec.add_dependency 'mina' 22 | 23 | spec.add_development_dependency 'bundler', '~> 1.5' 24 | spec.add_development_dependency 'rake', '~> 0' 25 | end 26 | --------------------------------------------------------------------------------