Rails 3.2 xsendfile not working - ruby

I have to download static files which need authentication.
I am following http://makandracards.com/makandra/990-speed-up-file-downloads-with-rails-apache-and-x-sendfile and https://tn123.org/mod_xsendfile/
I have set config.action_dispatch.x_sendfile_header = "X-Sendfile" # for apache in production.rb
Controller:
send_file("#{Rails.root}/private/myfile.mp3",
:filename => "myfile.mp3",
type: "audio/mpeg",
:disposition => 'inline')
Apache configuration:
<VirtualHost _default_:80>
ServerName myserver.com
RequestHeader Set X-Sendfile-Type X-Sendfile
XSendFile On
XSendFilePath /sites/releases
RailsEnv production
</VirtualHost>
Even though, I put this, it is not sending any x-sendfile header.
It is strange but if I remove X-Sendfile On and X-SendfilePath, then it sets X-Sendfile header but the file does not play in browser.
I am trying this for three days, I have read everything available on google.
I have a feeling I'm missing something simple. If anyone has any suggestions, please let me
know!
Thanks.

Related

Does my Sinatra API application need a class declaration?

Someone developed an API application for me that works well. Unfortunately, it doesn't log anywhere and there are no logs at all. The app runs with the "rackup" command and sits behind an nginx web server. The Sinatra errors are not logged to the nginx logs.
The app.rb file looks something like this:
require './libs'
require 'sinatra'
require 'sinatra/namespace'
set :bind, '::1'
before do
content_type :json
headers 'Access-Control-Allow-Origin' => '*', 'Access-Control-Allow-Methods' => ['OPTIONS', 'GET', 'POST']
end
namespace '/api/v1' do
namespace '/getit/:thingtoget' do
helpers do
def myhelper1
<stuff>
end
def myhelper1
<stuff>
end
end
before do
myhelper1
myhelper2
end
get '/info' do
WidgetDomain::get_info(#va1, #var2).to_json
end
<more API paths here>
end
Now this is working fine. But now I want to introduce logging. So I looked at the Sinatra README and it said I could enable logging like so:
class MyApp < Sinatra::Base
configure :production, :development do
enable :logging
end
end
Do I put that above the namespace stuff? If I declare an app like that, does my namespace stuff need to be inside that app code somehow? I don't have a grasp of how this works at all.
It almost looks like it's trying to log in the nginx log, but here's what an error line looks like there:
2018/12/30 19:53:15 [error] 6615#0: *21522 connect() failed (111: Connection refused) while connecting to upstream, client: <someip>, server: api.example.com, request: "GET /api/v1/getit/thingtoget1/stuff/var1/var2/var3 HTTP/1.1", upstream: "http://[::1]:9292/api/v1/getit/thingtoget/stuff/var1/var2/var3", host: "api.example.com", referrer: "an HTML page from the nginx server"
It's almost like it's trying to connect back to the server to retrieve a message or something. But in case the logging has something to do with the way I've declared the server in the nginx configuration, here it is:
server {
listen 443;
listen [::]:443;
server_name api.myapp.com;
ssl on;
ssl_certificate /etc/nginx/ssl/myapp_com.pem;
ssl_certificate_key /etc/nginx/ssl/star_myapp_com.key;
location / {
proxy_pass http://localhost:9292;
}
}
Have a look at Sinatra's README in the Logging section but I tend to set up a logger outside of the Sinatra app in case other bits get bolted on or several apps are used together and I can use the same logger for all of them. Simplest way is a global variable (one of the few places they're acceptable, but it's not the only way) :
require 'mono_logger' # because it's thread safe
require 'pathname' # because paths aren't strings :)
log_path = Pathname(__dir__).join("logs/app.log")
$logger = MonoLogger.new(log_path)
$logger.level = MonoLogger::INFO
Then in a route or wherever:
get '/' do
$logger.info "here"
In my terminal:
$ cat logs/app.log
I, [2019-01-07T13:03:52.989415 #64378] INFO -- : here
As to configuration blocks, you don't need to worry about putting them within a class declaration unless you're using a modular app (see Modular vs. Classic Style in the README) and yours is in the classic style.
Configuration blocks aren't namespaced (Sinatra::Namespace handles things that take a route as an argument, like get and before) so follow convention and stick it near the top of the file.
Hope that helps.

Apache - changing document root (Mac)

I'm just getting started using Apache on my Mac (El Capitan 10.11.13), but I'd like to change the document root. However, I'm not sure I fully understand how to do this, or if it is even possible to set it up the way that I would like to...
For starters, here's the snippet from my httpd.conf file (I haven't made any changes, this is stock)...
DocumentRoot "/Library/WebServer/Documents"
<Directory "/Library/WebServer/Documents">
#
# Possible values for the Options directive are "None", "All",
# or any combination of:
# Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
#
# Note that "MultiViews" must be named *explicitly* --- "Options All"
# doesn't give it to you.
#
# The Options directive is both complicated and important. Please see
# http://httpd.apache.org/docs/2.4/mod/core.html#options
# for more information.
#
Options FollowSymLinks Multiviews
MultiviewsMatch Any
#
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
# AllowOverride FileInfo AuthConfig Limit
#
AllowOverride None
#
# Controls who can get stuff from this server.
#
Require all granted
</Directory>
I would like to change the DocumentRoot to the folder where I generally keep my web development work, which is:
/Users/myname/Desktop/Stuff/Development
I know that the first step is to replace the DocumentRoot and Directory in the .conf file, but this doesn't work - I get the 403 forbidden error - and I'm not sure what to do after that.
My questions...
Is it possible to set the document root to a folder that is associated with a specific user - me, in this case? I'm the only user of my machine, and it would be much easier for me to do what I need to if Apache was pointed at that folder.
If so, what do I need to do other than change the first two lines from the snippet of my .conf file? Please be specific, as I'm relatively new to working with the terminal.
Thanks very much.
Yes you can't just change DocumentRoot without giving the permission to new path. If you must use it you have to add chmod +x to each path, in your case those are
User
myname
...
Development
It is not safe to do so. I would user a folder doesn't involved Users.

modify apache httpd.conf using ruby

Hey guys I'm trying to change in httpd.conf using ruby code 1.8.5 could you help me with it please
Apache configuration file The Apache configuration file must be configured specifically
for the server you have installed it on, i.e. the ServerName configuration item must be set to the IP address of the VM, not to 127.0.0.1 or any other generic address. TheServerAdmin configuration item must be set to cit470-sp2014-teamYOUR_TEAM_NUMBER#gmail.com. Create a gmail account with that name if you
have not done so already.
SSL All content must also be accessible using encrypted https versions of your URLs.
User directories Configure the server so that individual users can create their own web
sites by putting files in the public_html subdirectory of their home directory. These files should be available by both URLs of the form http://your_ip/~username and of the form http://your_ip/users/username. This may require that you create rewrite rules with mod_rewrite.
]# ./apconfigure.rb
./apconfigure.rb:8: warning: parenthesize argument(s) for future version
./apconfigure.rb:18: syntax error
.gsub(/ServerAdmin (.+)/, "ServerAdmin cit470su2015team2#gmail.com")
^
./apconfigure.rb:19: syntax error
.gsub(/UserDir disable/, "UserDir public_html")
^
./apconfigure.rb:20: syntax error
.gsub(userdir_regex, replacement) + "\nServerName 10.2.7.84 \nRewriteEngine on\nRewriteRule ^/users/(.*)$ /~$1 [PT]"
^
this is my code
#!/usr/bin/ruby
# This script edits the Apache configuration files and enables UserDir,
# as well as a custom RewriteRule
# path to apache config file
config_file = "/etc/httpd/conf/httpd.conf"
file = File.read(config _file)
# regex to uncomment the sample UserDir directives
userdir_regex = /#<Directory \/home\/\*\/public_html>.+?<\/Directory>/m
replacement = file.match(userdir_regex).to_s.gsub('#','')
# make some substitutions: change ServerName and ServerAdmin, enable UserDir,
# add rewrite rules.
edited_file =
file
.gsub(/ServerAdmin (.+)/, "ServerAdmin cit470su2015team2#gmail.com")
.gsub(/UserDir disable/, "UserDir public_html")
.gsub(userdir_regex, replacement) + "\nServerName 10.2.7.84 \nRewriteEngine on\nRewriteRule ^/users/(.*)$ /~$1 [PT]"
# write the changes to the file
File.open(config_file, 'w') do |f|
f.puts edited_file
end

