【问题标题】:Cakephp 3 NGINX URL rewriting issueCakephp 3 NGINX URL 重写问题
【发布时间】:2016-01-12 21:32:15
【问题描述】:

我正在尝试在 Linux 服务器 CentOS 7 操作系统上设置 CakePHP 3。我已经使用 composer 创建了一个项目,并且所有内容都以适当的权限正确安装。在我的虚拟主机文件中,我根据official document配置了配置文件

server {
      listen      80;
      server_name remote.inodd.com;
      ## redirect http to https ##
      rewrite 301 https://$server_name$request_uri permanent;
      #rewrite  301 https://$server_name$request_uri permanent;
}

server {
#    listen       80;
    listen       443 ssl;
    server_name  remote.inodd.com;
    ssl_certificate /home/vhost/www/domain/ssl/self-ssl.crt;
    ssl_certificate_key /home/vhost/www/domain/ssl/self-ssl.key;

    # note that these lines are originally from the "location /" block
    root   /home/vhost/www/domain/public_html;
    index index.php;
    access_log /home/vhost/www/domain/logs/access_log;
    error_log /home/vhost/www/domain/logs/error_log;

    location / {
        #try_files $uri $uri/ =404;
        try_files $uri $uri/ /index.php?url=$request_uri;
    }
    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

所以当我浏览 url https://remote.inodd.com/development/ 时,我会得到显示 cakephp 配置状态的默认登录页面。一切都检查正常,甚至数据库连接。但我收到以下警告,即重写配置不正确。

您的服务器上未正确配置 URL 重写。

1) 帮我配置一下

2) 我不/不能使用 URL 重写

不确定我在这里还缺少什么,因为我能够设置非 cakephp 网站并且它们工作正常。

【问题讨论】:

    标签: cakephp nginx cakephp-3.0


    【解决方案1】:

    搞定了。感谢Jamie 提供的出色解决方案。

    这是我的最终工作代码:

    server {
          listen      80;
          server_name remote.inodd.com;
          ## redirect http to https ##
          rewrite        ^ https://$server_name$request_uri? permanent;
    }
    
    server {
    #       listen   80;
        listen       443 ssl;
        server_name  remote.inodd.com;
        ssl_certificate /home/vhost/www/domain/ssl/self-ssl.crt;
        ssl_certificate_key /home/vhost/www/domain/ssl/self-ssl.key;
        access_log /home/vhost/www/domain/logs/access_log;
        error_log /home/vhost/www/domain/logs/error_log;
    
            location / {
                    root   /home/vhost/www/domain/public_html/development/webroot;
                    index  index.php index.html index.htm;
    
                    if (-f $request_filename) {
                            break;
                    }
    
                    if (-d $request_filename) {
                            break;
                    }
                    rewrite ^(.+)$ /index.php?q=$1 last;
            }
    
            location ~ .*\.php[345]?$ {
                    include fastcgi_params;
                    fastcgi_pass    unix:/var/run/php-fpm/php-fpm.sock;
                    fastcgi_index   index.php;
                    fastcgi_param SCRIPT_FILENAME
                    /home/vhost/www/domain/public_html/development/webroot$fastcgi_script_name;
            }
    }
    

    【讨论】:

      【解决方案2】:

      如果您的域在 vhost 中有一个 conf 文件,请将此行添加到您的 vhost domain.conf 文件中。

      location / {
          try_files $uri $uri/ /index.php?$uri&$args;
          rewrite ^/$ /app/webroot/ break;
          rewrite ^(.*)$ /app/webroot/$1 break;
      
      }
      location /app/ {
          rewrite ^/$ /webroot/ break;
          rewrite ^(.*)$ /webroot/$1 break;
      }
      location /app/webroot/ {
          if (!-e $request_filename){
          rewrite ^(.*)$ /index.php break;
          }
      }   
      

      主要问题是 cakephp 有 3 个不同的 htaccess, 首先是项目的主目录 第二个在 app 文件夹中 第三个是 webroot 文件夹。 所以我们必须处理所有这些。我找到了这样的解决方案。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-10-01
        • 1970-01-01
        相关资源
        最近更新 更多