How to convert Apache's `AllowOverride all` to

2019-09-05 22:27发布

问题:

I have an Apache2 & Passenger site for Rails app, that uses following configuration:

<VirtualHost *:80>
  ServerName localhost
  DocumentRoot /var/www/site/public
  <Directory /var/www/site/public>
    AllowOverride all
    Options -MultiViews
  </Directory>
</VirtualHost>

Is there an equivalent for nginx & Passenger site? Currently nginx responds to all requests in form {ID}/action with 404. Here's the relevant part of nginx conf:

   location / {
      passenger_app_root /var/www/site;
      passenger_document_root /var/www/site/public;
      passenger_enabled on;
      try_files $uri $uri/ =404;
   }

There's no .htaccess file in the app.

回答1:

Passenger author here. There is no equivalent for 'AllowOverride all' in Nginx, and you don't need it either. All you need is a virtual host block with the 'root' pointing to your app's 'public' directory, and 'passenger_enabled on':

server {
   listen 80;
   server_name www.example.com;
   root /var/www/site/public;
   passenger_enabled on;
}

...as explained by the official Passenger documentation's deployment instructions.

passenger_app_root and passenger_document_root are automatically inferred for you from root. There is no need for try_files either because Passenger automatically serves static files for you through Nginx.

You should read the official documentation.



回答2:

The problem was indeed in the try_files, just removing the line solves the issue. Slightly better and more readable solution would be:

location / {
    # nginx won't display 404, we leave this to Rails
    try_files $uri @passenger;
}

location @passenger {
   passenger_app_root /var/www/site;
   passenger_document_root /var/www/site/public;
   passenger_enabled on;
}

This was static files can be accessed without passing request to Passenger.



回答3:

The accepted answer was more related to Passenger documentation. The main reason I stumbled upon this thread is that I was looking to migrate Allow Override setting of .htaccess to nginx.conf.

If you don't know much about nginx you would assume(as I did) that there must be a way to have nginx.conf per directory basis just like we have in Apache.

But that's not the way nginx implemented its settings system and the reason is that structure-wise they find central configurations better instead of having settings scattered over multiple files(.htaccess) making it tough to know the settings effective.

So, to migrating your settings from a .htaccess file, one has to copy those settings(equivalent syntax of nginx) over to single nginx.conf Server{...} block.

Reference links:

https://www.nginx.com/blog/converting-apache-to-nginx-rewrite-rules/

https://www.digitalocean.com/community/tutorials/how-to-migrate-from-an-apache-web-server-to-nginx-on-an-ubuntu-vps