Nginx only serving static files for a Sinatra application - ruby

I'm trying to run ruby (sinatra) script under the Nginx.
My folder structure is:
/www (775)
/www/public (775)
/www/public/img.jpg (644)
/www/app.rb (775)
/www/index.html (775)
My nginx.conf is:
worker_processes 1;
events {
worker_connections 1024;
}
http {
passenger_root /home/my_user/.rvm/gems/ruby-2.4.1/gems/passenger-5.1.6;
passenger_ruby /home/my_user/.rvm/gems/ruby-2.4.1/wrappers/ruby;
include mime.types;
default_type application/octet-stream;
sendfile on;
server {
listen 80;
server_name xxx.xxx.xx.xx;
root /www/public;
passenger_enabled on;
}
}
But I'm getting served just a xxx.xxx.xx.xx/img.jpg, no script.
xxx.xxx.xx.xx/index.html gives me error 404 and xxx.xxx.xx.xx gives me error 403 again.
Is important under which user is nginx server running (folder /www and files inside it have a different user than the user running nginx).
Thanks

So thanks to #tadman the configuration was ok, the problem was just missing config.ru file.

Related

Nginx configuration setup for windows

I am trying to run my vue applications trough nginx on windows and i was using the following tutorials, one to run nginx with AlwaysUp and the other one to configure it.
https://www.coretechnologies.com/products/AlwaysUp/Apps/RunNginxAsAService.html
https://graspingtech.com/nginx-virtual-hosts-ubuntu-16-04/
I also stumbled upon the following stack overflow question which is basically the problem i have but it didnt work:
nginx Windows: setting up sites-available configs
The service is running and recognizes the two domains i am trying to set up but for whatever reason it always sends me back to the NGINX Welcome page and i am not sure what i am doing wrong.
I followed the steps on the second tutorial and did a few changes, such ass adding "server_names_hash_bucket_size 64;" to my nginx.config file. I also created the symlink between the "sites-available" and "sites-enabled directories" using windows mklink.
Here are my files.
Nginx.config
worker_processes 1;
events {
worker_connections 1024;
}
http {
server_names_hash_bucket_size 64;
include mime.types;
default_type application/octet-stream;
include "C:/nginx/nginx/sites-available/*.conf";
sendfile on;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
My app config file in sites-available which also contains a symlink in sites-enabled:
server {
listen 80;
listen [::]:80;
server_name myapp.nginx.br;
root "C:/Users/Documents/git-repository/my-app/dist";
index index.html;
location / {
try_files $uri $uri/ #rewrites;
}
location #rewrites {
rewrite ^(.+)$ /index.html last;
}
location ~* \.(?:ico|css|js|gif|jpe?g|png)$ {
expires max;
add_header Pragma public;
add_header Cache-Control "public, must-revalidate, proxy-revalidate";
}
}
I also faced the issue how to kill the nginx process. I stumbled a while until I came up with the following command which works:
taskkill /F /FI "IMAGENAME eq nginx.exe"
Well, after many hours i actually found out what was happening.
Everything in my files, from the nginx.conf file to every other .conf files were ok.
However i discovered that when restarting or stop/starting my nginx service with either the AlwaysUp program or the windows service options the service would still be active somehow and wouldnt apply my changes, therefore it would always show me the "welcome to nginx" page.
So i just restarted my computer because i wasnt able to kill the service with conventional means and it worked just fine for every single app i have.
I am sure there is a better way to kill the service and restart it but restarting my computer so the changes to my files would actually be applied solved it.
EDIT: I also discovered that windows takes a little while to stop the nginx service, so if you are using always up try stopping the service there, if it fails try stopping the service trough windows services menu. Also don't forget to set it to manual so you wont accidentally access you nginx app instead of your actual deployed app.

Multiple websites with one IP: settings with proxy server and proxy cache (flask, uwsgi, nginx)

