【问题标题】:NGINX is serving me php files instead of executing themNGINX 正在为我提供 php 文件而不是执行它们
【发布时间】:2017-03-11 14:04:48
【问题描述】:

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

我正在通过 vagrant 和 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;
}

【问题讨论】:

  • 您确定 PHP7 正在创建 /var/run/php/php7.0-fpm.sock 文件吗?默认的 PHP-FPM 池必须使用该套接字文件。
  • 嗨。让我帮助你,因为答案真的很可怕。我对以下陈述是否正确? 1) 如果我们请求 /images/logo.png<root>/images/logo.png 未找到,但 <root>/cms-system/public/images/logo.png 存在 - 需要发送此文件。 2) 通过<root>/cms-system/public/index.php 提供的所有未找到请求(/assets/* 除外)。另外,4) 您是否需要像/cms-system/public/index.php/cms-system/public/images/logo.png 这样的直接请求的支持? 5)你需要像/some.php/custom/path/这样的请求吗?

标签: php .htaccess nginx


【解决方案1】:

这就是你所需要的:

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

另请阅读:
porting-standard-apaches-mod_rewrite-rules-to-nginx
How do I convert mod_rewrite (QSA option) to Nginx equivalent?

【讨论】:

  • 更正......毕竟它不起作用......缓存让我上当了。我可以直接访问 assets 和 valemus/public 目录,但是只能通过完整路径而不是它们都成为顶级目录。基本网址给我一个 403,而所有其他网址都给我一个 500。
  • @MartijnImhoff nginx 是否有权访问这些文件夹?
猜你喜欢
  • 2014-10-24
  • 2017-07-28
  • 1970-01-01
  • 1970-01-01
  • 2019-04-22
  • 2019-07-21
  • 1970-01-01
  • 2014-06-07
  • 1970-01-01
相关资源
最近更新 更多