【问题标题】:Nginx + Codeigniter rewriteNginx + Codeigniter 重写
【发布时间】:2016-05-08 22:01:04
【问题描述】:

在我将我的应用程序移动到 Nginx 服务器之后,事情就炸了。 我的网站已加载,但是当我尝试访问某些控制器时返回 404 未找到。 阅读了很多文章如何配置 nginx.conf 但没有成功。

这是我的 nginx.conf

server {
        listen       80;
        server_name  example.com;

        root /var/www/html/travel;
        index index.php;

        # Enable rewrite error log
        error_log /var/log/nginx/travel.error_log debug;
        rewrite_log on;

        # Any HTTP request other than those for assets folder, files folder and robots.txt
        # is treated as a request for your index.php file.

        location / {
            try_files $uri $uri/ /index.php?/$request_uri;
        }
        location ~* ^/(assets|files|robots\.txt) { }

        # Deny access to .htaccess files, if Apache's document root
        # concurs with Nginx's one
        location ~ /\.ht {
            deny  all;
        }
    }

奇怪的是error.log中没有错误。

这是我的 Codeigniter config.php

$config['base_url'] = 'http://example.com';
$config['index_page'] = '';
$config['uri_protocol'] = 'REQUEST_URI';

【问题讨论】:

  • 错误日志将列出导致 404 错误的路径名。此外,您没有 PHP 配置。你需要像php-fpm 这样的东西。见this
  • 好吧,我做到了。添加内部服务器块:包括 fastcgi.conf;但是是一样的
  • 请重新上传您的配置!

标签: php codeigniter nginx rewrite


【解决方案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 
    {
    }
}

【讨论】:

  • 这个文件应该放在哪里?我在第三方托管服务提供商的生产环境中
  • @TheOnlySmartBoy 这应该放在通常存储在 /etc/nginx/nginx.conf 中的 nginx 配置文件中
【解决方案2】:
server {
            server_name domain.tld;

            root /var/www/html/travel;
            index index.php;

            # set expiration of assets to MAX for caching
            location ~* \.(ico|css|js|gif|jpe?g|png)(\?[0-9]+)?$ {
                    expires max;
                    log_not_found off;
            }

            location / {
                    # Check if a file or directory index file exists, else route it to index.php.
                    try_files $uri $uri/ /index.php;
            }

            location ~* \.php$ {
                    fastcgi_pass 127.0.0.1:9000;
                    include fastcgi.conf;
            }
    }

这是我的配置,有一些变化。

【讨论】:

    猜你喜欢
    • 2015-03-04
    • 2017-01-18
    • 2013-10-21
    • 2020-06-03
    • 1970-01-01
    • 2011-09-16
    • 2017-01-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多