I want to allow my nginx server to server multiple subdomains.
Each site is served by a flask + uwsgi, listening to own ports.
All sites have many endpoints with same name, and responses are cached in different zones: I want to serve the correct cache (or correct site) from the proxy server.
I read https://askubuntu.com/questions/766352/multiple-websites-on-nginx-one-ip : in my configuration I keep having domain2 redirected to domain1.
I cannot find proper configuration for listening to uwsgi and have the proxy server serve the correct site.
How to properly set the ports and proxy_cache on the proxy_server to allow nginx serve two flask sites from a single server?
Below is my set current setup:
configuration domain_1
server {
server_name www.domain1.com;
return 301 $scheme://domain1.com$request_uri;
}
server {
listen 8000 default_server;
server_name domain1.com;
root /var/www/example_site_1;
# common locations for all sites
location / {
include uwsgi_params;
uwsgi_pass unix:/var/www/example_site_1/domain1.sock;
}
# API
location /api {
include uwsgi_params;
uwsgi_param UWSGI_SCRIPT wsgi;
uwsgi_pass unix:/var/www/example_site_1/domain1.sock;
}
}
# Set cache directory for site
proxy_cache_path /tmp/nginx/domain1 levels=1:2 keys_zone=my_zone_domain_1:10m max_size=50m inactive=60m;
proxy_cache_key "$scheme$request_method$host$request_uri";
# Virtualhost/server configuration
server {
listen 80 default_server;
server_name domain1;
root /var/www/domain1;
## how to serve proxy_cache if locations of domain_1 and domain_2 are the same ?
location / {
proxy_cache my_zone_domain_1;
add_header X-Proxy-Cache $upstream_cache_status;
include proxy_params;
proxy_pass http://domain1.com:8000;
}
location /api {
add_header X-Proxy-Cache $upstream_cache_status;
proxy_cache my_zone_domain_1;
proxy_pass http://domain1.com:8000/api;
}
}
configuration domain_2
server {
server_name www.domain2.com;
return 301 $scheme://domain2.com$request_uri;
}
server {
listen 3000;
server_name domain2.com;
root /var/www/example_site_2;
# common locations for all sites
location / {
include uwsgi_params;
uwsgi_pass unix:/var/www/example_site_2/domain2.sock;
}
# API
location /api {
include uwsgi_params;
uwsgi_param UWSGI_SCRIPT wsgi;
uwsgi_pass unix:/var/www/example_site_2/domain2.sock;
}
}
# Set cache directory for site
proxy_cache_path /tmp/nginx/domain2 levels=1:2 keys_zone=my_zone_domain_2:10m max_size=50m inactive=60m;
proxy_cache_key "$scheme$request_method$host$request_uri";
# Virtualhost/server configuration
server {
# I tried listening on other ports than 80, but kept having redirects on domain_1
listen 80;
server_name domain2;
root /var/www/domain2;
## how to serve proxy_cache if locations of domain_1 and domain_2 are the same ?
location / {
proxy_cache my_zone_domain_2;
add_header X-Proxy-Cache $upstream_cache_status;
include proxy_params;
proxy_pass http://domain2.com:3000;
}
location /api {
add_header X-Proxy-Cache $upstream_cache_status;
proxy_cache my_zone_domain_2;
proxy_pass http://domain2.com:3000/api;
}
}
Your domain_2 configuration uses proxy_pass http://domain2.com:8000 but there is only domain_1 server listens on the port 8000 so it gets to serve requests directed to domain_2.
I also recommend you to rethink usage of proxy_pass in your configuration, it isn't really necessary.
I found the culprit was Uwsgi : it was loaded from usr/bin folder, and not from virtual environment folder of my app.
Maybe virtualenv folder was corrupted: when I tried to re-install uwsgi (pip install uwsgi), it kept saying requirements satisfied until I noticed that which uwsgi was not loaded from virtualenv.
I had to remove and reinstall virtualenv folder with uwsgi and python modules.
Now applications were running (answer to the question), but proxy_server configuration has to be further adjusted in my case.
The following may be useful for people making use of url_for() directives in flask: url_for() directive routes to an absolute path, which may conflict with the nginx proxy.
Example: nginx proxy server listens on domains2.com on port 80, proxy_pass /path location to http://domain2.com:3000; if flask is redirecting a route to /path (with url_for()), the resulting url will be http://domain2.com:3000/path (because it follows the port specified in absolute path) instead of http://domain2.com/path (the url of the proxy).
Add proxy_set_header Host $http_host; to /path location to allow nginx proxy follow the correct redirect of your flask application.

Linking nginx config with installing

I am trying to install nginx with a shell script but it seems like I have made a mistake:
# INSTALL NGINX
apt-get install nginx
# LINK CONFIGURATION FILE
ln -fs /etc/configuration/nginx/nginx/app.conf /etc/nginx/sites-available/app
ln -fs /etc/configuration/nginx/nginx/nginx.conf /etc/nginx/nginx.conf
# RESTART
service nginx restart
My configuration for my app.conf is as such:
server {
listen 80;
root /home/deploy/project123;
server_name www.project123.com;
location / { try_files $uri #app; }
location #app {
include uwsgi_params;
uwsgi_pass unix:/tmp/uwsgi.sock;
}
}
My nginx.conf is such:
user www-data;
worker_processes 1;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
access_log /var/log/nginx/access.log;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
My code is in: /home/deploy/project123
When I visit the site I get a 404 as if nothing is running. Can anyone tell me what I have done wrong please?
ln -fs /etc/configuration/nginx/nginx/app.conf /etc/nginx/sites-available/app
You need to link it to sites-enabled and not available - and it will probably work for you.
I would also not change the name of the conf-file, for clarity:
ln -fs /etc/configuration/nginx/nginx/app.conf /etc/nginx/sites-enabled/
would make it work
Looks like you are doing some configuration management of your own, have you check out puppet, chef, cfengine, saltstack?

