使用Nginx重写CodeIgniter URL

问题描述 投票:0回答:1

我正在尝试将CodeIgniter下的网站从Apache迁移到Nginx。我的第一个测试显示出非常好的性能,因此最糟糕的尝试。

但是我找不到正确的Nginx配置来替换那些RewriteRules:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

我在Nginx网站https://www.nginx.com/resources/wiki/start/topics/recipes/codeigniter/上找到了这个食谱

location / {
        try_files $uri $uri/ /index.php;
}

[在大多数情况下都可以使用,除非有斜杠。含义http://example.com/controller/param/

将调用http://example.com/controller/function/index.php(并返回404)

而不是http://example.com/index.php/controller/function/,因为它通常使用Apache重写。

我试图添加一个重写:

rewrite ^/(.*)/$ /$1 permanent;

但是它将POST重定向到GET,所以我丢失了所有的发布数据...

任何线索?

谢谢

codeigniter nginx-location
1个回答
0
投票

检查是否有帮助。替换两个出现的<source_directory>;使用正确的值:

    server {
    listen       80 default_server;
    listen       [::]:80 default_server;
    server_name  _;
    root         <source_directory>;
    rewrite_log  on;
    index index.html index.php;

    # Load configuration files for the default server block.
    include /etc/nginx/default.d/*.conf;

    if ($host ~* ^www\.(.*))
    {
        set $host_without_www $1;
        rewrite ^/(.*)$ $scheme://$host_without_www/$1 permanent;
    }

    # canonicalize codeigniter url end points
    # if your default controller is something other than "welcome" you should change the following
    if ($request_uri ~* ^(/welcome(/index)?|/index(.php)?)/?$)
    {
        rewrite ^(.*)$ / permanent;
    }

    # removes trailing "index" from all controllers
    if ($request_uri ~* index/?$)
    {
        rewrite ^/(.*)/index/?$ /$1 permanent;
    }

    # removes trailing slashes (prevents SEO duplicate content issues)
    if (!-d $request_filename)
    {
        rewrite ^/(.+)/$ /$1 permanent;
    }

    # removes access to "system" folder, also allows a "System.php" controller
    if ($request_uri ~* ^/system)
    {
        rewrite ^/(.*)$ /index.php?/$1 last;
        break;
    }

    # unless the request is for a valid file (image, js, css, etc.), send to bootstrap
    if (!-e $request_filename)
    {
        rewrite ^/(.*)$ /index.php?/$1 last;
        break;
    }

    # use fastcgi for all php files
    location ~ \.php$
    {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME <source_directory>$fastcgi_script_name;
        include fastcgi_params;
    }

    # deny access to apache .htaccess files
    location ~ /\.htaccess
    {
        deny all;
    }

    error_page 404 /404.html;
    location = /40x.html 
    {
    }

    error_page 500 502 503 504 /50x.html;
    location = /50x.html 
    {
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.