The nginx configuration for gitlab is:
# GITLAB
# Maintainer: #randx
# App Version: 3.0
upstream gitlab {
server unix:/home/gitlab/gitlab/tmp/sockets/gitlab.socket;
}
server {
listen YOUR_SERVER_IP:80; # e.g., listen 192.168.1.1:80;
server_name YOUR_SERVER_FQDN; # e.g., server_name source.example.com;
root /home/gitlab/gitlab/public;
# individual nginx logs for this gitlab vhost
access_log /var/log/nginx/gitlab_access.log;
error_log /var/log/nginx/gitlab_error.log;
location / {
# serve static files from defined root folder;.
# #gitlab is a named location for the upstream fallback, see below
try_files $uri $uri/index.html $uri.html #gitlab;
}
# if a file, which is not found in the root folder is requested,
# then the proxy pass the request to the upsteam (gitlab unicorn)
location #gitlab {
proxy_read_timeout 300; # https://github.com/gitlabhq/gitlabhq/issues/694
proxy_connect_timeout 300; # https://github.com/gitlabhq/gitlabhq/issues/694
proxy_redirect off;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://gitlab;
}
}
What should i change to serve gitlab as a surURI, www.mysuperserver.com/gitlab
i've tried many different things, but nothing worked
thanks
I've successfully made it working under a subdir url.
follow the instruction in the source code, like in /home/git/gitlab/config/gitlab.yml
# Uncomment and customize the last line to run in a non-root path
# WARNING: We recommend creating a FQDN to host GitLab in a root path instead of this.
# Note that four settings need to be changed for this to work.
# 1) In your application.rb file: config.relative_url_root = "/gitlab"
# 2) In your gitlab.yml file: relative_url_root: /gitlab
# 3) In your unicorn.rb: ENV['RAILS_RELATIVE_URL_ROOT'] = "/gitlab"
# 4) In ../gitlab-shell/config.yml: gitlab_url: "http://127.0.0.1/gitlab"
# To update the path, run: sudo -u git -H bundle exec rake assets:precompile RAILS_ENV=production
#
relative_url_root: /gitlab
change the nginx config to serve a suburi, plz refer to my example below:
The key point is the root under context server and alias under location. Plz refer to nginx pitfalls, nginx root note for more details.
# default.conf for nginx
upstream gitlab {
server unix:/home/git/gitlab/tmp/sockets/gitlab.socket;
}
server {
listen 80;
server_name $YOUR_DOMAIN;
# other settings, especially root settings, like below
# root /usr/local/nginx/html;
location /gitlab {
# serve static files from defined root folder;
alias /home/git/gitlab/public;
# individual nginx logs for this gitlab vhost
access_log /var/log/nginx/gitlab_access.log;
error_log /var/log/nginx/gitlab_error.log;
# #gitlab is a named location for the upstream fallback, see below
try_files $uri $uri/index.html $uri.html #gitlab;
}
location #gitlab {
proxy_read_timeout 300; # https://github.com/gitlabhq/gitlabhq/issues/694
proxy_connect_timeout 300; # https://github.com/gitlabhq/gitlabhq/issues/694
proxy_redirect off;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://gitlab;
}
# other locations' settings...
}
As of Gitlab 5.3 you can configure it to run in a suburi out of the box using the official installation document.
Uncomment line 8 of config/puma.rb:
ENV['RAILS_RELATIVE_URL_ROOT'] = "/"
Similarly for line 23 in config/gitlab.yml:
relative_url_root: /
I didn't have to modify my nginx config at all for it to work.
For old versions, for example, gitlab 7.4.5 , there is no gitlab-git-http-server (gitlab-workhorse). But there are some solutions for gitlab 7.4.5 to use suburi in the doc.
config/application.rb
# Relative url support
# Uncomment and customize the last line to run in a non-root path
# WARNING: We recommend creating a FQDN to host GitLab in a root path instead of this.
# Note that following settings need to be changed for this to work.
# 1) In your application.rb file: config.relative_url_root = "/gitlab"
# 2) In your gitlab.yml file: relative_url_root: /gitlab
# 3) In your unicorn.rb: ENV['RAILS_RELATIVE_URL_ROOT'] = "/gitlab"
# 4) In ../gitlab-shell/config.yml: gitlab_url: "http://127.0.0.1/gitlab"
# 5) In lib/support/nginx/gitlab : do not use asset gzipping, remove block starting with "location ~ ^/(assets)/"
#
# To update the path, run: sudo -u git -H bundle exec rake assets:precompile RAILS_ENV=production
#
# config.relative_url_root = "/gitlab"
config/gitlab.yml
# WARNING: See config/application.rb under "Relative url support" for the list of
# other files that need to be changed for relative url support
# relative_url_root: /gitlab
Configuring gitlab 7.4.5 with other website using same port and same domain name in a nginx configuration or apache configuration file seems still a challenge. I did not get this. I can access gitlab as www.mydomain.com/gitlab , but I did not get my another website in another root directory with www.mydomain.com. It should be resoved by configure nginx or apache. Hope people who are familiar with nginx or apache can give a solution.
Other references.Support installing GitLab in a relative URL path or sub directory #1950
EDIT
It works now.
stackoverflow suggest to use www.example.com as example here.
www.example.com/gitlab access gitlab.
www.example.com access another website, say, my blog.
Steps:
config/application.rb file: config.relative_url_root = "/gitlab"
config/gitlab.yml file: relative_url_root: /gitlab
config/unicorn.rb: ENV['RAILS_RELATIVE_URL_ROOT'] = "/gitlab"
../gitlab-shell/config.yml: gitlab_url: "http://www.example.com/gitlab"
copy lib/support/nginx/gitlab to gitlab.conf for nginx and do not use asset gzipping, remove block starting with "location ~^/(assets)/"
run: sudo -u git -H bundle exec rake assets:precompile
RAILS_ENV=production
Urls:
config/gitlab.yml file: host: example.com port: 80
config/unicorn.rb: listen "127.0.0.1:9095"
../gitlab-shell/config.yml: gitlab_url:
http://www.example.com/gitlab
gitlab will give git two way to access:
git#example.com:sample-project.git
http://example.com/gitlab/sample-project.git
I am not using https.
If you got:
git#example.com/gitlab:sample-project.git
http://example.com/gitlab/gitlab/sample-project.git
You might configure config/gitlab.yml as host: example.com/gitlab. Just remove the /gitlab.
nginx configuration file:
###################################
## configuration ##
###################################
##
upstream gitlab {
server unix:/home/git/gitlab/tmp/sockets/gitlab.socket fail_timeout=0;
}
## Normal HTTP host
server {
#listen *:80 default_server;
listen *:80 default_server;
server_name www.example.com; ## Replace this with something like gitlab.example.com
server_tokens off; ## Don't show the nginx version number, a security best practice
#root /home/git/gitlab/public;
root html;
location /{
#root html;
index index.html index.htm;
}
## Increase this if you want to upload large attachments
## Or if you want to accept large git objects over http
client_max_body_size 20m;
## Individual nginx logs for this GitLab vhost
access_log logs/example.gitlab_access.log;
error_log logs/example.gitlab_error.log;
location /gitlab {
alias /home/git/gitlab/public;
## Serve static files from defined root folder.
## #gitlab is a named location for the upstream fallback, see below.
try_files $uri $uri/index.html $uri.html #gitlab;
}
## If a file, which is not found in the root folder is requested,
## then the proxy passes the request to the upsteam (gitlab unicorn).
location #gitlab {
## If you use HTTPS make sure you disable gzip compression
## to be safe against BREACH attack.
# gzip off;
## https://github.com/gitlabhq/gitlabhq/issues/694
## Some requests take more than 30 seconds.
proxy_read_timeout 300;
proxy_connect_timeout 300;
proxy_redirect off;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Frame-Options SAMEORIGIN;
proxy_pass http://gitlab;
}
## Enable gzip compression as per rails guide:
## http://guides.rubyonrails.org/asset_pipeline.html#gzip-compression
## WARNING: If you are using relative urls remove the block below
## See config/application.rb under "Relative url support" for the list of
## other files that need to be changed for relative url support
#location ~ ^/(assets)/ {
# root /home/git/gitlab/public;
# #gzip_static on; # to serve pre-gzipped version
# expires max;
# add_header Cache-Control public;
#}
error_page 502 /502.html;
}
Apache(2.2.9) configuration file: Reference gitlab.conf for gitlab 6.0.6 and gitlab-8.0-apache2.2.conf for gitlab 8.0.0 on apache 2.2
# Module dependencies
# mod_rewrite
# mod_proxy
# mod_proxy_http
<VirtualHost *:80>
ServerAdmin admin#example.com
DocumentRoot "/data/webapp/www/wordpress"
ServerName www.example.com
ServerAlias example.com
#ErrorLog "logs/wordpress-error_log"
#CustomLog "logs/wordpress-access_log" common
#SetEnv ZF2_PATH "/data/webapp/www/ZendFramework-2.3.3/library"
SetEnv APPLICATION_ENV "development"
<Directory /data/webapp/www/wordpress>
DirectoryIndex index.php
AllowOverride All
Order allow,deny
Allow from all
</Directory>
#ServerName www.example.com
ServerSignature Off
ProxyPreserveHost On
# Ensure that encoded slashes are not decoded but left in their encoded state.
# http://doc.gitlab.com/ce/api/projects.html#get-single-project
AllowEncodedSlashes NoDecode
<Location /gitlab>
Order deny,allow
Allow from all
ProxyPassReverse http://127.0.0.1:9095
ProxyPassReverse http://www.example.com//
RewriteEngine on
#RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
RewriteRule .* http://127.0.0.1:9095%{REQUEST_URI} [P,QSA,NE]
</Location>
#apache equivalent of nginx try files
# http://serverfault.com/questions/290784/what-is-apaches-equivalent-of-nginxs-try-files
# http://stackoverflow.com/questions/10954516/apache2-proxypass-for-rails-app-gitlab
# RewriteEngine on
# RewriteCond /code/gitlab/{REQUEST_FILENAME} !-f
# RewriteRule .* http://127.0.0.1:9095%{REQUEST_URI} [P,QSA,NE]
# needed for downloading attachments
#DocumentRoot /home/git/gitlab/public
Alias /gitlab /home/git/gitlab/public
#Set up apache error documents, if back end goes down (i.e. 503 error) then a maintenance/deploy page is thrown up.
ErrorDocument 404 /404.html
ErrorDocument 422 /422.html
ErrorDocument 500 /500.html
ErrorDocument 503 /deploy.html
LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b" common_forwarded
ErrorLog logs/example.com_error.log
CustomLog logs/example.com_forwarded.log common_forwarded
CustomLog logs/example.com_access.log combined env=!dontlog
CustomLog logs/example.com.log combined
</VirtualHost>
Have you resolved this issue yet?
If not, try updating the location / directive to:
location /gitlab {
# serve static files from defined root folder;.
root /home/gitlab/gitlab/public;
# #gitlab is a named location for the upstream fallback, see below
try_files $uri $uri/index.html $uri.html #gitlab;
}
If that doesn't work, please paste the last few lines of /var/log/nginx/gitlab_error.log.
This configuration works
# GITLAB
# Maintainer: #randx
# App Version: 3.0
upstream gitlab {
server unix:/home/gitlab/gitlab/tmp/sockets/gitlab.socket;
}
server {
listen 80; # e.g., listen 192.168.1.1:80; 37.59.125.28:
server_name gitlab.<YOUR_DOMAIN>.com; # e.g., server_name source.example.com;
root /home/gitlab/gitlab/public;
# individual nginx logs for this gitlab vhost
access_log /var/log/nginx/gitlab_access.log;
error_log /var/log/nginx/gitlab_error.log;
location / {
# serve static files from defined root folder;.
# #gitlab is a named location for the upstream fallback, see below
try_files $uri $uri/index.html $uri.html #gitlab;
}
# if a file, which is not found in the root folder is requested,
# then the proxy pass the request to the upsteam (gitlab unicorn)
location #gitlab {
proxy_read_timeout 300; # https://github.com/gitlabhq/gitlabhq/issues/694
proxy_connect_timeout 300; # https://github.com/gitlabhq/gitlabhq/issues/694
proxy_redirect off;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://gitlab;
}
}
And I had a bad symbolic link between nginx available configuration and the enabled configuration.
Related
duelingpetWS2 droplet
address: http://68.183.163.139/
Currently installed:
NodeJS
NPM
Rbenv
ruby 2.5.1p57
Rails 5.2.2
MySQL
Ubuntu 18.04
nginx
/var/www/duelingpets.net/html/index.html
<html>
<head>
<title>Welcome to Duelingpets.net!</title>
</head>
<body>
<h1>Success! The duelingpets.net server block is working!</h1>
</body>
</html>
New Version
/etc/nginx/sites-available/duelingpets.net
upstream duelingpets {
server unix:///path/to/web/tmp/puma.sock;
}
server {
listen 80;
listen [::]:80;
root /var/www/duelingpets.net/html;
#index index.html index.htm index.nginx-debian.html;
server_name duelingpets.net www.duelingpets.net;
try_files $uri #app;
location #app {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://duelingpets;
}
}
Old version
/etc/nginx/sites-available/duelingpets.net
server {
listen 80;
listen [::]:80;
root /var/www/duelingpets.net/html;
index index.html index.htm index.nginx-debian.html;
server_name duelingpets.net www.duelingpets.net;
location / {
try_files $uri $uri/ =404;
}
}
sudo ln -s /etc/nginx/sites-available/duelingpets.net /etc/nginx/sites-enabled/
sudo vim /etc/nginx/nginx.conf
http {
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# server_tokens off;
server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
Current Behavior of the site.
Welcome to nginx!
If you see this page, the nginx web server is successfully installed and working. Further configuration is required.
For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.
Thank you for using nginx.
I guess you are using Puma as rails' app server, check this ticket: https://github.com/puma/puma/issues/125
It sets this config
<VirtualHost *:80>
NameVirtualHost 99.99.99.99
ServerName yourapp.com
ServerSignature Off
ProxyRequests Off
<Proxy *>
Order Allow,Deny
Allow from all
</Proxy>
ProxyPass / http://localhost:3000/
ProxyPassReverse / http://localhost:3000/
ProxyVia On
</VirtualHost>
Note the proxypass to localhost:3000, this is the important part (you don't need document root).
And make sure you start puma with puma -b tcp://127.0.0.1:3000 so it works via tcp instead of sockets.
Anyway, I prefer using nginx instead of apache, you can set nginx to use sockets which is how puma starts by default and there are more tutorials for nginx+puma (there's the config for nginx on that link too).
I am trying to set up apache in production server. My nginx configuration is shown below.
upstream puma {
server unix:///var/www/rails/shared/tmp/sockets/puma.sock;
}
server {
listen 80 default_server deferred;
# server_name example.com;
root /var/www/rails/current/public;
access_log /var/www/rails/current/log/nginx.access.log;
error_log /var/www/rails/current/log/nginx.error.log info;
location ^~ /assets/ {
gzip_static on;
expires max;
add_header Cache-Control public;
}
try_files $uri #puma;
location #puma {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://puma;
}
error_page 500 502 503 504 /500.html;
client_max_body_size 10M;
keepalive_timeout 10;
}
But I couldn't find how to specify socket in httpd configuration. Please help.
To set the port an Apache server listens on, use the Listen directive.
Here’s what it says in the default httpd.conf file (found in /etc/apache2/ in the apache2 install I’m using — although different installs/systems put the config directory in other locations):
# Listen: Allows you to bind Apache to specific IP addresses and/or
# ports, instead of the default. See also the <VirtualHost>
# directive.
#
# Change this to Listen on specific IP addresses as shown below to
# prevent Apache from glomming onto all bound IP addresses.
#
#Listen 12.34.56.78:80
#Listen 66.18.208.111:80
#Listen 66.18.208.112:80
Listen 80
If you are using named virtual hosts (multiple websites on one server) you’ll want to see the instructions for the NameVirtualHost and VirtualHost directives in Apache 2.2 vhosts documentation
To use Nginx's X-Accel-Redirect feature with passenger, apparently you use passenger_set_header and, if mapping to another location, passenger_set_cgi_param. For instance, here is a configuration which apparently used to work for someone else:
passenger_set_cgi_param HTTP_X_ACCEL_MAPPING "/home/user/rails_app/shared/files/=/documents/";
passenger_pass_header X-Accel-Redirect;
location ~ ^/documents/(\d\d\d)/(\d\d\d)/(\d\d\d)/(.*)$ {
alias /home/user/rails_app/shared/files/$1/$2/$3/$4;
internal;
}
But with passenger 5 they say in release notes:
[Nginx] The passenger_set_cgi_param option has been removed and
replaced by passenger_set_header and passenger_env_var.
Not much information about how to use the two together though for X-Accel-Redirect. No up-to-date tutorials or blogs seem to show how to do it either. How is this done? I can get the following nginx.conf to work for the rails development server (non-passenger) but it does not work with passenger.
upstream api_server {
server localhost:5000;
# (starting passenger with ``` RAILS_ENV=development passenger start -a 127.0.0.1 -p 5000 -d ```) not using unix:socket for a good reason
}
server {
listen 9000;
server_name $host;
return 301 https://$host:9443$request_uri;
#error_page 497 https://$host:9443$request_uri;
}
server {
charset UTF-8;
server_name localhost 0.0.0.0;
root /var/www/html/app;
listen 9443 ssl;
ssl on;
ssl_certificate /opt/nginx/conf/ssl/app.chain.pem;
ssl_certificate_key /opt/nginx/conf/ssl/app.key.pem;
error_page 497 https://$host:9443$request_uri;
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Expose-Headers' 'Content-Length, Content-Type, Keep-Alive, Date, Server, Transfer-Encoding, Cache-Control';
add_header 'Access-Control-Allow-Headers' 'Content-Length, Content-Type, Keep-Alive, Date, Server, Transfer-Encoding, Cache-Control';
passenger_env_var X-Sendfile-Type "X-Accel-Redirect";
passenger_env_var X-Accel-Mapping "/special/place/on/filesystem/=/protected_files/";
passenger_pass_header X-Accel-Redirect;
passenger_pass_header X-Sendfile-Type;
# --------- Serve static applications --------
location / {
try_files $uri $uri/ /index.html;
}
# --------- API --------
location /protected_files/{
# Used for X-Accel-Redirect
internal;
add_header Pragma "no-cache";
alias /special/place/on/filesystem/;
}
location ~ /(api|auth|raw)/ {
# Host + forwarding headers
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
passenger_pass_header Host;
passenger_pass_header X-Real-IP;
passenger_pass_header X-Forwarded-For;
# Configuration for X-Sendfile style fast & authenticated static serving
proxy_set_header X-Sendfile-Type X-Accel-Redirect;
# proxy_set_header X-Accel-Mapping /mounts/test_data_filesystem/=/protected_files/;
proxy_set_header X-Accel-Mapping /special/place/on/filesystem/=/protected_files/;
passenger_env_var X-Sendfile-Type "X-Accel-Redirect";
passenger_env_var X-Accel-Mapping "/special/place/on/filesystem/=/protected_files/";
passenger_pass_header X-Accel-Redirect;
passenger_pass_header X-Sendfile-Type;
proxy_pass http://api_server;
}
Looks like you found an alternate solution, but posting this in case others run into the same issue and wish to upgrade from Passenger 4 syntax to Passenger 5 along with X-Accel-Redirect.
The following is the changes that worked for me:
Passenger 4 version:
passenger_set_cgi_param HTTP_X_ACCEL_MAPPING /path/to/railsapp/public/=/storage/;
passenger_pass_header X-Accel-Redirect;
location /storage {
root /var/www/shared;
internal;
}
Passenger 5 version:
passenger_set_header X-Sendfile-Type "X-Accel-Redirect";
passenger_env_var HTTP_X_ACCEL_MAPPING /path/to/railsapp/public/=/storage/;
passenger_pass_header X-Accel-Redirect;
location /storage {
root /var/www/shared;
internal;
}
Also, there's a symbolic link to the rails app in /var/www/shared, ln -s /path/to/railsapp/public /var/www/shared/storage, however this can be different based on your nginx configuration.
Hope this helps!
This question already has answers here:
multiple rails apps on nginx and unicorn
(3 answers)
Closed 8 years ago.
I'm looking for set up a nginx server with unicorn. I the first app is set but it's on the root "/". what i really want is type localhost/app1 and it would run, while if a just enter to the root, html or php pages are going to be open.
Any clue?
Here's the current nginx.config:
worker_processes 4;
user nobody nogroup; # for systems with a "nogroup"
pid /tmp/nginx.pid;
error_log /tmp/nginx.error.log;
events {
worker_connections 1024; # increase if you have lots of clients
accept_mutex off; # "on" if nginx worker_processes > 1
}
http {
include mime.types;
default_type application/octet-stream;
access_log /tmp/nginx.access.log combined;
sendfile on;
tcp_nopush on; # off may be better for *some* Comet/long-poll stuff
tcp_nodelay off; # on may be better for some Comet/long-poll stuff
gzip on;
gzip_http_version 1.0;
gzip_proxied any;
gzip_min_length 500;
gzip_disable "MSIE [1-6]\.";
gzip_types text/plain text/html text/xml text/css
text/comma-separated-values
text/javascript application/x-javascript
application/atom+xml;
upstream sip {
server unix:/home/analista/www/sip/tmp/sockets/sip.unicorn.sock fail_timeout=0;
}
server {
listen 80 default deferred; # for Linux
client_max_body_size 4G;
server_name sip_server;
keepalive_timeout 5;
# path for static files
root /home/analista/www/sip/public;
try_files $uri/index.html $uri.html $uri #app;
location #app {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
# proxy_buffering off;
proxy_pass http://sip;
}
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://sip;
break;
}
}
# Rails error pages
error_page 500 502 503 504 /500.html;
location = /500.html {
root /home/analista/www/sip/public;
}
}
}
I've got it!
Turns out it was really simple and I wrote a post about it on my blog. http://jrochelly.com/post/2013/08/nginx-unicorn-multiple-rails-apps/
Here's the content:
I'm using Ruby 2.0 and Rails 4.0. I suppose you already have nginx and unicorn installed. So, let's get started!
In you nginx.conf file we are going to make nginx point to a unicorn socket:
upstream unicorn_socket_for_myapp {
server unix:/home/coffeencoke/apps/myapp/current/tmp/sockets/unicorn.sock fail_timeout=0;
}
Then, with your server listening to port 80, add a location block that points to the subdirectory your rails app is (this code, must be inside server block):
location /myapp/ {
try_files $uri #unicorn_proxy;
}
location #unicorn_proxy {
proxy_pass http://unix:/home/coffeencoke/apps/myapp/current/tmp/sockets/unicorn.sock;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_set_header X-Forwarded-Proto $scheme;
}
Now you can just Unicorn as a Deamon:
sudo unicorn_rails -c config/unicorn.rb -D
The last thing to do, and the one I dug the most is to add a scope for your rails routes file, like this:
MyApp::Application.routes.draw do
scope '/myapp' do
root :to => 'welcome#home'
# other routes are always inside this block
# ...
end
end
This way, your app will map a link /myapp/welcome, intead of just /welcome
But there's a even better way
Well, the above will work on production server, but what about development? Are you going to develop normally then on deployment you change your rails config? For every single app? That's not needed.
So, you need to create a new module that we are going to put at lib/route_scoper.rb:
require 'rails/application'
module RouteScoper
def self.root
Rails.application.config.root_directory
rescue NameError
'/'
end
end
After that, in your routes.rb do this:
require_relative '../lib/route_scoper'
MyApp::Application.routes.draw do
scope RouteScoper.root do
root :to => 'welcome#home'
# other routes are always inside this block
# ...
end
end
What we are doing is to see if the root directory is specified, if so use it, otherwise, got to "/". Now we just need to point the root directory on config/enviroments/production.rb:
MyApp::Application.configure do
# Contains configurations for the production environment
# ...
# Serve the application at /myapp
config.root_directory = '/myapp'
end
In config/enviroments/development.rb I do not specify the config.root_directory. This way it uses the normal url root.
I just got VM which had nginx, unicorn and a fresh rails app installed. I looked at the nginx config file, and I do not understand how it connects to unicorn, especially since there doesn't seem to be any upstream setting (which is what most tutorial's say you need).
Here are my config settings:
/home/unicorn/unicorn.conf
listen "127.0.0.1:8080"
worker_processes 2
user "rails"
working_directory "/home/rails"
pid "/home/unicorn/pids/unicorn.pid"
stderr_path "/home/unicorn/log/unicorn.log"
stdout_path "/home/unicorn/log/unicorn.log"
/etc/nginx/sites-enabled/default - only default file within sites-enabled directory
server {
listen 80;
root /home/rails/public;
server_name _;
index index.htm index.html;
location / {
try_files $uri/index.html $uri.html $uri #app;
}
location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js|mp3|flv|mpeg|avi)$ {
try_files $uri #app;
}
location #app {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://app_server;
}
}
The server seems to be defining a location #app, and passing requests to http://app_server ... I am not sure where does it obtain the app_server ... would that be an upstream defined elsewhere?
Fore more info, this article has the exact same set up as my vps. https://www.digitalocean.com/community/articles/how-to-1-click-install-ruby-on-rails-on-ubuntu-12-10-with-digitalocean
Normally app_server would be defined in another file as an upstream. For example - on my debian system I have a location set up similar to yours pointing to an upstream called www
in my /etc/nginx/sites-enabled directory I have a file called upstream_www_app with the following content
upstream www {
server localhost:24910 fail_timeout=0 weight=5;
}
24910 is the port defined in my applications config/unicorn.rb and my main nginx.conf includes all files in the sites-enabled directory.