How to make Sinatra listen on two ports with socat - ruby

I had a classical Sinatra application which was accessible on two ports. After migrating it to modular style the second port is not working anymore.
My initial implementation was:
require 'sinatra'
set :port, 8080
set :bind, '0.0.0.0'
----some routes-----
...
The resulting implementation was:
require 'sinatra/base'
require_rel 'lib'
class MyApp < Sinatra::Base
register Sinatra::SomeRegister
helpers Sinatra::SomeHelper
set :port, 8080
set :bind, '0.0.0.0'
----some routes-----
...
run!
end
The application is run using:
socat tcp-l:8181,fork,reuseaddr tcp:localhost:8080 &
ruby /path/my_app.rb
The application doesn't respond on port 8181 anymore.

The fix was to install socat first:
apt-get update && apt-get --allow-unauthenticated -y install socat

Related

Can't access sinatra service outside of container

I am getting ERR_EMPTY_RESPONSE in the browser after lunching the Sinatra container in docker. If I connect to the docker container and run curl localhost:4567 it works, but outside of the container - doesn't
web.rb
require 'sinatra'
set :bind, '0.0.0.0'
get '/' do
'Hello world!'
end
Dockerfile
FROM ruby:3.0.2
EXPOSE 4567
COPY ./web.rb .
RUN gem install sinatra
RUN gem install puma
CMD ["ruby", "web.rb"]
The solution was in binding application to 0.0.0.0 host
require 'sinatra'
set :bind, '0.0.0.0'
get '/' do
'Hello world!'
end

Expose port on all interfaces using Webrick

I am new to Ruby.
I am working with a script that deploys a HTTPServer using WEBrick.
When the script runs, it logs out :
check_1 | [2018-12-20 17:51:47] INFO WEBrick::HTTPServer#start: pid=1 port=4567
When I do a netstat on the machine, I notice that the service is listening only on 127.0.0.1
$ netstat -tulpn
root#c4a20aa7bd8c:/opt/service# netstat -tulpn
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.1:4567 0.0.0.0:* LISTEN 1/ruby1.9.3
I want to listen on all the IPs so that I can communicate with this service from another container.
How or what configs will be necessary ?
My script definition for the server is :
#!/usr/bin/ruby
require 'rubygems'
require 'sinatra'
require 'sinatra/reloader' if development?
also_reload 'config.yml'
require "sinatra/config_file"
require 'mechanize'
require "json"
require 'open-uri'
require 'mongo'
require 'uri'
require 'addressable/uri'
require 'cgi'
include Mongo
require 'nokogiri'
set :server, 'webrick'
The answer you're looking for is in the Sinatra config docs: http://sinatrarb.com/configuration.html
Specifically the :bind option. Example: set :bind, '0.0.0.0'

How to run Sinatra App in Passenger with Nginx?

I'm trying to run a very simple Sinatra app using my existing Nginx & Passenger setup. I'm familiar with running Rails on Passenger but this is my first time setting up Sinatra.
I've installed Sinatra through bundler and RVM. Take a look at my configuration and tell me what I'm doing wrong.
Nginx conf:
server {
listen 80;
server_name demo.my-example.com;
root /home/user/demo.my-example.com/sinatra;
passenger_ruby /usr/local/rvm/wrappers/ruby-2.3.1#my_gemset/ruby;
passenger_enabled on;
}
/home/user/demo.my-example.com/sinatra/config.ru
require 'rubygems'
Gem.clear_paths
disable :run, :reload
set :environment, :production
require "./stripe"
run StripeApp
/home/user/demo.my-example.com/sinatra/stripe.rb
require 'sinatra/base'
class StripeApp < Sinatra::Base
get '/' do
"Hello world"
end
end
/home/user/demo.my-example.com/sinatra/config.ru
require 'rubygems'
Gem.clear_paths
require "./stripe"
run StripeApp

How to make passenger and rvm work together?

