简体   繁体   中英

getting 403 Forbidden nginx with rails, puma, and server blocks

First time trying to deploy an app with rails - I'm trying to get a rails app running which is currently giving me a 403. I'm using ubuntu and nginx. I have two server blocks setup which I tested first with node apps which ran fine, and now one domain runs fine as a node app, but I'm trying to setup the other as a rails app which is giving the 403.

I used a combination of these two tutorials:

I tried setting 755 permision in my /home/deploy dir and /var/www/ , but no luck.

Other guesses -

  • something to do with the index index.html index.htm ?
  • something to do with the default_server deferred; in my nginx.conf (though I tried removing and no affect)

Here are my relevant files, which may be at fault, let me know if others are of help -

rails app, one in question: /etc/nginx/sites-enabled/centers

server {
        listen 80;
        listen [::]:80;

        root /home/deploy/apps/mll/current/public;
        index index.html index.htm;

        server_name myrailssite.com;

        location / {
                try_files $uri $uri/ =404;
        }
}

node app, works as intended at /etc/nginx/sites-enabled/nodeapp

server {
        listen 80 default_server;
        listen [::]:80 default_server ipv6only=on;

        root /var/www/nodeapp/html;
        index index.html index.htm;

        server_name 162.243.199.170;

        location / {
                try_files $uri $uri/ =404;
        }
}

nginx.conf

upstream puma {
  server unix:///home/deploy/apps/mll/shared/tmp/sockets/mll-puma.sock;
}

server {
  listen 80 default_server deferred;
  server_name myrailssite.com;

  root /home/deploy/apps/mll/current/public;
  access_log /home/deploy/apps/mll/current/log/nginx.access.log;
  error_log /home/deploy/apps/mll/current/log/nginx.error.log info;

  location ^~ /assets/ {
    gzip_static on;
    expires max;
    add_header Cache-Control public;
  }

  try_files $uri/index.html $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://mll-puma;
  }

  error_page 500 502 503 504 /500.html;
  client_max_body_size 10M;
  keepalive_timeout 10;
}

Permissions:

deploy@banana:/usr/bin$ namei -l /var/www/centers/
f: /var/www/centers/
drwxr-xr-x root root /
drwxr-xr-x root root var
drwxr-xr-x root root www
drwxrwxr-x root root centers


deploy@banana:/usr/bin$ namei -l /home/deploy/apps/mll/current/public/
f: /home/deploy/apps/mll/current/public/
drwxr-xr-x root   root   /
drwxr-xr-x root   root   home
drwxr-xr-x deploy deploy deploy
drwxr-xr-x deploy deploy apps
drwxr-xr-x deploy deploy mll
lrwxrwxrwx deploy deploy current -> /home/deploy/apps/mll/releases/20160415005003
drwxr-xr-x root   root     /
drwxr-xr-x root   root     home
drwxr-xr-x deploy deploy   deploy
drwxr-xr-x deploy deploy   apps
drwxr-xr-x deploy deploy   mll
drwxr-xr-x deploy deploy   releases
drwxrwxr-x deploy deploy   20160415005003
drwxrwxr-x deploy deploy public

I have also encountered the same issue with my application https://www.wiki11.com .

Your issue is coming because nginx is trying to search for index.html file into /home/deploy/apps/mll/current/public which is not present there. In order to fix, you will need to add passenger with your nginx.

Instructions to follow.

sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 561F9B9CAC40B2F7
sudo apt-get install -y apt-transport-https ca-certificates

Add Passenger APT repository

sudo sh -c 'echo deb https://oss-binaries.phusionpassenger.com/apt/passenger xenial main > /etc/apt/sources.list.d/passenger.list'
sudo apt-get update

Install passenger and nginx

sudo apt-get install -y nginx-extras passenger

Now start nginx webserver.

sudo service nginx start

Next, we need to update the Nginx configuration to point Passenger to the version of Ruby that we're using.

sudo vim /etc/nginx/nginx.conf

And add or uncomment

include /etc/nginx/passenger.conf;

Save and close nginx.conf. Then open /etc/nginx/passenger.conf

sudo vim /etc/nginx/passenger.conf

If you are using .rbenv, then

passenger_ruby /home/deploy/.rbenv/shims/ruby;

Or if you are using rvm, then

passenger_ruby /home/deploy/.rvm/wrappers/ruby-2.5.0/ruby;

Or if you are using system ruby, then

passenger_ruby /usr/bin/ruby;

Next, restart nginx server

sudo service nginx restart

Add passenger_enabled on; into your site-enabled/centers or site-enabled/nodeapp file.

server {
        listen 80;
        listen [::]:80;

        root /home/deploy/apps/mll/current/public;
        index index.html index.htm;

        server_name myrailssite.com;
        passenger_enabled on;

        location / {
                try_files $uri $uri/ =404;
        }
}

Restart nginx server again, sudo service nginx restart . Hopefully it should work.

For more details, follow, https://www.phusionpassenger.com/library/install/nginx/install/oss/xenial/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM