转换 NginX 服务器的 ExpressionEngine 重写规则

发布于 2024-08-21 16:40:23 字数 2560 浏览 3 评论 0原文

我正在尝试将正在运行的 ExpressionEngine 安装从 Apache 环境迁移到另一个机器上的 NginX 环境。我在尝试将一些 .htaccess 重写转换为 NginX 时遇到了问题。

该网站使用多语言模块,因此需要为每种其他语言制定自定义重写规则。

这是我的标准虚拟主机配置,它似乎可以使 ExpressionEngine 正常工作(没有多语言模块):

server {
  listen        80;
  server_name   domain.co.uk www.domain.co.uk;
  root          /var/www/vhosts/domain.co.uk/http;

  # Redirects non-www to www
  if ($host = 'domain.co.uk') {
    rewrite ^/(.*) http://www.domain.co.uk/$1 permanent;
  }

  access_log    /var/www/vhosts/domain.co.uk/log/access.log;
  error_log     /var/www/vhosts/domain.co.uk/log/error.log;

  location / {
    index       index.html index.htm index.php;
    # Removes index.php from URLs
    if (!-e $request_filename) {
      rewrite ^/(.*)$ /index.php/$1 last;
    }
  }

  # Standard pass for all PHP files
  location ~ \.php$ {
    include       fastcgi_params;
    fastcgi_pass  127.0.0.1:9000;
    fastcgi_param SCRIPT_FILENAME /var/www/vhosts/domain.co.uk/http$fastcgi_script_name;
  }

  # This is where all the ExpressionEngine magic happens
  location ~ \.php($|/) {
    include       fastcgi_params;
    fastcgi_pass  127.0.0.1:9000;

    set           $script $uri;
    set           $path_info "";

    if ($uri ~ "^(.+\.php)(/.+)") {
      set         $script $1;
      set         $path_info $2;
    }

    fastcgi_param SCRIPT_FILENAME /var/www/vhosts/domain.co.uk/http$script;
    fastcgi_param SCRIPT_NAME $script;
    fastcgi_param PATH_INFO $path_info;
  }
}

上面的内容似乎工作得很好并且可以实现我想要的功能。多语言模块文档基于 Apache 设置。对于每种附加语言,它都需要一个具有自己的 htaccess 重写规则的目录 - 有点像这样:

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.domain\.co\.uk$
RewriteRule (.*) http://www.domain.co.uk/ar/$1 [R=301,L]

# Remove index.php
RewriteCond $1 !^(index\.php) [NC]
RewriteRule ^(.*)$ /de/index.php/$1 [L]

我通过添加重新创建了上述规则:

location /de {
  index     index.php;
  if (!-e $request_filename) {
    rewrite ^/(.*)$ /de/index.php/$1 last;
  }
}

添加上述内容在我尝试访问 http 时只会出现 404 错误页面://www.domain.co.uk/de/my_page

所以,我想这可能与 fcgi_param SCRIPT FILENAME 有关,所以我将其更改为:(将 de 添加到路径末尾)

fastcgi_param SCRIPT_FILENAME /var/www/vhosts/spectrumhealthcare.co.uk/http/de$script;

现在这样做会给我一个 <当我访问 http://www.domain.co.uk/de/my_page 时,出现 code>No input file returned 错误。

我现在有点陷入困境,所以真正帮助 SO 社区可以帮助我。你还没有让我失望:)。

I'm trying to migrate a working ExpressionEngine installation from an Apache environment over to an NginX environment on a different box. I have come across a problem trying to convert some .htaccess rewrites to NginX.

The site uses the multi language module so needs a custom rewrite rule for every additional language.

This is my standard vhost config which seems to get ExpressionEngine working nicely (without the multi language module):

server {
  listen        80;
  server_name   domain.co.uk www.domain.co.uk;
  root          /var/www/vhosts/domain.co.uk/http;

  # Redirects non-www to www
  if ($host = 'domain.co.uk') {
    rewrite ^/(.*) http://www.domain.co.uk/$1 permanent;
  }

  access_log    /var/www/vhosts/domain.co.uk/log/access.log;
  error_log     /var/www/vhosts/domain.co.uk/log/error.log;

  location / {
    index       index.html index.htm index.php;
    # Removes index.php from URLs
    if (!-e $request_filename) {
      rewrite ^/(.*)$ /index.php/$1 last;
    }
  }

  # Standard pass for all PHP files
  location ~ \.php$ {
    include       fastcgi_params;
    fastcgi_pass  127.0.0.1:9000;
    fastcgi_param SCRIPT_FILENAME /var/www/vhosts/domain.co.uk/http$fastcgi_script_name;
  }

  # This is where all the ExpressionEngine magic happens
  location ~ \.php($|/) {
    include       fastcgi_params;
    fastcgi_pass  127.0.0.1:9000;

    set           $script $uri;
    set           $path_info "";

    if ($uri ~ "^(.+\.php)(/.+)") {
      set         $script $1;
      set         $path_info $2;
    }

    fastcgi_param SCRIPT_FILENAME /var/www/vhosts/domain.co.uk/http$script;
    fastcgi_param SCRIPT_NAME $script;
    fastcgi_param PATH_INFO $path_info;
  }
}

The above seems to work nicely and does what I want it to. The Multi Language Module documentation is based on an Apache setup. For each additional language it requires a directory with it's own htaccess rewrite rule - a little like this:

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.domain\.co\.uk$
RewriteRule (.*) http://www.domain.co.uk/ar/$1 [R=301,L]

# Remove index.php
RewriteCond $1 !^(index\.php) [NC]
RewriteRule ^(.*)$ /de/index.php/$1 [L]

I have recreated the above rule by adding:

location /de {
  index     index.php;
  if (!-e $request_filename) {
    rewrite ^/(.*)$ /de/index.php/$1 last;
  }
}

Adding the above gets me no further than a 404 error page when I try and visit http://www.domain.co.uk/de/my_page.

So, I figured maybe this was something to do with the fcgi_param SCRIPT FILENAME so I changed that to: (added de to end of path)

fastcgi_param SCRIPT_FILENAME /var/www/vhosts/spectrumhealthcare.co.uk/http/de$script;

Doing this now gives me a No input file specified error when I visit http://www.domain.co.uk/de/my_page.

I'm kind of at a brick wall now so really helping the SO community can help me. You haven't let me down yet :).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

朕就是辣么酷 2024-08-28 16:40:23

我可以回答我自己的问题。看来我的语言重写规则略有错误。应该是这样的:

location /de {
  if (!-e $request_filename) {
    rewrite ^/de/(.*)$ /de/index.php/$1 last;
  }
}

I can answer my own question. Looks like I had language rewrite rule slightly wrong. Is should look like this:

location /de {
  if (!-e $request_filename) {
    rewrite ^/de/(.*)$ /de/index.php/$1 last;
  }
}
太傻旳人生 2024-08-28 16:40:23

应该很简单,在默认语言的 nginx 站点配置文件中,您可以执行如下操作:

location / {
  try_files $uri $uri/ /index.php?q=$uri&$args;
}

对于其他语言支持(例如 de/da/fr/ 等):

location /de {
  try_files $uri $uri/ /de/index.php?q=$uri&$args;
}

Should be straight forward, in nginx site config file for default language you do something like below:

location / {
  try_files $uri $uri/ /index.php?q=$uri&$args;
}

For other language support (say de/da/fr/ etc):

location /de {
  try_files $uri $uri/ /de/index.php?q=$uri&$args;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文