Error WSDL: SOAP-ERROR: Parsing WSDL: Couldn't load from

My problem is very common. I read almost all topics on stackoverflow about this issue, but still can not do thing work. My code is :
ini_set("soap.wsdl_cache", "0");
ini_set("soap.wsdl_cache_enabled", "0");
$proxy = new SoapClient('http://Myhostname/api/soap?wsdl');
I had this error :
Error WSDL: SOAP-ERROR: Parsing WSDL: Couldn't load from
'http://Myhostname/api/soap?wsdl' : failed to load external entity
"http://Myhostname/api/soap?wsdl"
In the browser the URL
http://Myhostname/api/soap?wsdl
works great (returns an xml response)
I try this code to see if PHP can reach this URL :
if (file_get_contents('http://Myhostname/api/soap?wsdl') === false) {
echo "ERROR: file_get_contents <br/>";
}
It displays me my error "ERROR: file_get_contents", so it does not work. On the forums I saw that to solve this problem I must add hostname into OS "hosts" file. So I did it. In my hosts file I added this line
127.0.0.1 Myhostname
But it still does not work. Then I changed URL in my code to
http://localhost/Myhostname/api/soap?wsdl
but it does not work either.
I tried another solution I found on the internet. For some people it worked. In my URL I changed '?' character to '.'. So my new URL became
http://localhost/Myhostname/api/soap.wsdl
but it does not work either.
I saw official SOAP doc! And tried to create SoapClient object in different way (with options like 'proxy_host' and 'proxy_port'). But this did not help me.
It's my second day I try to solve this problem, but I can not. I am doing my internship, I do not have a lot of experience, so maybe I did not try solutions I found correctly. Maybe someone can help. I would be very pleased.
Few steps to check
Have a ping to Myhostname. It would give the ip. Use ip instead and see
Check if the services are enabled
S0AP,
php_openssl,
openssl,
curl
Hope you would get it :)
I have the similar problem, I solve it uncommenting -MultiViews from .htaccess file, and later from Apache configuration of local website.
My working Apache configuration is:
<Directory /var/www/>
Options Indexes FollowSymLinks -MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
You can try also url:
http://localhost/Myhostname/api.php?type=soap&wsdl
if you use community edition.
instead of $proxy = new SoapClient('http://Myhostname/api/soap?wsdl');
try $proxy = new SoapClient(WSDLFILENAME);
where WSDLFILENAME is the OS name of the WSDL file.
This has the additional benefit of making one client-server roundtrip less.
I tried the SOAP Web Services on my local host and it was working fine. The reason why the Magento was not able to give a response to the request was because it was not able to verify SSL Certificates enabled on the site.
The way I resolved it was to allow self signed SSL verification:
$opts = [
'http' =>
[
'header' => "Authorization: Bearer ".$token
],
'ssl' => [
'allow_self_signed' => true ,
'verify_peer' => false,
'verify_peer_name' => false]
];
$context = stream_context_create($opts);
I hope it helps. ;)

