簡體   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