【问题标题】:CodeIgniter URL rewrite with Nginx使用 Nginx 重写 CodeIgniter URL
【发布时间】:2020-06-03 21:24:04
【问题描述】:

我正在尝试将 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】:

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

    【讨论】:

    • 谢谢,但是您的代码中的什么可以防止在重写时丢失 POST 数据?我已经测试过rewrite ^/(.+)/$ /$1 permanent;,它将 POST 转换为 GET
    • 尝试使用反向代理支持。一个示例位置部分是:location / { proxy_pass http://localhost:8080; proxy_redirect http://localhost:8080/ /; proxy_read_timeout 60s; # May not need or want to set Host. Should default to the above hostname. proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; }
    • 我应该用我的域/端口替换 localhost:8080 吗?
    猜你喜欢
    • 2011-09-16
    • 2018-02-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-08
    • 1970-01-01
    • 1970-01-01
    • 2016-02-07
    • 2015-03-19
    相关资源
    最近更新 更多