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/
Related
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;
}
...
}
I'm trying to deploy a second app to Digital Ocean.
I successfully deployed the first app with this tutorial: https://www.phusionpassenger.com/library/walkthroughs/deploy/ruby/digital_ocean/integration_mode.html
I added a second app to the same place following the same tutorial. When I try to visit the second app, I get the message "404 Not Found" and the log says:
2021/08/09 11:39:43 [error] 43452#43452: *21 "/var/www/philosophische_insel/public/index.html" is not found (2: No such file or directory)
There is a troubleshooting-guide for this exact problem: https://www.phusionpassenger.com/docs/advanced_guides/troubleshooting/nginx/troubleshooting/node/
Here is what I tried so far:
To "Cause and solution #1"
I added "passenger_enabled on;":
#cat /etc/nginx/sites-enabled/philosophische_insel.conf
server {
listen 80;
server_name philosophische-insel.ch www.philosophische-insel.ch;
# Tell Nginx and Passenger where your app's 'public' directory is
root /var/www/philosopische_insel/public;
# Turn on Passenger
passenger_enabled on;
passenger_ruby /home/sandro/.rvm/gems/ruby-3.0.0/wrappers/ruby;
}
To "Cause and solution #2"
passenger_root is set to: /usr/lib/ruby/vendor_ruby/phusion_passenger/locations.ini;
cat /etc/nginx/conf.d/mod-http-passenger.conf
### Begin automatically installed Phusion Passenger config snippet ###
passenger_root /usr/lib/ruby/vendor_ruby/phusion_passenger/locations.ini;
passenger_ruby /usr/bin/passenger_free_ruby;
### End automatically installed Phusion Passenger config snippet ###
It is the same as the result of passenger-config --root
/usr/lib/ruby/vendor_ruby/phusion_passenger/locations.ini
To "Cause and solution #3"
I tried to find some errors but I was not successful.
When I reload nginx and check error.log, I get this:
[ N 2021-08-09 12:10:57.2432 44738/T1 age/Wat/WatchdogMain.cpp:1373 ]: Starting Passenger watchdog...
[ N 2021-08-09 12:10:57.2904 44741/T1 age/Cor/CoreMain.cpp:1340 ]: Starting Passenger core...
[ N 2021-08-09 12:10:57.2905 44741/T1 age/Cor/CoreMain.cpp:256 ]: Passenger core running in multi-application mode.
[ N 2021-08-09 12:10:57.3033 44741/T1 age/Cor/CoreMain.cpp:1015 ]: Passenger core online, PID 44741
[ N 2021-08-09 12:10:59.4811 44741/T5 age/Cor/SecurityUpdateChecker.h:519 ]: Security update check: no update found (next check in 24 hours)
2021/08/09 12:11:03 [error] 44756#44756: *1 "/var/www/philosopische_insel/public/index.html" is not found (2: No such file or directory), client: 87.245.104.21, server: philosophische-insel.ch, request: "GET / HTTP/1.1", host: "www.philosophische-insel.ch"
passenger-status only shows the first app
I don't know if it is important but passenger-status only shows the first app, not the second in Application groups:
----------- General information -----------
Max pool size : 6
App groups : 1
Processes : 1
Requests in top-level queue : 0
----------- Application groups -----------
/var/www/dialectica (production):
App root: /var/www/dialectica
Requests in queue: 0
* PID: 45528 Sessions: 0 Processed: 1 Uptime: 1m 57s
CPU: 0% Memory : 34M Last used: 1m 57s ago
Further Information
The first app works. However it has a different ruby version. Here is a comparison:
Ruby version:
First app:
ruby 2.6.3p62 (2019-04-16 revision 67580) [x86_64-linux]
Second app:
ruby 3.0.0p0 (2020-12-25 revision 95aff21468) [x86_64-linux]
Nginx-configuration
First app:
server {
listen 80;
server_name 159.65.120.231;
# Tell Nginx and Passenger where your app's 'public' directory is
root /var/www/dialectica/public;
# Turn on Passenger
passenger_enabled on;
passenger_ruby /home/sandro/.rvm/gems/ruby-2.6.3/wrappers/ruby;
}
Second app:
server {
listen 80;
server_name philosophische-insel.ch www.philosophische-insel.ch;
# Tell Nginx and Passenger where your app's 'public' directory is
root /var/www/philosopische_insel/public;
# Turn on Passenger
passenger_enabled on;
passenger_ruby /home/sandro/.rvm/gems/ruby-3.0.0/wrappers/ruby;
}
Any ideas?
At this point, I don't know how to proceed. Any ideas?
Assuming both apps are working fine, I have three recommendations:
Keep your root statements and add passenger_app_root with your real apps root path instead their public path
In your first app config, you are saying something like: " redirect every request looking for 159.65.120.231:80 to dialecta path", but the problem is that your DNS also resolves philosophische-insel.ch to 159.65.120.231:80. So you will never be able to reach your second app. Try using a different port or different domain (or subdomains) in each of your app's config
Remember to always check your Nginx config with sudo nginx -t and, if
config's fine, restart Nginx with sudo service nginx reload
So the following could be one config for your server:
server {
## Any of the followings should work
## Option 1: use a subdomain for this, remember that your DNS must be
## redirecting subdomains to this IP
listen 80;
server_name dialecta.philosophische-insel.ch www.dialecta.philosophische-insel.ch;
## Option 2: use a different domain. Also needs DNS config
# listen 80;
# server_name dialecta.ch www.dialecta.ch;
## Option 3: use a different port ##
# listen 81;
# server_name 159.65.120.231;
passenger_enabled on;
passenger_app_root /var/www/dialectica;
passenger_ruby /home/sandro/.rvm/gems/ruby-2.6.3/wrappers/ruby;
root /var/www/dialectica/public;
}
server {
listen 80;
server_name philosophische-insel.ch www.philosophische-insel.ch;
passenger_enabled on;
passenger_app_root /var/www/philosopische_insel;
passenger_ruby /home/sandro/.rvm/gems/ruby-3.0.0/wrappers/ruby;
root /var/www/philosopische_insel/public;
}
If you keep getting error, please post the output of sudo nginx -t.
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
I am trying to deploy ActionCable and Rails 5 To Production server (DigitalOcean). I have followed all steps mention in the Gorails video here: https://gorails.com/episodes/deploy-actioncable-and-rails-5
The app was deployed using Phusion Passenger + Nginx + Capistrano.
And when I tried to checked my site on production, I got this error on the browser's console:
WebSocket connection to 'ws://139.59.175.34/cable' failed: Error during WebSocket handshake: Unexpected response code: 404
These are my settings:
/etc/nginx/sites-enabled/default
server {
listen 80;
listen [::]:80 ipv6only=on;
server_name my_server_domain;
passenger_enabled on;
rails_env production;
root /home/deploy/my_app_domain/current/public;
# ActionCabel config (disable this if u r not using it)
location /cable {
passenger_app_group_name actioncable_websocket;
passenger_force_max_concurrent_requests_per_process 0;
}
# redirect server error pages to the static page /50x.html
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
production.rb
config.action_cable.url = "/cable"
config.action_cable.allowed_request_origins = ["http://139.59.175.34"]
cable.js
(function() {
this.App || (this.App = {});
App.cable = ActionCable.createConsumer("/cable");
}).call(this);
I have tried to change the ActionCable config on config/production.rb to:
config.action_cable.url = [/ws:\/\/*/, /wss:\/\/*/]
config.action_cable.allowed_request_origins = [/http:\/\/*/, /https:\/\/*/]
but still no luck.
I have also looked into the production.log on the server and this is the error that was recorded:
WebSocket error occurred: One or more reserved bits are on: reserved1 = 1, reserved2 = 0, reserved3 = 0
UPDATE:
Below is firewall setting on the server:
Finally I found the answer to my problem. It turns out that I do not have redis installed on my production server. So I just need to install redis and now everything works great!
To check whether you have redis installed on the server or not, use this command:
redis-cli
If the redis console appear, then you're all set. If an error/warning comes out, you can follow these steps to install and run redis:
sudo apt install redis-server
redis-server
Hope it helps!
Your configuration looks correct.
This issue can happens if you have a firewall running.
Check with your iptables configuration (linux) if something is blocking it (I don't have a specific line of code to give to you though).
If you're on Windows, check that there are no "antivirus" or "firewall" intercepting / blocking the bits. I heard that Node32 is a very good candidate to block websockets.
Update: Can you also try to connect using wss://139.59.175.34/cable instead of ws://139.59.175.34/cable ? (Be sure your NGinx is proxying with SSL). https://docs.nginx.com/nginx/admin-guide/security-controls/securing-http-traffic-upstream/
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; #