【问题标题】:Existing Apache .htaccess to Nginx Config现有的 Apache .htaccess 到 Nginx 配置
【发布时间】:2018-02-24 13:00:39
【问题描述】:

我想我有点迷路了。

我尝试了一些解决方案,但我没有得到一个部分。一切都必须由 index.php 处理,我无法让它在 nginx 中工作。我一直在学习 Nginx,因为它很棒,但非常感谢某人的快速帮助。

我的 .htaccess:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?_route_=$1 [L,QSA]

我的 Nginx 配置是:

server {
        listen 80;
        listen [::]:80;

        root /folder;
        index index.php index.html index.htm index.nginx-debian.html;

        server_name hostname.com;

        location / {
        try_files $uri $uri/ =404;
            }

    location ~ \.php$ {
        try_files = $uri @missing;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
        location @missing {
           rewrite ^ $scheme://$host/index.php permanent;
}
}

有人能指出我正确的方向吗?谢谢

【问题讨论】:

  • 您的问题到底是什么? “不让它工作”是什么意思?请阅读:stackoverflow.com/help/mcve
  • 嘿,Maciej,我的错。我有一个旧的 cms,我的客户希望在他们的新 nginx 服务器上拥有它。问题是应用程序用于工作,因此它采用whatever.php“whatever”并将其用作动态标题。所以 php 应用程序读取 index.php 作为主页。其他任何内容都作为标题,并且有特定页面内容的 mysql 查找。
  • 请使用所有这些详细信息编辑您的原始问题。确保您清楚地陈述问题并呈现不希望的和期望的效果。

标签: php apache .htaccess nginx


【解决方案1】:

任何不以.php 结尾的URI 都由location / 块处理。如果你想实现你的 Apache .htaccess 规则,那就应该这样做。请参阅this document 了解更多信息。

例如:

location / {
    try_files $uri $uri/ /index.php?_route_=$uri;
}

location ~ \.php$块中,要提防passing uncontrolled requests to PHP,例如:

location ~ \.php$ {
    try_files $uri =404;
    ...
}

【讨论】:

  • 嗨,理查德,感谢您的回复。在我来到 stackoverflow.com 之前,我已经尝试过 aboe 解决方案。它仍然不起作用。这是一个旧网站,但 PHP 功能确实很老派,但设计已修复,客户希望保留一切。但它是在apache上。我在 apacehe 上测试了它,whatever-its-here.php 将成为页面的标题,就像 ond 学校动态标题一样。目前这是我得到的,hostname.com 目前无法处理这个请求。 HTTP 错误 500
猜你喜欢
  • 2016-03-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多