繁体   English   中英

使用 Nginx 重写 CodeIgniter URL

[英]CodeIgniter URL rewrite with Nginx

我正在尝试将 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,所以我丢失了所有的帖子数据......

任何线索?

谢谢

检查这是否有帮助。 替换出现的<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 
    {
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM