Nginx 403 forbidden error in Rails 4 (without index.html file)

I follow Railscast http://railscasts.com/episodes/293-nginx-unicorn?view=asciicast about creating Nginx and Unicorn on Vagrant with one important difference. Ryan makes his application with Rails 3 (which has default / public / index.html, which Rails 4 only generates dynamically). After installing and starting Nginx, we were able to see the default page on port 8080. Then we created a basic configuration file for Nginx to be placed in the rails application configuration directory

/config/nginx.conf

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

and then deleted the default page on sites included and tied 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 that, Ryan restarted nginx and was able to see the Rails index page on localhost: 8080. However, when I visit localhost: 8080, I get 403 Forbidden error.

 403 Forbidden nginx/1.1.19 

Update

since Rails 4 no longer has a public / index.html file, I think the 403 error could be caused by this, as I found out from this blog post at http://www.nginxtips.com/403-forbidden-nginx/ . He says that to automatically set autoindex to on (disabled by default) in the config, but I'm not sure how to set it to show the Rails main page.

When i did it

 server { listen 80 default; root /vagrant/public; location / { autoindex on; } } 

he got rid of 403 permission errors (yay!), however he does not show the default Rails homepage. Rather, it shows the directory structure, so I wonder what the correct way to install it. enter image description here

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

 location /public { autoindex on; } 

Update

Since I use Vagrant (virtual box), the application is in / vagrant, however setting the location to location / vagrant also leads to 403 error

 location /vagrant { autoindex on; } 
+4
source share
1 answer

You will need to transfer the request from Nginx to Unicorn. You can do it like this:

 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 need to change the proxy_pass URL. By default, the unicorn will listen on 127.0.0.1:8080, but if you change it, you will need to specify this port.

+2
source

Source: https://habr.com/ru/post/1492130/


All Articles