【问题标题】:Problems moving Sites from Apache to Nginx (mod_rewrite)将站点从 Apache 移动到 Nginx 的问题 (mod_rewrite)
【发布时间】:2017-11-28 06:32:20
【问题描述】:

我正在将一些虚拟主机从 apache 迁移到 nginx。不幸的是,由于 apache 实例中的 .htaccess 文件,我遇到了一些麻烦。

情况如下(apache):

DocumentRoot 有不同的文件夹,里面有项目。我可以毫无问题地访问其中的大多数(通过www.example.com/appname)。

但是在访问(主页)www.example.com 时,以下.htaccess 文件会重定向用户。

/var/www/html/.htaccess:

Redirect /index.html https://example.com/example/

然后在 /var/www/html/example/.htaccess 中有另一个 .htaccess

Options -Indexes IndexIgnore */* Options FollowSymlinks RewriteEngine on RewriteRule ^(.+)?$ web/$1

那么 /var/www/html/example/web 中的下一个 .htaccess

RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . index.php

如何将其转换为 nginx 位置块或块?我似乎无法正确处理。在我上次检查时,客户端浏览器说重定向有问题。这是 nginx 虚拟主机配置:

请记住,我仍然找到了一些尝试,但到目前为止我没有任何运气。非常感谢您的帮助!

谢谢大家

server {
  server_name example.com www.example.com;
  listen 80;

  root   /var/www/html;
  index index.php index.html index.htm;

  access_log /dev/stdout;
  error_log /dev/stdout info;

  # FIRST .htaccess FILE
  # redirect index.html to example.com/example
  # the first line seems to have worked, the 2nd one is not yet tested
  location /index.html { rewrite ^(.*)$ http://example.com/example/ redirect; }
  location /index.html { return $301 $scheme:http://example.com/example/$request_uri; }

  location / {
    try_files $uri $uri/ /index.php?$args /index.html;
    #try_files $uri /index.php?$args /index.html;
  }

  location /example {
    root /var/www/html;

    # try_files options: 
    # try_files $uri $uri/ /index.php?$args;
    # try_files $uri /index.php?url=$request_uri;

    # SECOND .htaccess FILE
    rewrite ^/(.+)?$ /web/$1;

    location /example/web {
      # THIRD .htaccess FILE

      # option 1
      # if (!-e $request_filename){ rewrite ^(.*)$ /index.php; }

      # option 2
      #rewrite ^/(.*)$ /$1.php last;

      try_files $uri $uri/ /index.php$args;
    }

  }


  location ~ \.php$ {
    include fastcgi_params;
    fastcgi_pass php5:9000;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  }


  # deny access to .htaccess files, if Apache's document root
  # ocurs with nginx's one
  #
  location ~ /\.ht {
      deny  all;
  }
}

【问题讨论】:

  • “客户端浏览器说重定向有问题” - 你能发布错误消息吗?
  • 该配置中发生了很多事情。如果我是你,我会删除所有那些 location 块,然后一个一个地重新添加它们,并在添加更多之前测试以确保它在每个阶段都在做我期望的事情

标签: php apache .htaccess mod-rewrite nginx


【解决方案1】:

看看这个:Apache mod_rewrite to Nginx rewrite rules.
基本上,apache rewrite 和 nginx rewrite 并不完全是同一件事。看来您正在使用 nginx rewrite,而您应该使用 nginx try_files。 所以,你可以尝试做

try_files /web/$uri /web/$uri/ /web/index.php?$args

而不是

rewrite ^/(.+)?$ /web/$1;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-11
    • 1970-01-01
    • 2015-10-09
    • 1970-01-01
    • 1970-01-01
    • 2014-06-04
    相关资源
    最近更新 更多