Apache2 doesn't execute .rb files (mod_ruby)

I want to make apache2 run ruby scripts. I managed to do this by using CGI, but now I want to go one step further and do it with mod_ruby. I installed mod_ruby through apt-get so it's supposed to be enabled, but when I visit http://localhost/test.rb my browser downloads the file instead of displaying it's output.
If I open /etc/apache2/mods-enabled/ruby.load there is a line containing the path do this mod.
I also added the following lined to my apache2.conf:
<IfModule mod_ruby.c>
<Directory /var/www/>
Options +ExecCGI
</Directory>
RubyRequire apache/ruby-run
#RubySafeLevel 0
<Files *.rb>
SetHandler ruby-object
RubyHandler Apache::RubyRun.instance
</Files>
<Files *.rbx>
SetHandler ruby-object
RubyHandler Apache::RubyRun.instance
</Files>
</IfModule>
Can you please help me?
PS. Please don't suggest using ROR. I'm familiar with that. Now I just want to use ruby for educational reasons. Also please keep things simple I'm a total newbie to apache.
See this article - it has some instructions. Basically you need to tell Apache to pass your Ruby files through the handler.
Also, consider trying Sinatra - it's a micro web framework that's as simple as plain ruby files. And it runs with Passenger which is the well-documented Ruby module for Apache.
The article on HowtoForge covers almost the whole story, but I needed to add one line inside the <Directory /var/www> directive to prevent browsers from downloading the file:
AddType text/html .rb .rbx
Here are a couple more sites that I found helpful:
https://defuse.ca/blog/2012/07/how-to-install-mod_ruby-on-debian-squeeze/
http://modruby.net/en/doc/
Debian Stretch 9.
I tried installing mod_ruby with apt-get install libapache2-mod-ruby but there is no package by that name. In the end, all I needed to do was add this line to the apache-config to get the server to run my scripts.
AddHandler cgi-script .rb

Resources