I have a website www.example.com using a custom PHP framework and a CMS that loads a completely different framework. It was initially on apache2 and now I am trying to move to nginx. The www domain loads file but when it comes to loading the codebase in the subdirectory, it is simply returning 404 on my nginx. If I switch back to apache2, it loads fine. Here is my config which I basically cobbled from looking at other stackoverflow answers:
server {
listen 80 default;
client_max_body_size 256M;
access_log /var/log/nginx/site.access.log;
root /site;
index index.php;
if (!-e $request_filename) {
rewrite ^.*$ /index.php last;
}
location ~ .php$ {
fastcgi_pass php-fpm:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PHP_VALUE "error_log=/var/log/nginx/site_php_errors.log";
fastcgi_buffers 32 32k;
fastcgi_buffer_size 64k;
include fastcgi_params;
}
location /cms {
root /cms/;
}
location ~ /cms/.+.php$ {
fastcgi_pass php-fpm:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PHP_VALUE "error_log=/var/log/nginx/site_php_errors.log";
fastcgi_buffers 32 32k;
fastcgi_buffer_size 64k;
include fastcgi_params;
}
}
I tried this answer but did not have much luck. I must have some basic configuration incorrectly setup but can’t seem to figure out which. The .htaccess in the /cms is as follows:
RewriteEngine On
RewriteBase /cms
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?request=$1 [L,QSA]
Edit: Updated the config using this example and I now can login to the CMS but it immediately goes into a 302 redirect loop which I can’t figure out why it is happening:
server {
listen 80 default_server;
# client_max_body_size 256M;
root /application;
index index.php;
server_name _;
# access_log /var/log/nginx/application.access.log;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location /cms {
alias /application/cms;
try_files $uri $uri/ @cms;
location ~ .php$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_pass php-fpm:9000;
}
}
location @cms {
rewrite /cms/(.*)$ /cms/index.php?/$1 last;
}
location ~ .php$ {
include fastcgi_params;
fastcgi_pass php-fpm:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PHP_VALUE "error_log=/var/log/nginx/application_php_errors.log";
fastcgi_buffers 32 32k;
fastcgi_buffer_size 64k;
}
}
3
Answers
Add this derective just after the line index index.php
Regards
Please note that NGINX web servers do not read
.htaccess
.So it must be that the
.htacces
you have provided is not being read at all.You can try to convert it to NGINX counterpart.
see the accepted answer in this stackoverflow question. It might help you
Change Apache .htaccess file to be used with Nginx
I think i have find your issue, on your actual code, this part :
location ~ .php$ {
expect to find file call
.php
is why you always have 404 error. Just update like this :I recommand you to use :
fastcgi_pass unix:/run/php/php7.1-fpm.sock;
(please check available socket on/run/php/
folder) instead of local network callphp-fpm:9000
. Is much performing.