passenger_base_uri not working on passenger 4.0.0rc4

We are running ruby 2.0.0-p0 with passenger 4.0.0rc (nginx/1.2.7) on ubuntu 12.04 server in production (rails 3.2.12). Just notice that the passenger_base_uri in our nginx.conf is not pointing to the base subdir. Instead it return nothing. Here is passenger_base_uri in nginx.conf:
server {
listen 80;
server_name 154.49.55.6;
root /ebs/www/;
passenger_enabled on;
rails_env production;
passenger_base_uri /nbhy;
#for rails >= 3.1, assets pipeline
location ~ ^/assets/ {
expires max;
add_header Cache-control public;
add_header ETag "";
break;
}
}
A symlink nbhy was created on the server under /ebs/www and point to /ebs/www/nbhyop/current/public. The /nbhy is the base uri for rails app.
Here is the nginx error log,
[ 2013-05-06 17:47:00.5469 718/7f5097fc0700 Pool2/Implementation.cpp:1098 ]: [App 838 stdout]
2013/05/06 17:47:26 [error] 735#0: *104 open() "/ebs/www/authentify/session" failed (2: No such file or directory), client: 6.5.5.94, server: 154.49.55.6, request: "POST /authentify/session HTTP/1.1", host: "154.49.55.6", referrer: "http://154.49.55.6/nbhy/signin"
The path above should be: /ebs/www/nbhy/authentify/session instead of /ebs/www/authentify/session.
The same config is working on passenger 3.x. How to fix this problem? Thanks for help
I believe you were using passenger_base_uri for the wrong purpose. By specifying passenger_base_uri, you are saying:
"I have another web app, living under /nbhy"
I see in the Nginx output that you were doing a POST /authentify/session. Since you are not accessing any URL under /nbhy, the passenger_base_uri option has no effect. If you POST /nbhy/authentify/session, then it'll have effect.
Perhaps you want to set the virtual host's root to /ebs/www/nbhyop/current/public?

How do I create multiple locations with Nginx, Passenger, Sinatra

I have a server section that looks like:
server {
listen 80;
server_name arch;
root /data/apps/production/fentonGem2/current/public;
passenger_enabled on;
}
which works fine. However, I'd like to deploy two or more apps to the same server_name and listen port. So presumably I'd use something like the following:
server {
listen 80;
server_name arch;
location /app1 {
root /data/apps/production/fentonGem2/current/public;
passenger_enabled on;
}
location /app2 {
root /data/apps/production/fentonGem3/current/public;
passenger_enabled on
}
}
But that doesn't work. Does anyone know how I can deploy two separate apps, and reach them by:
http://domain.com/app1/
and:
http://domain.com/app2/
The setup uses Nginx, Phusion Passenger, Rack, and Sinatra.
UPDATE:
Thanks for the responses, but I found them and the approach not helpful, though maybe I'm not understanding it well. It kind of seems like I have to deploy one application inside another, which seems very unclean. What I finally resorted to was having separate server sections, and then updating my /etc/hosts file to have server aliases for the same IP address. So now I have:
http://app1/
and:
http://app2/
and server sections that look like:
server {
listen 80;
server_name app1;
root /data/apps/production/app1/current/public;
passenger_enabled on;
}
server {
listen 80;
server_name app2;
root /data/apps/production/app2/current/public;
passenger_enabled on;
}
and in /etc/hosts:
192.168.1.30 app1 app2
The following worked:
First made symlinks named app1 and app2 pointing to the "public" directory as follows:
ln -s /data/apps/production/fentonGem2/current/public /data/apps/production/fentonGem2/current/app1
ln -s /data/apps/production/fentonGem2/current/public /data/apps/production/fentonGem2/current/app2
Modify nginx.conf to have rails_base_uri, which should look something like the following:
...
server {
listen 80;
server_name arch;
location ^~ /app1 {
root /data/apps/production/fentonGem2/current;
rails_env production;
passenger_enabled on;
passenger_base_uri /app1;
}
location ^~ /app2 {
root /data/apps/production/fentonGem2/current;
rails_env production;
passenger_enabled on;
passenger_base_uri /app2;
}
}
...
Hope this helps.
Not sure, but you might need passenger_base_uri /app1;
More about Passenger and Nginx conf:
http://www.modrails.com/documentation/Users%20guide%20Nginx.html#PassengerBaseURI
EDIT:
"It is allowed to specify this option multiple times. Do this to deploy multiple applications in different sub-URIs under the same virtual host."

Resources