简体   繁体   中英

CakePHP Htaccess 2 Nginx rewrite

We're moving a CakePHP Framework installation to a server where there's an Nginx running. Previous server had Apache. This CakePHP has multiple sub-installations on subfolders which all include the /app/webroot/ folder. We've managed to get the index.php working but all the other files located under /app/webroot/ like javascript and CSS don't link up there.

Now, we've tried getting this to work on nginx with multiple different variations. The problem is, the site loads up PHP files and clean URL'S work. Loading CSS and JS files which are located under /app/webroot/ don't.

We're trying to set up the root to subdomain.example.com where there's an index.php with a header() function to redirect the user to a folder, where there's CakePHP. Basically multiple sites under sub folders. So the CakePHP sites are http://subdomain.example.com/subfolder

Here's the nginx conf we're trying. I've been trying various different options with no effect.

server {
    rewrite ^(.*) http://example.com$1 permanent;
}

server {
    listen         80;
    server_name    example.com www.example.com subdomain.example.com;

    access_log /home/example.com/logs/access.log;
    error_log /home/example.com/logs/error.log error;

    root /home/example.com/public_html/;
    index index.php;

    gzip_static on;

    location /subfolder {
            root /home/example.com/public_html/subfolder/;
            index index.php;

            rewrite ^/subfolder/(/.*)$ /app/webroot$1 break;
            try_files $uri $uri/ /subfolder/app/webroot/index.php?$args;
    }

    location = /favicon.ico {
            log_not_found off;
            access_log off;
    }

    location = /robots.txt {
            allow all;
            log_not_found off;
            access_log off;
    }

    location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
            expires max;
            log_not_found off;
    }

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

app/webroot/ will be your server root. And separate location for process index.php file.

Example:

server {
    listen   80;
    server_name yourserver.com;

    root   /web/path/;
    index  index.php;

    location / {
        rewrite ^(/.*)$ /app/webroot$1 break;
        try_files $uri $uri/ /app/webroot/index.php?$args;
    }

    location ~ \.php$ {
        try_files $uri =404;
        include fastcgi_params;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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