Host multiple rails applications over subfolder using nginx+unicorn - ruby-on-rails-3

I would like to host multiple rails applications using nginx + unicorn which is currently being served using apache + passenger with railsbaseuri. The only reason is being apache needs to be reloaded after every new application is deployed. I would like to know if adding new application is possible in unicorn+nginx without reloading server.
I want to deploy applications on subfolder like host-name/nginx-app1, host-name/nginx-app2 while host-name points to a basic html page.
Read somewhere related to using sockets to handle individual applications and would be looking for some help to implement this. In my case the application is deployed only once with no further iterations. Once i deploy the new application, there should be no downtime in order to make the current application running.
EDIT
config/unicorn.rb file inside the application.
working_directory "/home/ubuntu/application_1"
pid "/home/ubuntu/application_1/pids/unicorn.pid"
stderr_path "/home/ubuntu/application_1/log/unicorn.log"
stdout_path "/home/ubuntu/application_1/log/unicorn.log"
listen "/tmp/unicorn.todo.sock"
worker_processes 2
timeout 30

One way to go about it is hosting the rails applications as UDS. And nginx to have multiple server blocks to read from each UDS (Unix Domain Sockets). Writing the logic adhoc pardon for syntax errors.
e.g. Have a look at this.
http://projects.puppetlabs.com/projects/1/wiki/using_unicorn
You can host app1 using app1.conf for unicorn which will have a line.
listen '/var/run/app1.sock', :backlog => 512
and have multiple nginx upstreams like
upstream app1 {
server unix:/var/run/app1.sock fail_timeout=0;
}
upstream app2 {
server unix:/var/run/app2.sock fail_timeout=0;
}
....
and route requests (proxypass) from a server block based on location or host header
server {
listen 80;
location /app1 {
proxy_pass http://app1;
proxy_redirect off;
}
location /app2 {
proxy_pass http://app2;
proxy_redirect off;
}
}

Related

ASK | Request URL was not found using nginx reverse proxy

First of all, I am so sorry if I broke any rules by making a thread / question.
And also I am sorry for my bad English.
I'm so stressed out to figure out the solution. I am learning about Reverse Proxy on Nginx.
So, I am making 2 servers (both are Centos), one using Apache Web Server and another using Nginx as Web Server. Both will run under an Nginx Reverse Proxy.
This is my configuration
server {
listen 80;
index index.html index.htm;
location / {
proxy_pass http://<apache-ip-address>;
}
location /demo {
proxy_pass http://<nginx-ip-address>;
}
}
And this seems working fine.
I can access the Apache Web Server using http://reverse-proxy-ip/ and access the Nginx Web Server using http://reverse-proxy-ip/demo
But, when I tried to change location / to nginx-ip-address , and /demo to apache-ip-address , I got error "Not Found. The requested URL /demo was not found on this server."
Here the configuration
server {
listen 80;
index index.html index.htm;
location / {
proxy_pass http://<nginx-ip-address>;
}
location /demo {
proxy_pass http://<apache-ip-address>;
}
}
So, my question is, why does this error occur after changing the destination server, and how to solve this problem?
Need some advice for a newbie like me.
Thank you.

I have a Vuejs/nuxt app - I need to make same app for with another style and deploy on same server

I have an existing VueJs/nuxt app. The same app but with different CSS/ Images need to be deployed on same server (different URL) - its the same app but for another client.
Currently what we do is pull the branch on the Linux server and execute npm run generate for the current app.
I presume we need to generate the other app into another folder.
Is this the best solution? Then how do we configure for the new URL to point to this new folder.
example : current URL is www.potato.com and new url for will be www.potato.com/newclient
Thanks for your help.
You can set a Reverse Proxy with Nginx and launch both apps in different ports.
Let's say that potato.com will be in port 4000 and potato.com/newclient will be in port 5000.
You basically state that in your Nginx
server {
servername www.potato.com
listen 80;
listen [::]:80;
location / {
proxy_pass http://127.0.0.1:4000;
}
location /newclient {
proxy_pass http://127.0.0.1:5000;
}
}
Now your apps can live anywhere in your file system. Just deploy them with different ports.

uWSGI and nginx configuration for multiple flask apps running on different ports on the same server

