NGINX正在为我提供PHP文件而不是执行它们

12

我发现很多人和我有同样的问题,但是我找不到正确的解决方案。

我通过虚拟机和Homestead运行NGINX服务器。在生产环境中,我使用apache,因此我有一个htaccess文件:

RewriteEngine On
RewriteCond %{REQUEST_URI} !(^cms-system/public|^assets)
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ cms-system/public/$1 [L]
我使用这个htaccess重写所有的URL(除了cms-system/public和assets),指向位于cms-system/public下的index.php。
我尝试使用这个工具将htaccess转换为nginx配置:https://winginx.com/en/htaccess,但效果不太好。我做了一些调整,例外规则起作用,但是无法让index.php的重写起作用。请问有人能帮帮我吗?
server {
    listen 80;
    listen 443 ssl http2;
    server_name company.dev;
    root "/home/vagrant/company/";

    index index.html index.htm index.php;

    charset utf-8;

    location ~ ^/cms-system/public/(.*) {

    }

    location ~ ^/assets/(.*) {

    }

    location / {
        if ($request_uri !~ "-f"){
            rewrite ^(.*)$ /cms-system/public/$1 break;
        }
    }

# original location rule
#   location / {
#       try_files $uri $uri/ /index.php?$query_string;
#   }

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

    access_log off;
    error_log  /var/log/nginx/company.dev-error.log error;

    sendfile off;

    client_max_body_size 100m;

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

        fastcgi_intercept_errors off;
        fastcgi_buffer_size 16k;
        fastcgi_buffers 4 16k;
        fastcgi_connect_timeout 300;
        fastcgi_send_timeout 300;
        fastcgi_read_timeout 300;
    }

    location ~ /\.ht {
        deny all;
    }

    ssl_certificate     /etc/nginx/ssl/company.dev.crt;
    ssl_certificate_key /etc/nginx/ssl/company.dev.key;
}

3
你确定是PHP7在创建/var/run/php/php7.0-fpm.sock文件吗?默认的PHP-FPM池必须使用该套接字文件。 - Christos Lytras
你好。让我来帮助你,因为答案真的很糟糕。以下说法是否正确?1)如果我们请求“/images/logo.png”,并且在“<root>/images/logo.png”找不到,但是“<root>/cms-system/public/images/logo.png”存在-需要发送此文件。2)所有未找到的请求(除/assets/*之外)都通过“<root>/cms-system/public/index.php”提供服务。另外,4)您需要支持直接请求,例如“/cms-system/public/index.php”或“/cms-system/public/images/logo.png”吗?5)您需要像“/some.php/custom/path/”这样的请求吗? - Dmitry MiksIr
1个回答

5
这就是你所需的全部内容:
location / {
try_files $uri @rewrite;
}

location @rewrite {
if ($uri !~* "^/(cms-system\/public|assets)$") {set $block "A";}
if (!-e $request_filename) {set $block "${block}B";}
if ($block = "AB") {rewrite ^(.*)$ /cms-system/public/$1 last;}
}

改为:

RewriteEngine On
RewriteCond %{REQUEST_URI} !(^cms-system/public|^assets)
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ cms-system/public/$1 [L]

出于安全考虑,建议执行以下操作:

location ~* \.(engine|inc|info|install|make|module|profile|test|po|sh|.*sql|theme|tpl(\.php)?|xtmpl)$|^(\..*|Entries.*|Repository|Root|Tag|Template)$|\.php_ {
deny all;
}

相关阅读:
将标准Apache的mod_rewrite规则移植到Nginx
如何将mod_rewrite(QSA选项)转换为Nginx等效选项?


3
更正一下,它实际上不起作用了...缓存让我被愚弄了。我能够直接访问资源和valemus/public目录,但只能通过完整路径而非它们成为顶级目录来访问。基本的URL给我返回403错误,而所有其他的URL都导致500错误。 - Martijn Imhoff
3
nginx是否有访问那些文件夹的权限? - Edson Horacio Junior

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接