I've had rvm installed beforehand. And I decided to install passenger from a package (nginx-full and passenger) and would like to use the ruby installed with rvm. But somehow it doesn't work. Here's the test sinatra app I'm using (~yuri/a1/app.rb):
require 'rubygems'
require 'sinatra'
get '/' do
"Hello and Goodbye"
end
~yuri/a1/config.ru:
require 'rubygems'
require 'sinatra'
require './app.rb'
run Sinatra::Application
nginx.conf:
http {
...
passenger_root /usr/lib/ruby/vendor_ruby/phusion_passenger/locations.ini;
# the paths in the above file point out to debian repository's ruby version
server {
server_name a1;
root /home/yuri/a1;
access_log /var/log/nginx/a1-access.log;
error_log /var/log/nginx/a1-error.log;
passenger_enabled on;
passenger_ruby /home/yuri/.rvm/wrappers/ruby-1.9.3-p385#a1/ruby;
}
}
But when I do w3m http://a1 access.log says:
127.0.0.1 - - [12/Sep/2013:21:14:58 +0300] "GET / HTTP/1.0" 403 168 "-" "w3m/0.5.2+cvs-1.1027"
and error.log:
2013/09/12 21:14:58 [error] 27622#0: *1 directory index of "/home/yuri/tr/" is forbidden, client: 127.0.0.1, server: tr, request: "GET / HTTP/1.0", host: "a1"
The app works if I run it as follows: rvm ruby-1.9.3-p385#a1 && ruby app.rb.
Is there a way to track down what's happening there? Or how to make it work?
passenger expects application directories to have a certain layout. Particularly, config.ru must reside up one level relative to the document root. That is:
/etc/nignx/nginx.conf:
...
http {
...
passenger_root /usr/lib/ruby/vendor_ruby/phusion_passenger/locations.ini;
server {
server_name a1;
root /home/yuri/a1/public;
access_log /var/log/nginx/a1-access.log;
error_log /var/log/nginx/a1-error.log;
passenger_ruby /home/yuri/.rvm/wrappers/ruby-1.9.3-p385#a1/ruby;
passenger_enabled on;
}
}
~yuri/a1/app.rb:
require 'sinatra'
get '/' do
"Hello World!"
end
~yuri/a1/config.ru:
require './app'
run Sinatra::Application
And:
$ rvm install 1.9.3-p385
...
$ rvm --create use 1.9.3-p385#a1
...
$ curl http://a1
Hello World!
I just went through setting up passenger 4.0.17 on Ubuntu Precise using the apt approach.
Here is what worked for me.
In the /etc/nginx/nginx.conf
passenger_root /usr/lib/ruby/vendor_ruby/phusion_passenger/locations.ini;
passenger_ruby /usr/local/rvm/wrappers/ruby-2.0.0-p247/ruby;
Try moving the passenger_ruby to outside of the server block.
Good Luck

Sinatra on Nginx configuration - what's wrong?

I followed this tutorial more or less... I installed the passenger gem, executed passenger-install-ginx-module, sucessfully installed nginx and inserted this into the config:
server {
listen 80;
server_name localhost;
root /home/admin/sintest/public; # <--- be sure to point to 'public'!
passenger_enabled on;
}
In /home/admin/sintest I have: an empty public folder,
the config.ru:
require 'sinatra'
set :env, :production
disable :run
require './app.rb' #the app itself
run Sinatra::Application
and a test sinatra app.rb:
require 'sinatra'
get '/' do
"hello world!"
end
Now when I run nginx and open up http://localhost what I get is: 403 Forbidden
What am I doing wrong? Have I missed something?
Make sure that the user nginx is running as (in most cases 'nobody' or 'www-data') has permission to read the contents of your home directory /home/admin.
Also you can look into the nginx logs and read exactly what the error was.
I had the same error until I added passenger_root and passenger_ruby directives in the http block.

Resources