I have multiple small flask apps. I would want to run each of the flask apps on different ports on the same server(single domain).
For example say i have 3 flask apps
tasks.py --> has API endpoints with /task only
users.py --> has API endpoints with /user only
analysis.py --> has API endpoints with /analysis only
domain name : api.test.com
I want to run tasks.py on port 8080 , users.py on port 5000 and analysis.py on say port 4500.
I want to configure my uWSGI and nginx so that when i hit the api.test.com/task/xxxx i want the request to be directed to port 8080 (where tasks.py is running),
similarly api.test.com/user/xxxx should be directed to port 5000 and api.test.com/analysis/xxxx to 4500
It seems to me that you could have one single uWSGI isntance for that with one port, but if you like this way of thinking, then you can follow the next way.
Suppose, you already have several uWSGI instances running on different ports: 8080, 5000 and 4500.
Then you need to create an Nginx config with approximately the following content (read the comments, please):
# webserver configuration
server {
# port to be listened by the web-server
listen 80;
# domain name with its aliases or the ip
server_name api.test.com;
# SETUP THREE LOCATION SECTIONS FOR EACH ACTION
location /task {
# AND SPECIFY THE NEEDED ADDRESS:PORT HERE
uwsgi_pass YOUR_SERVER_IP:8080;
# ONE MAY ALSO USE UNIX-SOCKET (IF ON THE SAME SERVER), INSTEAD OF A PORT
# uwsgi_pass unix:/path/to/the/socket.sock
include /etc/nginx/uwsgi_params; # stadard parms with environment variables required by a uwsgi app
}
location /user {
uwsgi_pass YOUR_SERVER_IP:5000;
include /etc/nginx/uwsgi_params;
}
location /analysis {
uwsgi_pass YOUR_SERVER_IP:4500;
include /etc/nginx/uwsgi_params;
}
}
I hope, you know, how to run three uWSGI instances for each of the ports.
In my opinion, this is extremely ugly, as in order to add any new action you will have to edit Nginx config again. So I don't recommend this solution and only suggest this answer as demonstration of Nginx's capabilities in connection with other web servers.

how do I route multiple domains to multiple node applications?

I'm used to the typical lamp web hosting environment where you just click a few buttons in cpanel and your domain gets zoned and mapped to a folder within htdocs. I've been using node.js a lot and it doesn't seem as simple to do the same thing. If I had multiple node apps and I wanted to route domain1.com:80 and domain2.com:80 each to its own node app and port, how do I go about doing that? where do I start?
This is typically done with nginx. Nginx is a reverse proxy, a piece of software you put infront node.js.
server {
listen 80;
server_name www.domain1.com;
root /var/www/domain1;
location / {
proxy_pass http://localhost:1337; # this is where your node.js app_domain1 is listening
}
}
server {
listen 80;
server_name www.domain2.com;
root /var/www/domain2;
location / {
proxy_pass http://localhost:1338; # this is where your node.js app_domain2 is listening
}
}
From here: Nginx Different Domains on Same IP
I dont recomend apache to do these, nginx fits better with nodejs.
You can run the apps for example at the port 3000 and 3001,
then proxy it to mydomain1:80, and mydomain2:80.
To get mydomain1 and mydomain2 unther the port 80, these is all about DNS not apache.
Theres no way to run apache/nginx and your node httpserver on the same port. ull get a error.
p.s. Im not sure in u can do these #tipical lamp webhost
hope it helps
You can setup virtual domains in Node if you're using Express.
The code you would use to start your server with would look something like this.
var sys = require('sys'),
express = require('express');
var app = express.createServer();
app.configure(function() {
app.use(express.vhost('subdomain1.local', require('./subdomain1/app').app));
app.use(express.vhost('subdomain2.local', require('./subdomain2/app').app));
app.listen(3000);
});
Then you would export app in each subdomain.
var app = express.createServer();
exports.app = app;
Here's a post to read more about vhost in Express.js.

How can I host multiple Rails apps with nginx and Unicorn?

How can I host multiple Rails apps with nginx and Unicorn?
I currently have one site up and running thanks to "Deploying to a VPS".
I have searched but I need a step-by-step guide to get this working. The results I found are not so well explained to help me understand how to accomplish this.
Basically, you do the same thing you did to get everything for your first application running minus the Nginx installation. So, however you got your Unicorn instance for your first application running, do it again for your next application.
You can then just add another server block into your Nginx config with an upstream that points to that new Unicorn instance.
One Nginx running for the entire machine will do fine, with one Unicorn running per application.
Hope this helps some.
Here is a sample of the additional server block you would need to add for Nginx to serve additional applications:
upstream unicorn_app_x {
server unix:/path/to/unicorn/socket/or/http/url/here/unicorn.sock;
}
server {
listen 127.0.0.1:80;
server_name mysitehere.com aliasfor.mysitehere.com;
root /path/to/rails/app/public;
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
if (!-f $request_filename) {
proxy_pass http://unicorn_app_x;
break;
}
}
}
The instructions provided above were not enough.
my startup file: /etc/init.d/unicorn had several references to a single host's configuration. With these configurations, it would not serve a second host.
so I created a new startup instance of unicorn.
cp /etc/init.d/unicorn /etc/init.d/unicorn_app_x
edited /etc/init.d/unicorn_app_x, replacing references to the first site with references to the second: including the unique socket.
then I added the file to startup automatically: update-rc.d act_unicorn defaults
it finally worked!