How to convert Apache's `AllowOverride all` to nginx

前端 未结 3 1376
野性不改
野性不改 2021-01-27 10:27

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


  ServerName localhost
  DocumentRoot /var/www/         


        
相关标签:
3条回答
  • 2021-01-27 10:58

    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.

    0 讨论(0)
  • 2021-01-27 10:59

    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

    0 讨论(0)
  • 2021-01-27 11:17

    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.

    0 讨论(0)
提交回复
热议问题