使用PHP的su目录的Nginx配置返回404

时间:2018-10-22 22:36:18

标签: php apache nginx

我有一个使用自定义PHP框架和CMS的网站www.example.com,该CMS加载了完全不同的框架。它最初是在apache2上,现在我正在尝试移至nginx。 www域会加载文件,但是在子目录中加载代码库时,它只是在我的nginx上返回404。如果我切换回apache2,则加载正常。这是我的配置,基本上我是通过查看其他stackoverflow答案来弄清的:

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;
}

}

我尝试了this answer,但运气不佳。我必须正确设置一些基本配置,但似乎无法弄清楚。 / cms中的.htaccess如下:

RewriteEngine On
RewriteBase /cms

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?request=$1 [L,QSA]

编辑:使用this example更新了配置,现在我可以登录CMS了,但它立即进入302重定向循环,我不知道为什么会这样:

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 个答案:

答案 0 :(得分:2)

我想我已经在您的实际代码中找到了您的问题,这部分是location ~ \.php$ { 期望找到文件调用.php是为什么您始终会出现404错误的原因。像这样更新:

location ~ index.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;
}

我建议您使用:fastcgi_pass unix:/run/php/php7.1-fpm.sock;(请检查/run/php/文件夹上的可用套接字),而不要使用本地网络调用php-fpm:9000。表现很好。

答案 1 :(得分:0)

在行索引index.php之后添加此矫正

location / {
   try_files $uri $uri/ /index.php?q=$uri&$args; 
}

致谢

答案 2 :(得分:0)

请注意,NGINX Web服务器不会读取.htaccess。 因此必须完全不读取您提供的.htacces

您可以尝试将其转换为NGINX。 请参阅此stackoverflow问题中可接受的答案。可能会对您有帮助

Change Apache .htaccess file to be used with Nginx