如何将 Apache 的 `AllowOverride all` 转换为 nginx

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

我有一个用于 Rails 应用程序的 Apache2 和 Passenger 站点,它使用以下配置:

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

是否有 nginx 和 Passenger 站点的等效项?目前 nginx 以 {ID}/action404 形式响应所有请求。这是 nginx conf 的相关部分:

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

应用程序中没有 .htaccess 文件。

问题确实出在try_files,只需删除该行即可解决问题。更好、更易读的解决方案是:

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;
}

这是静态文件,无需向 Passenger 传递请求即可访问。

这里是客作者。 Nginx 中没有 'AllowOverride all' 的等效项,您也不需要它。您只需要一个虚拟主机块,其中 'root' 指向您应用程序的 'public' 目录,并且 'passenger_enabled on':

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

...如 the official Passenger documentation's deployment instructions 所述。

passenger_app_rootpassenger_document_root 是根据 root 自动为您推断的。也不需要 try_files 因为 Passenger automatically serves static files for you through Nginx.

你应该阅读 the official documentation

接受的答案与乘客证件更相关。我偶然发现这个线程的主要原因是我想将 .htaccessAllow Override 设置迁移到 nginx.conf

如果您对 nginx 了解不多,您会假设(就像我所做的那样)必须有一种方法可以让每个目录基础 nginx.conf 就像我们在 Apache 中有。

但这不是 nginx 实现其设置系统的方式,原因是在结构方面,他们发现集中配置更好,而不是将设置分散在多个文件 (.htaccess) 中,这使得很难知道设置是否有效。

因此,要从 .htaccess 文件迁移您的设置,必须将这些设置(nginx 的等效语法)复制到单个 nginx.conf Server{...}块。

参考链接:

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