nginx.conf - passenger_env_var not loading in rails - ruby-on-rails

I defined environment variables in nginx.conf like below -
server {
listen 80;
server_name XX.XX.XX.XX; //Masked for this question
location ~ ^/clients/abc(/.*|$) {
alias /home/abc/Project/public$1;
passenger_base_uri /clients/abc;
passenger_app_root /home/abc/Project;
passenger_document_root /home/abc/Project/public;
passenger_enabled on;
passenger_env_var AWS_U disha;
}
}
I restarted nginx but when I open rails c and type ENV['AWS_U'], it returns nil.
What possibly could I be doing wrong?

U need to move passenger_env_var directive to server section.
server {
...
passenger_env_var VAR value;
}

Environment variables are only set when the application gets loaded by Passenger. They are not set when the application starts by rails c command.
If you want to know the value of an environment varialbe on the running application loaded by Passenger, you can make a temporal action for debugging:
class DebugController < ApplicationController
def foobar
render plain: "FOOBAR = '#{ENV['FOOBAR']}'"
end
end
Rails.application.routes.draw do
get "/debug/foobar" => "debug#foobar"
end

Related

Nuxt 3 expose .env file to all

I'm building a Nuxt 3 app, but I have a problema with .env file.
In production if I navigate www.mydomain.com/.env i can see al sensible data like API key.
I'trying to use runtimeConfig with apiSecret like this
runtimeConfig: {
// Keys within public, will be also exposed to the client-side
apiSecret: {
//stripe_pk: process.env.STRIPE_PK,
//client_id: process.env.CLIENT_ID,
api_key: example
},
but I encountered the same problem. Where is the mistake?
It is problem of your production server. In server config u should disble access to .env file.
Example for nginx:
server {
...
location ~ /\.(?!well-known).* {
deny all;
access_log off;
log_not_found off;
}
...
}

Serve static html page with nginx based on rails server response

I have 2 html files in /var/files: host1.html and host2.html.
I want nginx serving any of them (as index file) depending on rails response, eg http://localhost - should render host1.html or host2.html.
This is Rails controller:
class HomeController < ApplicationController
def index
response.headers['X-Accel-Redirect'] = 'host1.html'
head :ok
end
end
And nginx config:
upstream app_server {
server 0.0.0.0:3000 fail_timeout=0;
}
server {
listen 8080 default;
location /{
proxy_pass http://app_server;
internal;
root /var/files/;
}
}
This is not working.
If remove internal - request proxied to Rails, but no internal redirect occurs...
Also, i want to serve not only html files - any actually.
Please advice.
Thanks.
I would use something like this
nginx config:
upstream app_server {
server 0.0.0.0:3000 fail_timeout=0;
}
server {
listen 8080 default;
location / {
proxy_pass http://app_server;
}
location /internal/ {
internal;
alias /var/files/;
}
}
Rails controller:
class HomeController < ApplicationController
def index
response.headers['X-Accel-Redirect'] = '/internal/host1.html'
head :ok
end
end
Here we define “virtual” folder /internal/ that will serve static files. When Rails “redirects” to /internal/host1.html nginx will match it to location /internal/, replace /internal/ with /var/files/ (see alias for details) and serve static file /var/files/host1.html. But if user points his browser to /internal/host1.html he will get 404 error due to internal directive.

Nginx shows public directory content instead of executing Rails App

I am trying to execute my Rails Application on Nginx and Passenger but it shows the public directory content instead of executing the application.
server {
server_name 104.236.218.36;
listen 80 default_server;
root /var/www/noise/public;
passenger_enabled on;
rails_spawn_method smart;
rails_env production;
autoindex on;
}
If i remove autoindex on; i am getting the following error.
2015/02/02 06:16:06 [error] 13528#0: *3 directory index of "/var/www/noise/public/" is forbidden, client: 122.178.204.27, server: 104.236.218.36, request: "GET / HTTP/1.1", ho$
The config you show works ok for me, but maybe you have something else set wrong outside the system scope.
You could also specify the Rails app directory try adding:
passenger_app_root /var/www/noise;

NGINX and environment variables from configuration file

I'm trying to set some environment variables to nginx via it's configuration file. I'm using nginx/0.8.53 and it's not working.
server {
listen 80;
server_name localdictus;
root /opt/vdmo_dictus/public; # <--- be sure to point to 'public'!
passenger_enabled on;
rails_env development;
env VDMO_MANDANT = "somevalue";
}
This is the error message:
unknown directive "env" in /opt/nginx/conf/nginx.conf:43
The documentation tells me that there is an "env" command... so what I'm doing wrong ??
http://wiki.nginx.org/CoreModule#env
setting the environment variables via export on the shell is not an option for my application by the way.
Here are the lines:
37: server {
38: listen 80;
39: server_name localdictus;
40: root /opt/vdmo_dictus/public; # <--- be sure to point to 'public'!
41: passenger_enabled on;
42: rails_env development;
43: env VDMO_MANDANT = "somevalue";
44: }
Regards,
Alex
From the documentation you linked to the "Context" for the env directive is main, not server. Put the directive outside of your server { ... } block (outside of any block).
See also this discussion. I do not believe that the env directive does what you are looking for.
Don't pass the env directive. Just use the -E flag when starting nginx:
sudo -E /usr/local/nginx/sbin/nginx
A solution for setting the environment variables for a rails app using nginx.
Your RAILS_ROOT , for example, is : /opt/myapp_MANDANT
Then the following code would extract MANDANT from the RAILS_ROOT path and set in to the rails env.
split = RAILS_ROOT.split("_")
puts split.inspect
if split.size > 1
ENV['VDMO_SYSTEM'] = split[2]
ENV['VDMO_MANDANT'] = split[1]
elsif split.size > 0
ENV['VDMO_SYSTEM'] = nil
ENV['VDMO_MANDANT'] = split[1]
end
put this code in your environment.rb file to work.
A nice way for using this approch is mount with the --bind option.
Example:
mkdir railsapp_mandant
mount -t /originalsource /railsapp_mandant
then set the public path of the rails app to to /originalsource/public/ instead of /railsapp_mandant/public/

How do I configure nginx to have a Rails app at a domain and WordPress at /blog/?

I've got a Rails app deployed via nginx/passenger. It will have multiple domains pointing to it.
I'm wondering if it's possible to configure nginx so that any URL that matches [somedomain.com]/blog/ will be servered by PHP/WordPress located in a different directory.
So, for example:
domain1.com, domain2.com, & domain2.com/some-resource/1 point to the Rails app at /var/rails/mainapp/
but domain1.com/blog/ goes to /var/sites/domain1.com/
and domain2.com/blog/ goes to /var/sites/domain2.com/
server {
location /blog {
alias /var/sites/domain1.com/;
}
location / {
}
}
You need define you /blog before / location
Here is my config. Hope it helps someone.
# Redirect all requests containing 'www.your-website.ru'
# to 'your-website.ru'
server {
listen 80;
server_name www.your-website.ru;
rewrite ^(.*) http://your-website.ru$1 permanent;
}
server {
listen 80;
server_name your-website.ru;
access_log logs/your-website.ru.log;
root /path-to-your-website.ru/current/public;
#####################
# Rails
#####################
location / {
rails_env production; # this is a production server
passenger_enabled on; #

Resources