BrainLikeADullPencil
BrainLikeADullPencil

Reputation: 11663

nginx 403 forbidden error in Rails 4 (with no index.html file)

I'm following along with a Railscast http://railscasts.com/episodes/293-nginx-unicorn?view=asciicast about setting up Nginx and Unicorn on Vagrant, with one important difference. Ryan's making his application with Rails 3 (that has the default /public/index.html that Rails 4 only generates dynamically). After getting Nginx installed and running, we were able to see the default page on port 8080. We then created a basic config file for Nginx to put in the config directory of the rails application

/config/nginx.conf

server {
 listen 80 default;
 # server_name example.com;
 root /vagrant/public; 
}

and then removed the default page in sites enabled and symlinked to the configuration file

vagrant@lucid32:/etc/nginx/sites-enabled$ sudo rm default 
vagrant@lucid32:/etc/nginx/sites-enabled$ sudo ln -s /vagrant/config/nginx.conf todo 

After this, Ryan restarted nginx and was able to see the Rails index page at localhost:8080. However, when I visit localhost:8080, I'm getting a 403 Forbidden error.

403 Forbidden
nginx/1.1.19

Update

since Rails 4 doesn't have the public/index.html file anymore, I think the 403 error might be caused by that, as I learned from this blog post http://www.nginxtips.com/403-forbidden-nginx/. It says to set autoindex to on (the default is off) in the config, but I'm not sure how to set it to get the Rails homepage to show.

When I did this

server {
 listen 80 default;

 root /vagrant/public; 
 location / {
               autoindex on;
        }
}

it got rid of the 403 permissions error (yay!), however, it's not showing the default Rails home page. Rather it's showing the directory structure so I'm wondering what the proper way to set it is. enter image description here

If I try to set it to location/public, I get the 403 error again. Any ideas?

location /public {
                   autoindex on;
            }

Update

Since I'm using Vagrant (virtual box), the app is in /vagrant, however setting the location to location/vagrant also results in a 403 error

location /vagrant {
               autoindex on;
        }

Upvotes: 4

Views: 2932

Answers (1)

btobolaski
btobolaski

Reputation: 168

You'll need to pass the request from Nginx to Unicorn. You can do this like so:

server {
  listen *:80;
  root /vagrant/public;

  location / {
    # Serve static files if they exist, if not pass the request to rails
    try_files $uri $uri/index.html $uri.html @rails;
  }

  location @rails {
    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://127.0.0.1:8080;
  }
}

You may have to change the proxy_pass url. By default, unicorn will listen on 127.0.0.1:8080 but, if you have changed that, then you will need to specify that port.

Upvotes: 2

Related Questions